//主方法
package com.example.day9;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.google.gson.Gson;
import com.nostra13.universalimageloader.core.ImageLoader;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import view.XListView;
public class MainActivity extends AppCompatActivity {
private XListView xlv;
List<Bean.DataBean> list = new ArrayList<Bean.DataBean>();
Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if(msg.what==1){
xlv.stopLoadMore();
}else {
Date date=new Date(System.currentTimeMillis());
//yyyy-MM-dd HH:mm:ss
SimpleDateFormat simpleDateFormat=new SimpleDateFormat("HH:mm");
String updateTime=simpleDateFormat.format(date);
//设置刷新时间
xlv.setRefreshTime(updateTime);
xlv.stopRefresh();//关闭头部视图
}
}
};
int index=1;
Myadapter myadapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
xlv = (XListView) findViewById(R.id.xlv);
xlv.setPullRefreshEnable(true);
xlv.setPullLoadEnable(true);
xlv.setXListViewListener(new XListView.IXListViewListener() {
@Override
public void onRefresh() {
Mytask mytask=new Mytask(new Mytask.Icallbacks(){
@Override
public void updateUIjson(String str) {
Gson gson=new Gson();
Bean json = gson.fromJson(str, Bean.class);
list.addAll(0,json.getData());
setAdapter();
handler.sendEmptyMessageDelayed(2,500);
}
});
mytask.execute("http://www.yulin520.com/a2a/impressApi/news/mergeList?pageSize=10&page="+index);
}
@Override
public void onLoadMore() {
index++;
Mytask mytask=new Mytask(new Mytask.Icallbacks(){
@Override
public void updateUIjson(String str) {
Gson gson=new Gson();
Bean json = gson.fromJson(str, Bean.class);
list.addAll(json.getData());
setAdapter();
handler.sendEmptyMessageDelayed(1,500);
}
});
mytask.execute("http://www.yulin520.com/a2a/impressApi/news/mergeList?pageSize=10&page="+index);
}
});
request();
}
private void request(){
Mytask mytask=new Mytask(new Mytask.Icallbacks(){
@Override
public void updateUIjson(String str) {
Gson gson=new Gson();
Bean json = gson.fromJson(str, Bean.class);
list.addAll(json.getData());
setAdapter();
}
});
mytask.execute("http://www.yulin520.com/a2a/impressApi/news/mergeList?pageSize=10&page=1");
}
public void setAdapter(){
if(myadapter==null){
myadapter=new Myadapter();
xlv.setAdapter(myadapter);
}else{
myadapter.notifyDataSetChanged();
}
}
class Myadapter extends BaseAdapter{
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
ViewHolder holder;
if (view == null){
view = View.inflate(MainActivity.this, R.layout.item_layout,null);
holder = new ViewHolder();
holder.imageView=(ImageView) view.findViewById(R.id.image_view);
holder.textView = (TextView)view.findViewById(R.id.text_title);
view.setTag(holder);
}else {
holder = (ViewHolder) view.getTag();
}
holder.textView.setText(list.get(i).getTitle());
ImageLoader.getInstance().displayImage(list.get(i).getUserImg(), holder.imageView);
return view;
}
private class ViewHolder{
ImageView imageView;
TextView textView;
}
}
}
//Bean类
package com.example.day9;
import java.util.List;
/**
* @author zhanghj
* @date 2017/10/12
* @description:
*/
public class Bean {
/**
* code : 1
* success : true
* height : 0
* width : 0
* data : [{"userAge":27,"introduction":"89年/摩羯座/时而安静,时而活泼/喜欢耐心,看重我,脾气好的男生/美工","replyTimes":10,"url":"http://www.yulin520.com/a2a/h5/impress/i1/353?a=1507688804769","createTime":1467367017000,"hxKey":"e5eac6f35952bee01b48d470aef7e30c","img":"http://img1.yulin520.com/news/VQA5D2ZGFFR0O5E1JWUK.jpg#488_597","yulin":"81952","star":44,"title":"柯汝敏 :后现代派美工小姐","occupation":"人力资源","userImg":"http://img1.yulin520.com/news/VQA5D2ZGFFR0O5E1JWUK.jpg#488_597","impressEditId":353,"impressType":0,"topTime":1467367188000,"recommend":0,"remark":"","click":0,"userName":"lxp123","source":0,"reporter":0,"status":1,"content":""},{"userAge":28,"introduction":"88年/巨蟹座/温柔顾家,机智幽默,追求刺激/喜欢温柔,体贴,可爱的女孩","replyTimes":16,"url":"http://www.yulin520.com/a2a/h5/impress/i1/351?a=1507688804769","createTime":1467187123000,"hxKey":"8cc35a10e61912bc39999d82690c3424","img":"http://img1.yulin520.com/news/GQ8129KIXFR0OFTZ9T0H.jpg#640_420","yulin":"81757","star":41,"title":"谢志艺 :坚持自我也是一种自律","occupation":"","userImg":"http://img1.yulin520.com/news/GQ8129KIXFR0OFTZ9T0H.jpg#640_420","impressEditId":351,"impressType":0,"topTime":1467188417000,"recommend":0,"remark":"","click":0,"userName":"81757","source":0,"reporter":0,"status":1,"content":""},{"userAge":25,"introduction":"91年/摩羯座/性格开朗,笑声高调/喜欢高大,帅气,有孝心,有上进心的男生/销售","replyTimes":5,"url":"http://www.yulin520.com/a2a/h5/impress/i1/352?a=1507688804769","createTime":1467188398000,"hxKey":"6d2be6c708681df935787b5c5abfbf15","img":"http://img1.yulin520.com/news/ZQ8P3878YFR0OMXSC079.jpg#723_599","yulin":"81664","star":24,"title":"张晓琳 :笑得高调,活得诚恳 ","occupation":"服务业","userImg":"http://img1.yulin520.com/news/ZQ8P3878YFR0OMXSC079.jpg#723_599","impressEditId":352,"impressType":0,"topTime":1467188571000,"recommend":0,"remark":"","click":0,"userName":"爱我还是他","source":0,"reporter":0,"status":1,"content":""},{"userAge":27,"introduction":"89年/白羊座/温柔耐心,脾气超好/喜欢有礼貌,有孝心,待人真诚的男生/美容","replyTimes":5,"url":"http://www.yulin520.com/a2a/h5/impress/i1/350?a=1507688804769","createTime":1467107647000,"hxKey":"cd98e39b00905343305ea3c4d939054f","img":"http://img1.yulin520.com/news/QQ7914TLXFR0OMOU5OW6.jpg#628_640","yulin":"81787","star":15,"title":"赖心如 :不是林心如,只是个温柔纯真girl","occupation":"美容","userImg":"http://img1.yulin520.com/news/QQ7914TLXFR0OMOU5OW6.jpg#628_640","impressEditId":350,"impressType":0,"topTime":1467107647000,"recommend":0,"remark":"","click":0,"userName":"lai","source":0,"reporter":0,"status":1,"content":""},{"userAge":28,"introduction":"88年/巨蟹座/乐观开朗,积极向上/喜欢大方,有气质,没小脾气的女生/销售","replyTimes":19,"url":"http://www.yulin520.com/a2a/h5/impress/i1/349?a=1507688804769","createTime":1467105885000,"hxKey":"c428fb215a8f35a359c2c172dbd068b7","img":"http://img1.yulin520.com/news/6Q7P1B4OWFR0O0NKIIFY.jpg#800_450","yulin":"81853","star":34,"title":"陈海军: 笑对人生,有什么解决不了","occupation":"房地产","userImg":"http://img1.yulin520.com/news/6Q7P1B4OWFR0O0NKIIFY.jpg#800_450","impressEditId":349,"impressType":0,"topTime":1467106111000,"recommend":0,"remark":"","click":0,"userName":"陳","source":0,"reporter":0,"status":1,"content":""},{"userAge":25,"introduction":"91年/狮子座/不温不火,既能自嗨,也能安静/喜欢高大,踏实,有上进心的男生/实习教师","replyTimes":4,"url":"http://www.yulin520.com/a2a/h5/impress/i1/348?a=1507688804769","createTime":1467021733000,"hxKey":"30b44277869e77cf20f776af08eff8d3","img":"http://img1.yulin520.com/news/VQ5ZYC0PRFR0O7TC7B28.jpg#611_640","yulin":"81811","star":35,"title":"张淼淼 :温柔似水狮子座","occupation":"基础教育","userImg":"http://img1.yulin520.com/news/VQ5ZYC0PRFR0O7TC7B28.jpg#611_640","impressEditId":348,"impressType":0,"topTime":1467021776000,"recommend":0,"remark":"","click":0,"userName":"张焱焱","source":0,"reporter":0,"status":1,"content":""},{"userAge":23,"introduction":"93年/巨蟹座 /温和踏实,乐观直率,完美主义/喜欢成熟,稳重,孝顺的女生","replyTimes":10,"url":"http://www.yulin520.com/a2a/h5/impress/i1/347?a=1507688804769","createTime":1467021492000,"hxKey":"0a7ab699639fc9eb180a78b28413c3ea","img":"http://img1.yulin520.com/news/PQ5RXIJ1OFR0O3VBNR04.jpg#2208_1656","yulin":"78388","star":34,"title":"江南龙 :谁说兵哥哥不识柔情?","occupation":"电子商务","userImg":"http://img1.yulin520.com/news/PQ5RXIJ1OFR0O3VBNR04.jpg#2208_1656","impressEditId":347,"impressType":0,"topTime":1467021591000,"recommend":0,"remark":"","click":0,"userName":"江南","source":0,"reporter":0,"status":1,"content":""},{"userAge":26,"introduction":"90年/金牛座/沉稳内敛,踏实靠谱/喜欢温柔,体贴的女生","replyTimes":12,"url":"http://www.yulin520.com/a2a/h5/impress/i1/345?a=1507688804769","createTime":1466774496000,"hxKey":"bde89a5d6925c70e7bb2f30f6a2c9ccd","img":"http://img1.yulin520.com/news/8Q2PRULPSFR0OM923765.jpg#1136_1136","yulin":"79696","star":28,"title":"巴牛: 把生活过成诗才真牛","occupation":"通信","userImg":"http://img1.yulin520.com/news/8Q2PRULPSFR0OM923765.jpg#1136_1136","impressEditId":345,"impressType":0,"topTime":1466774601000,"recommend":0,"remark":"","click":0,"userName":"巴牛 ","source":0,"reporter":0,"status":1,"content":""},{"userAge":25,"introduction":"91年/摩羯座/爱热闹,性格开朗/喜欢高大,白净,贴心上的男生/HR","replyTimes":11,"url":"http://www.yulin520.com/a2a/h5/impress/i1/346?a=1507688804769","createTime":1466848274000,"hxKey":"103f42c5dec0669a7fe227296f2250bf","img":"http://img1.yulin520.com/news/AQ3EPGR15FR0O36BM2GY.jpg#631_462","yulin":"81505","star":42,"title":"杨函 :什么都能丢,少女心不能丢","occupation":"人力资源","userImg":"http://img1.yulin520.com/news/AQ3EPGR15FR0O36BM2GY.jpg#631_462","impressEditId":346,"impressType":0,"topTime":1466848274000,"recommend":0,"remark":"","click":0,"userName":"涵","source":0,"reporter":0,"status":1,"content":""},{"userAge":33,"introduction":"83年/摩羯座/沉稳踏实,温柔顾家/喜欢善良,单纯,专一的女孩","replyTimes":11,"url":"http://www.yulin520.com/a2a/h5/impress/i1/344?a=1507688804769","createTime":1466773531000,"hxKey":"02f61e28355fa36b885f83507302c3d9","img":"http://img1.yulin520.com/news/IQ24RR99RFR0OW9UKMMT.jpg#629_600","yulin":"80182","star":35,"title":"陈文燃 :生活是一首需要长久坚持的歌","occupation":"特殊建造","userImg":"http://img1.yulin520.com/news/IQ24RR99RFR0OW9UKMMT.jpg#629_600","impressEditId":344,"impressType":0,"topTime":1466773751000,"recommend":0,"remark":"","click":0,"userName":"阿燃","source":0,"reporter":0,"status":1,"content":""}]
* message : null
*/
private int code;
private boolean success;
private int height;
private int width;
private Object message;
private List<DataBean> data;
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public boolean isSuccess() {
return success;
}
public void setSuccess(boolean success) {
this.success = success;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public Object getMessage() {
return message;
}
public void setMessage(Object message) {
this.message = message;
}
public List<DataBean> getData() {
return data;
}
public void setData(List<DataBean> data) {
this.data = data;
}
public static class DataBean {
/**
* userAge : 27
* introduction : 89年/摩羯座/时而安静,时而活泼/喜欢耐心,看重我,脾气好的男生/美工
* replyTimes : 10
* url : http://www.yulin520.com/a2a/h5/impress/i1/353?a=1507688804769
* createTime : 1467367017000
* hxKey : e5eac6f35952bee01b48d470aef7e30c
* img : http://img1.yulin520.com/news/VQA5D2ZGFFR0O5E1JWUK.jpg#488_597
* yulin : 81952
* star : 44
* title : 柯汝敏 :后现代派美工小姐
* occupation : 人力资源
* userImg : http://img1.yulin520.com/news/VQA5D2ZGFFR0O5E1JWUK.jpg#488_597
* impressEditId : 353
* impressType : 0
* topTime : 1467367188000
* recommend : 0
* remark :
* click : 0
* userName : lxp123
* source : 0
* reporter : 0
* status : 1
* content :
*/
private int userAge;
private String introduction;
private int replyTimes;
private String url;
private long createTime;
private String hxKey;
private String img;
private String yulin;
private int star;
private String title;
private String occupation;
private String userImg;
private int impressEditId;
private int impressType;
private long topTime;
private int recommend;
private String remark;
private int click;
private String userName;
private int source;
private int reporter;
private int status;
private String content;
public int getUserAge() {
return userAge;
}
public void setUserAge(int userAge) {
this.userAge = userAge;
}
public String getIntroduction() {
return introduction;
}
public void setIntroduction(String introduction) {
this.introduction = introduction;
}
public int getReplyTimes() {
return replyTimes;
}
public void setReplyTimes(int replyTimes) {
this.replyTimes = replyTimes;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public long getCreateTime() {
return createTime;
}
public void setCreateTime(long createTime) {
this.createTime = createTime;
}
public String getHxKey() {
return hxKey;
}
public void setHxKey(String hxKey) {
this.hxKey = hxKey;
}
public String getImg() {
return img;
}
public void setImg(String img) {
this.img = img;
}
public String getYulin() {
return yulin;
}
public void setYulin(String yulin) {
this.yulin = yulin;
}
public int getStar() {
return star;
}
public void setStar(int star) {
this.star = star;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getOccupation() {
return occupation;
}
public void setOccupation(String occupation) {
this.occupation = occupation;
}
public String getUserImg() {
return userImg;
}
public void setUserImg(String userImg) {
this.userImg = userImg;
}
public int getImpressEditId() {
return impressEditId;
}
public void setImpressEditId(int impressEditId) {
this.impressEditId = impressEditId;
}
public int getImpressType() {
return impressType;
}
public void setImpressType(int impressType) {
this.impressType = impressType;
}
public long getTopTime() {
return topTime;
}
public void setTopTime(long topTime) {
this.topTime = topTime;
}
public int getRecommend() {
return recommend;
}
public void setRecommend(int recommend) {
this.recommend = recommend;
}
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
public int getClick() {
return click;
}
public void setClick(int click) {
this.click = click;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public int getSource() {
return source;
}
public void setSource(int source) {
this.source = source;
}
public int getReporter() {
return reporter;
}
public void setReporter(int reporter) {
this.reporter = reporter;
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
}
//imageloder类+一定记得在清单文件里面把这个类添加一下
package com.example.day9;
import android.app.Application;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
/**
* author:Created by WangZhiQiang on 2017/10/13.
*/
public class Myapp extends Application{
@Override
public void onCreate() {
super.onCreate();
ImageLoaderConfiguration build = new ImageLoaderConfiguration.Builder(this)
.build();
ImageLoader.getInstance().init(build);
}
}
//MyTask类+接口
package com.example.day9;
import android.os.AsyncTask;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
/**
* author:Created by WangZhiQiang on 2017/10/13.
*/
public class Mytask extends AsyncTask<String,Void,String>{
Icallbacks icallbacks;
public Mytask(Icallbacks icallbacks) {
this.icallbacks = icallbacks;
}
@Override
protected String doInBackground(String... strings) {
String str = "";
try {
//使用HttpUrlConnection
URL url=new URL(strings[0]);
HttpURLConnection connection=(HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setReadTimeout(5000);
connection.setConnectTimeout(5000);
if(connection.getResponseCode()==200){
InputStream inputStream=connection.getInputStream();
//调用工具类中的静态方法
str=Streamtostring(inputStream);
}
} catch (MalformedURLException e) {
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}
return str;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
icallbacks.updateUIjson(s);
}
public interface Icallbacks{
void updateUIjson(String str);
}
public String Streamtostring(InputStream inputStream){
StringBuilder builder=new StringBuilder();
try {
BufferedReader br=new BufferedReader(new InputStreamReader(inputStream));
String con;
while ((con=br.readLine())!=null){
builder.append(con);
}
br.close();
return builder.toString();
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
}
//xlistview框架的jar包自己下载,还有第三方的imageloder的jar包.