imageLoader
来源:互联网 发布:redhat 删除数据库实例 编辑:程序博客网 时间:2024/06/18 15:11
//适配器
package com.example.wz.imageloaderdemo;
import android.content.Context;
import android.graphics.Bitmap;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import java.util.List;
public class MyAdapter extends BaseAdapter{
private Context context;
private List<Data.DataBean.ArticleListBean> list;
private int itemLayoutId;
private ImageLoader imageLoader;
private DisplayImageOptions options;
public MyAdapter(Context context, List<Data.DataBean.ArticleListBean> list, int itemLayoutId) {
this.context = context;
this.list = list;
this.itemLayoutId = itemLayoutId;
//创建默认的ImageLoader配置参数
ImageLoaderConfiguration configuration = ImageLoaderConfiguration
.createDefault(context);
//将configuration配置到imageloader中
imageLoader=ImageLoader.getInstance();
imageLoader.init(configuration);
options=new DisplayImageOptions.Builder()
.cacheInMemory(true)
.cacheOnDisk(true)
.bitmapConfig(Bitmap.Config.ARGB_8888)
.showImageOnLoading(R.mipmap.ic_launcher)
.showImageForEmptyUri(R.mipmap.ic_launcher)
.showImageOnFail(R.mipmap.ic_launcher)
.build();
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder=null;
if(convertView==null){
convertView=View.inflate(context,itemLayoutId,null);
holder=new ViewHolder();
holder.url= (TextView) convertView.findViewById(R.id.url);
holder.image= (ImageView) convertView.findViewById(R.id.image);
convertView.setTag(holder);
}else{
holder= (ViewHolder) convertView.getTag();
}
Data.DataBean.ArticleListBean bean=list.get(position);
holder.url.setText(bean.getImgSrc());
imageLoader.displayImage(bean.getImgSrc(),holder.image,options);
return convertView;
}
class ViewHolder{
TextView url;
ImageView image;
}
}
//网络请求
package com.example.wz.imageloaderdemo;
import android.util.Log;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;
/**
* 类描述:
* 创建人:yekh
* 创建时间:2017/6/16 11:27
*/
public class UrlUtils {
/**
* HttpURLConnection的post请求
* @param urlPath
* @param map
* @return
*/
public static String postUrlConnect(String urlPath, Map<String,Object> map){
StringBuffer sbRequest =new StringBuffer();
if(map!=null&&map.size()>0){
for (String key:map.keySet()){
sbRequest.append(key+"="+map.get(key)+"&");
}
}
String request = sbRequest.substring(0,sbRequest.length()-1);
try {
//创建URL
URL url = new URL(urlPath);
//由URL的openConnection方法得到一个HttpURLConnection(需要强转)
HttpURLConnection httpURLConnection =
(HttpURLConnection) url.openConnection();
//设置post提交
httpURLConnection.setRequestMethod("POST");
//设置超时时间
httpURLConnection.setConnectTimeout(30000);
httpURLConnection.setReadTimeout(30000);
httpURLConnection.setDoInput(true);
httpURLConnection.setDoOutput(true);
//把请求正文通过OutputStream发出去
OutputStream os =httpURLConnection.getOutputStream();
os.write(request.getBytes());
os.flush();
//判断响应码 200 代表成功
if(httpURLConnection.getResponseCode()==200){
//由HttpURLConnection拿到输入流
InputStream in=httpURLConnection.getInputStream();
StringBuffer sb=new StringBuffer();
//根据输入流做一些IO操作
byte [] buff =new byte[1024];
int len=-1;
while((len=in.read(buff))!=-1){
sb.append(new String(buff,0,len,"utf-8"));
}
in.close();
os.close();
httpURLConnection.disconnect();
return sb.toString();
}else{
return null;
}
}catch (Exception e){
Log.e("post","code:"+e.getMessage());
return null;
}
}
/**
* HttpURLConnection的get请求
* @param urlPath
* @return
*/
public static String getUrlConnect(String urlPath){
try {
//创建URL
URL url = new URL(urlPath);
//由URL的openConnection方法得到一个HttpURLConnection(需要强转)
HttpURLConnection httpURLConnection =
(HttpURLConnection) url.openConnection();
//设置连接
httpURLConnection.connect();
//判断响应码 200 代表成功
if(httpURLConnection.getResponseCode()==200){
//由HttpURLConnection拿到输入流
InputStream in=httpURLConnection.getInputStream();
StringBuffer sb=new StringBuffer();
//根据输入流做一些IO操作
byte [] buff =new byte[1024];
int len=-1;
while((len=in.read(buff))!=-1){
sb.append(new String(buff,0,len,"utf-8"));
}
in.close();
httpURLConnection.disconnect();
return sb.toString();
}else{
return null;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
//been
//package com.example.wz.imageloaderdemo;
import java.util.List;
public class Data {
private DataBean data;
private String errorCode;
private boolean success;
private String msg;
public DataBean getData() {
return data;
}
public void setData(DataBean data) {
this.data = data;
}
public String getErrorCode() {
return errorCode;
}
public void setErrorCode(String errorCode) {
this.errorCode = errorCode;
}
public boolean isSuccess() {
return success;
}
public void setSuccess(boolean success) {
this.success = success;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public static class DataBean {
private int size;
private List<ArticleListBean> articleList;
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
public List<ArticleListBean> getArticleList() {
return articleList;
}
public void setArticleList(List<ArticleListBean> articleList) {
this.articleList = articleList;
}
public static class ArticleListBean {
private int _id;
private int id;
private String url;
private String title;
private String writer;
private String category;
private String keywords;
private String description;
private int thumbUp;
private int thumbDown;
private String content;
private String tag;
private long createTime;
private String source;
private String site;
private int readed;
private int comment;
private int applaud;
private String imgSrc;
private String markTime;
private double score;
private double categoryScore;
private boolean haveReaded;
private int property;
private Object time;
private String tags;
private Object typeMapList;
private long recommend_time;
private String first_image;
private String tag_weight;
private double site_weight;
private String cate;
public int get_id() {
return _id;
}
public void set_id(int _id) {
this._id = _id;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getWriter() {
return writer;
}
public void setWriter(String writer) {
this.writer = writer;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public String getKeywords() {
return keywords;
}
public void setKeywords(String keywords) {
this.keywords = keywords;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public int getThumbUp() {
return thumbUp;
}
public void setThumbUp(int thumbUp) {
this.thumbUp = thumbUp;
}
public int getThumbDown() {
return thumbDown;
}
public void setThumbDown(int thumbDown) {
this.thumbDown = thumbDown;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getTag() {
return tag;
}
public void setTag(String tag) {
this.tag = tag;
}
public long getCreateTime() {
return createTime;
}
public void setCreateTime(long createTime) {
this.createTime = createTime;
}
public String getSource() {
return source;
}
public void setSource(String source) {
this.source = source;
}
public String getSite() {
return site;
}
public void setSite(String site) {
this.site = site;
}
public int getReaded() {
return readed;
}
public void setReaded(int readed) {
this.readed = readed;
}
public int getComment() {
return comment;
}
public void setComment(int comment) {
this.comment = comment;
}
public int getApplaud() {
return applaud;
}
public void setApplaud(int applaud) {
this.applaud = applaud;
}
public String getImgSrc() {
return imgSrc;
}
public void setImgSrc(String imgSrc) {
this.imgSrc = imgSrc;
}
public String getMarkTime() {
return markTime;
}
public void setMarkTime(String markTime) {
this.markTime = markTime;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
public double getCategoryScore() {
return categoryScore;
}
public void setCategoryScore(double categoryScore) {
this.categoryScore = categoryScore;
}
public boolean isHaveReaded() {
return haveReaded;
}
public void setHaveReaded(boolean haveReaded) {
this.haveReaded = haveReaded;
}
public int getProperty() {
return property;
}
public void setProperty(int property) {
this.property = property;
}
public Object getTime() {
return time;
}
public void setTime(Object time) {
this.time = time;
}
public String getTags() {
return tags;
}
public void setTags(String tags) {
this.tags = tags;
}
public Object getTypeMapList() {
return typeMapList;
}
public void setTypeMapList(Object typeMapList) {
this.typeMapList = typeMapList;
}
public long getRecommend_time() {
return recommend_time;
}
public void setRecommend_time(long recommend_time) {
this.recommend_time = recommend_time;
}
public String getFirst_image() {
return first_image;
}
public void setFirst_image(String first_image) {
this.first_image = first_image;
}
public String getTag_weight() {
return tag_weight;
}
public void setTag_weight(String tag_weight) {
this.tag_weight = tag_weight;
}
public double getSite_weight() {
return site_weight;
}
public void setSite_weight(double site_weight) {
this.site_weight = site_weight;
}
public String getCate() {
return cate;
}
public void setCate(String cate) {
this.cate = cate;
}
}
}
}
//住方法
package com.example.wz.imageloaderdemo;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.widget.ListView;
import com.google.gson.Gson;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private String urlPath = "http://zkread.com/htnewsroom/v2.0/mobileapp/user-0/categories/articles?itemType=recommendation&itemName=%25E6%258E%25A8%25E8%258D%2590&itemId=-1&page=1&size=20&firstId=1338557";
private ListView lv;
private List<Data.DataBean.ArticleListBean> list;
private MyAdapter adapter;
private Handler mHandler=new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
Gson gson=new Gson();
Data data=gson.fromJson(msg.obj.toString(),Data.class);
list.addAll(data.getData().getArticleList());
adapter.notifyDataSetChanged();
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv= (ListView) findViewById(R.id.lv);
list=new ArrayList<>();
adapter=new MyAdapter(this,list,R.layout.item);
lv.setAdapter(adapter);
loadData();
}
private void loadData() {
new Thread(){
@Override
public void run() {
String result = UrlUtils.getUrlConnect(urlPath);
Message msg= Message.obtain();
msg.what=1;
msg.obj=result;
mHandler.sendMessage(msg);
}
}.start();
}
}
- ImageLoader
- ImageLoader
- ImageLoader
- ImageLoader
- ImageLoader
- imageloader
- ImageLoader
- ImageLoader
- imageLoader
- ImageLoader
- ImageLoader
- ImageLoader
- ImageLoader
- ImageLoader
- imageloader
- ImageLoader
- ImageLoader
- ImageLoader
- Relu-Rectified linear unit激活函数
- javascript 正则表达式使用说明详细教程
- javascript 正则表达式使用说明详细教程
- 关于引起更新源索引时Hash Sum mismatch问题的真正原因及解决方案
- Linux系统的工具目录
- imageLoader
- 因为您要求某些软件包保持现状,就是它们破坏了软件包间的依赖关系
- Tab 实现知多少,?你所知道有多少 你都知道吗( 持续更新中。。。。。。)?
- 51nod 1245 Binomial Coefficients Revenge
- DevSecOps 简介
- myflag step13:接口的完成
- JavaScript正则表达式
- 用python实现爬取百度贴吧的小项目
- myflag step14:打卡功能