import java.io.IOException;
import java.util.ArrayList;
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.util.EntityUtils;
import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.GridView;
import com.example.bean.Myinfo;
import com.example.bean.Myinfo.MyData;
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 SendActivity extends Activity {
private PullToRefreshGridView gv_2;
private String url;
private int index = 10;
private Adapter adapter;
private List<MyData> data = new ArrayList<Myinfo.MyData>();
private List<MyData> list=new ArrayList<Myinfo.MyData>();
String id;
private int port=1;
Handler handler=new Handler(){
public void handleMessage(Message msg) {
data=(List<MyData>) msg.obj;
if (port==1) {//也就是说第一次联网请求数据
list.clear();//清空集合中的数据
}
list.addAll(data);//将这个数据添加到适配器器上面展示一下
if (adapter==null) {
adapter=new Adapter(list, SendActivity.this);
gv_2.setAdapter(adapter);
gv_2.onRefreshComplete();
}else {
//适配器通知数据发生改变
adapter.notifyDataSetChanged();
gv_2.onRefreshComplete();
}
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_send);
gv_2 = (PullToRefreshGridView) findViewById(R.id.gv_2);
Intent intent = getIntent();
id = intent.getStringExtra("id");
gv_2.setMode(Mode.BOTH);
gv_2.setOnRefreshListener(new OnRefreshListener2<GridView>() {
@Override
public void onPullDownToRefresh(
PullToRefreshBase<GridView> refreshView) {
MyAsyncTask asyncTask = new MyAsyncTask();
index = index + 10;
asyncTask.execute();
}
@Override
public void onPullUpToRefresh(
PullToRefreshBase<GridView> refreshView) {
MyAsyncTask asyncTask = new MyAsyncTask();
index = index + 10;
asyncTask.execute();
}
});
}
class MyAsyncTask extends AsyncTask<Void, Void, List<MyData>> {
@Override
protected List<MyData> doInBackground(Void... params) {
List<MyData> list = null;
url = "http://apis.juhe.cn/goodbook/query?key=9d6ef8c31647a206e05fcaff70527182&catalog_id="
+ id + "&rn=10&pn=20";
port++;
try {
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
int statusCode = httpResponse.getStatusLine().getStatusCode();
if (statusCode == 200) {
HttpEntity entity = httpResponse.getEntity();
String string = EntityUtils.toString(entity, "UTF-8");
Gson gson = new Gson();
Myinfo json = gson.fromJson(string, Myinfo.class);
list=json.getResult().getData();
Log.i("TAG", list.size()+"");
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return list;
}
@Override
protected void onPostExecute(List<MyData> result) {
Message message=Message.obtain();
message.obj=result;
handler.sendMessage(message);
super.onPostExecute(result);
}
}
}