标签:
package com.ce.activity;
import java.io.IOException;
import java.util.List;
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.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;
import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.widget.GridView;
import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import com.handmark.pulltorefresh.library.PullToRefreshGridView;
public class Info extends Activity {
private String id;
private List<Data2> list;
private PullToRefreshGridView gv;
private GvAdapter adapter;
private int pn = 0;
Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
int a = msg.what;
if (a == 0) {
adapter = new GvAdapter(list, Info.this);
gv.setAdapter(adapter);
} else {
adapter.notifyDataSetChanged();
gv.onRefreshComplete();
}
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.info);
// 接收传来的值
Intent intent = getIntent();
String data = intent.getStringExtra("data");
id = intent.getStringExtra("id");
initview();
// 请求数据
getData(0);
// 设置监听
gv.setMode(Mode.BOTH);
gv.setOnRefreshListener(new OnRefreshListener2<GridView>() {
public void onPullDownToRefresh(
PullToRefreshBase<GridView> refreshView) {
new Asy().execute();
gv.onRefreshComplete();
}
public void onPullUpToRefresh(
PullToRefreshBase<GridView> refreshView) {
new Thread() {
public void run() {
String data = httpGet(1);
// 解析数据
Gson gson = new Gson();
SuperClass2 json = gson.fromJson(data,
SuperClass2.class);
List<Data2> list3 = json.getResult().getData();
list.addAll(list.size(), list3);
// /将数据发送handler
handler.sendEmptyMessage(2);
};
}.start();
}
});
}
private void getData(final int i) {
new Thread() {
@Override
public void run() {
super.run();
String data = httpGet(i);
// 解析数据
Gson gson = new Gson();
SuperClass2 json = gson.fromJson(data, SuperClass2.class);
list = json.getResult().getData();
// /将数据发送handler
handler.sendEmptyMessage(0);
}
}.start();
}
private void initview() {
// 找到控件
gv = (PullToRefreshGridView) findViewById(R.id.gridview);
}
private String httpGet(int pn) {
String result = null;
HttpGet get = new HttpGet(
"http://apis.juhe.cn/goodbook/query?key=9d6ef8c31647a206e05fcaff70527182&catalog_id="
+ id + "&pn=" + pn + "&rn=20");
// "http://apis.juhe.cn/goodbook/query?key=9d6ef8c31647a206e05fcaff70527182&catalog_id=242&pn=0&rn=20"
HttpParams parms = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(parms, 5000);
HttpConnectionParams.setSoTimeout(parms, 5000);
HttpClient client = new DefaultHttpClient(parms);
HttpResponse execute;
try {
execute = client.execute(get);
if (execute.getStatusLine().getStatusCode() == 200) {
HttpEntity entity = execute.getEntity();
result = EntityUtils.toString(entity, "utf-8");
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
class Asy extends AsyncTask<Void, Void, List<Data2>> {
@Override
protected List<Data2> doInBackground(Void... params) {
String string = httpGet(2);
// 解析数据
Gson gson = new Gson();
SuperClass2 json = gson.fromJson(string, SuperClass2.class);
List<Data2> list2 = json.getResult().getData();
return list2;
}
@Override
protected void onPostExecute(List<Data2> result) {
list.addAll(0, result);
// 刷新适配器
adapter.notifyDataSetChanged();
super.onPostExecute(result);
}
}
}
标签:
原文地址:http://www.cnblogs.com/weiyangge/p/5454524.html