threed+handler解析gson文件,并且上拉加载,下拉刷新

时间:2022-09-15 12:58:12

threed+handler解析gson文件,并且上拉加载,下拉刷新

ThreeActivity.class:

package com.example.yuekao_one_try;

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.example.vo.DataDemo;
import com.example.vo.Demo;
import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshGridView;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.widget.GridView;

public class ThreeActivity extends Activity{
    private PullToRefreshGridView grid_view;
    private List<Demo> data=new ArrayList<Demo>();
    private int p=10;
    private int z=10;
    private Mybasetwo base;
    private String str;
    private String url="http://apis.juhe.cn/goodbook/query?key=9d6ef8c31647a206e05fcaff70527182&catalog_id=246&rn=50&rn=";
     private int i=1;
     private Handler handler=new Handler(){
         public void handleMessage(android.os.Message msg) {
             switch (msg.what) {
            case 0:
                base=new Mybasetwo(data, getApplicationContext());
                grid_view.setAdapter(base);
                break;
            case 3:
            
                  base=new Mybasetwo(data, getApplicationContext());
                    grid_view.setAdapter(base);
                base.notifyDataSetChanged();
                grid_view.onRefreshComplete();
                break;
            case 4:
                

                  base=new Mybasetwo(data, getApplicationContext());
                    grid_view.setAdapter(base);
                    grid_view.onRefreshComplete();
                break;
            default:
                break;
            }
         };
     };
    protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
        setContentView(R.layout.grid);
        grid_view=(PullToRefreshGridView) findViewById(R.id.grid_view);
        grid_view.setMode(Mode.BOTH);
             init();
            grid_view.setOnRefreshListener(new OnRefreshListener<GridView>() {

                @Override
                public void onRefresh(PullToRefreshBase<GridView> refreshView) {
                    // TODO Auto-generated method stub
                       i++;
                          new Thread(){
                               public void run() {
                                   url="http://apis.juhe.cn/goodbook/query?key=9d6ef8c31647a206e05fcaff70527182&catalog_id=246&rn=50&rn="+i;
                                   
                                   getClient();
                                   Gson g=new Gson();
                                DataDemo dd=g.fromJson(str, DataDemo.class);
                                data=dd.getResult().getData();
                                   handler.sendEmptyMessage(4);
                               };
                           }.start();
                }
                  
                    public void onPullUpToRefresh(PullToRefreshBase<GridView> refreshView) {
                        // TODO Auto-generated method stub
                        i++;
                          new Thread(){
                               public void run() {
                                   url="http://apis.juhe.cn/goodbook/query?key=9d6ef8c31647a206e05fcaff70527182&catalog_id=246&rn=50&rn="+i;
                                   getClient();
                                   Gson g=new Gson();
                                DataDemo dd=g.fromJson(str, DataDemo.class);
                                List<Demo> list=dd.getResult().getData();
                                data.addAll(list);
                                
                                   handler.sendEmptyMessage(3);
                                   
                               };
                           }.start();
                    }
                });
            
        }
    private void init() {
        // TODO Auto-generated method stub
           new Thread(){
               public void run() {
                   url="http://apis.juhe.cn/goodbook/query?key=9d6ef8c31647a206e05fcaff70527182&catalog_id=246&rn=50&rn=10";
                   getClient();
                   Gson g=new Gson();
                DataDemo dd=g.fromJson(str, DataDemo.class);
                data=dd.getResult().getData();
                  handler.sendEmptyMessage(0);
                
               };
           }.start();
    }
      private void getClient() {
            // 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();
            }
            
        };
}

再加上适配器,这些就可以刷新出新的数据了