MainActivity
package com.example.yuekaoliaxi_demo01; import gson_vo.Book; import gson_vo.Data; 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.StatusLine; 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 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; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.os.Handler; import android.util.Log; public class HomeActivity extends Activity { private int pn; private String url="http://apis.juhe.cn/goodbook/query?key=9d6ef8c31647a206e05fcaff70527182&rn=10&catalog_id="; private String id,str; private MyBase base; private PullToRefreshGridView grid; private List<Book> data; private List<Book> datas=new ArrayList<Book>(); private Handler handler=new Handler(){ public void handleMessage(android.os.Message msg) { switch (msg.what) { case 0: msg.obj=str; Gson gson=new Gson(); Data da=gson.fromJson(str, Data.class); data=da.getResult().getData(); base=new MyBase(data, HomeActivity.this); grid.setAdapter(base); break; //下拉 case 1: msg.obj=str; Gson gson1=new Gson(); Data da1=gson1.fromJson(str, Data.class); if(da1!=null){ data=da1.getResult().getData(); base=new MyBase(data, HomeActivity.this); grid.setAdapter(base); base.notifyDataSetChanged(); grid.onRefreshComplete(); } break; case 2: msg.obj=str; Gson gson2=new Gson(); Data da2=gson2.fromJson(str, Data.class); if(da2!=null){ List<Book> datas=da2.getResult().getData(); data.addAll(datas); // base=new MyBase(datas, HomeActivity.this); //grid.setAdapter(base); base.notifyDataSetChanged(); grid.onRefreshComplete(); } break; } }; }; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.home); grid=(PullToRefreshGridView) findViewById(R.id.grid); Intent in=getIntent(); id=(String) in.getSerializableExtra("aaa"); Log.i("aaa", "/"+id); grid.setMode(Mode.BOTH); new Thread(){ public void run() { url=url+id; getClinet(); handler.sendMessage(handler.obtainMessage(0,str)); } }.start(); grid.setOnRefreshListener(new OnRefreshListener2() { @Override public void onPullDownToRefresh(PullToRefreshBase refreshView) { // TODO Auto-generated method stub pn++; new Thread(){ public void run() { url=url+id+"&pn="+pn; getClinet(); handler.sendMessage(handler.obtainMessage(1,str)); }; }.start(); } @Override public void onPullUpToRefresh(PullToRefreshBase refreshView) { // TODO Auto-generated method stub pn++; new Thread(){ public void run() { url=url+id+"&pn="+pn; getClinet(); handler.sendMessage(handler.obtainMessage(2,str)); }; }.start(); } }); } private void getClinet() { // TODO Auto-generated method stub HttpClient client=new DefaultHttpClient(); HttpGet get=new HttpGet(url); try { HttpResponse response=client.execute(get); StatusLine statusLine=response.getStatusLine(); int StatusCode=statusLine.getStatusCode(); if(StatusCode==200){ HttpEntity entity=response.getEntity(); str=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(); } }; } |