自己写的
package com.example.lianxi;
import java.io.IOException;
import java.util.LinkedList;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.com.cctest.view.XListView;
import org.com.cctest.view.XListView.IXListViewListener;
import com.bwie.beantuijian.Data;
import com.bwie.beantuijian.Data.DataBean;
import com.google.gson.Gson;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
public class MainActivity extends Activity implements IXListViewListener {
String path = "http://ic.snssdk.com/2/article/v25/stream/?category=news_entertainment&count=20&min_behot_time=1465266126&bd_latitude=4.9E-324&bd_longitude=4.9E-324&bd_loc_time=1465265107&loc_mode=5&lac=4527&cid=28883&iid=4464422609&device_id=17258390367&ac=wifi&channel=baidu&aid=13&app_name=news_article&version_code=460&device_platform=android&device_type=SAMSUNG&os_api=19&os_version=4.4.4&uuid=353040417908000&openudid=84c1c7b192991cc6";
private XListView listView;
private Handler handler = new Handler() {
private Data data;
private LinkedList<DataBean> list;
LinkedList<DataBean> listbean;
private MyAdapter myAdapter;
public void handleMessage(Message msg) {
data = (Data) msg.obj;
switch (msg.arg1) {
case 0:
list = data.getData();
listbean = list;
myAdapter = new MyAdapter(listbean, MainActivity.this);
listView.setAdapter(myAdapter);
stopListView();
break;
case 1:
list.clear();
list = data.getData();
for (DataBean d : list) {
listbean.addFirst(d);
}
myAdapter.notifyDataSetChanged();
stopListView();
break;
case 2:
list.clear();
list = data.getData();
for (DataBean d : list) {
listbean.addLast(d);
}
myAdapter.notifyDataSetChanged();
stopListView();
break;
default:
break;
}
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (XListView) findViewById(R.id.xlistview);
listView.setPullLoadEnable(true);
listView.setPullRefreshEnable(true);
listView.setXListViewListener(this);
httpJson(path, 0);
}
public void httpJson(String path, final int i) {
HttpUtils utils = new HttpUtils();
utils.configCurrentHttpCacheExpiry(0);
utils.send(HttpMethod.GET, path, new RequestCallBack<String>() {
// 获取失败
@Override
public void onFailure(HttpException arg0, String arg1) {
// TODO Auto-generated method stub
}
// 获取成功
@Override
public void onSuccess(ResponseInfo<String> arg0) {
// TODO Auto-generated method stub
String result = arg0.result;
Gson gson = new Gson();
Data data = gson.fromJson(result, Data.class);
Message message = new Message();
message.obj = data;
message.arg1 = i;
handler.sendMessage(message);
}
});
}
/**
* 停止刷新,下拉
*/
public void stopListView() {
listView.stopLoadMore();
listView.stopRefresh();
}
@Override
public void onRefresh() {
// TODO Auto-generated method stub
httpJson(path, 1);
}
@Override
public void onLoadMore() {
// TODO Auto-generated method stub
httpJson(path, 2);
}
}