public class MainActivity extends Activity implements XListView.IXListViewListener{
private XListView xListView;
MyHander hander=new MyHander(this);
private int page=1;
List<News.ListBean> list=new ArrayList<News.ListBean>();
private MyAdapter adapter;
static class MyHander extends Handler{
WeakReference<MainActivity> weakReference;
public MyHander(MainActivity activity){
weakReference=new WeakReference<MainActivity>(activity);
}
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
MainActivity activity = weakReference.get();
if (activity==null){
return;
}
switch (msg.what){
case 1:
List<News.ListBean> listBeen= (List<News.ListBean>) msg.obj;
activity.update(listBeen,1);
break;
case 2:
activity.update((List<News.ListBean>) msg.obj,2);
break;
}
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//去标题
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
xListView = (XListView) findViewById(R.id.xlistview_demo);
xListView.setXListViewListener(this);
xListView.setPullLoadEnable(true);
xListView.setPullRefreshEnable(true);
adapter = new MyAdapter(this, list);
xListView.setAdapter(adapter);
getData(page);
}
private void update(List<News.ListBean> data,int type){
if (type==1){
list.clear();
list.addAll(data);
adapter.notifyDataSetChanged();
xListView.stopRefresh();
xListView.setRefreshTime("最新数据");
}else {
list.addAll(data);
adapter.notifyDataSetChanged();
xListView.stopLoadMore();
}
}
class IAsyncTask extends AsyncTask<Object,Void,String>{
Map params;
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(Object... params) {
Map map = (Map) params[0];
this.params=map;
String path="http://qhb.2dyt.com/Bwei/news";
try {
URL url=new URL(path);
HttpURLConnection httpURLConnenction= (HttpURLConnection) url.openConnection();
httpURLConnenction.setRequestMethod("POST");
httpURLConnenction.setConnectTimeout(10000);
httpURLConnenction.setReadTimeout(10000);
//拼接参数
StringBuffer stringBuffer=new StringBuffer();
Set<String> set = map.keySet();
for (String key : set) {
stringBuffer.append(key);
stringBuffer.append("=");
stringBuffer.append(map.get(key));
stringBuffer.append("&");
}
//发送参数
StringBuffer buffer = stringBuffer.deleteCharAt(stringBuffer.length() - 1);
OutputStream outputStream = httpURLConnenction.getOutputStream();
outputStream.write(stringBuffer.toString().getBytes());
outputStream.flush();
outputStream.close();
if (httpURLConnenction.getResponseCode()==200){
InputStream inputStream = httpURLConnenction.getInputStream();
String result = StringUtils.inputStreamToString(inputStream);
return result;
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Gson gson = new Gson();
News news=gson.fromJson(s,News.class);
Message message = Message.obtain();
message.what= page == 1 ? 1 : 2 ;
//list == 0 ? list.size() : 0;
int page= (int) params.get("page");
message.obj=news.getList();
hander.sendMessage(message);
}
}
//请求数据
private void getData(int page){
Map map=new HashMap();
map.put("page",page);
map.put("postkey","1503d");
new IAsyncTask().execute(map);
}
@Override
public void onRefresh() {
page=1;
getData(1);
}
@Override
public void onLoadMore() {
page=page+1;
getData(page);
}
}