关闭

解析json的刷新数据

47人阅读 评论(0) 收藏 举报


package combwie.map;

import java.util.ArrayList;
import java.util.List;

import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import combwie.map.adapter.MyAdapter1;
import combwie.map.bean.MyResult;
import combwie.map.bean.Rows;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener; 
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2; 

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

public class InfoActivity extends Activity{
 private PullToRefreshListView mPullToRefreshListView;
 private int p=1;
 private MyAdapter1 adapter1;
 private List<Rows> list=new ArrayList<Rows>();
 Handler handler=new Handler(){
  public void handleMessage(android.os.Message msg) {
   switch (msg.what) {
   case 1:
    List<Rows> list1=(List<Rows>) msg.obj;
    adapter1=new MyAdapter1(list, InfoActivity.this);
    mPullToRefreshListView.setAdapter(adapter1);
    break;
   case 2:
    List<Rows> list2=(List<Rows>) msg.obj;
    list.addAll(0, list2);
    adapter1.notifyDataSetChanged();
    mPullToRefreshListView.onRefreshComplete();
    break;
   case 3:
    List<Rows> list3=(List<Rows>) msg.obj;
    list.addAll( list3);
    adapter1.notifyDataSetChanged();
    mPullToRefreshListView.onRefreshComplete();
    break;
   }
  };
 };
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  // TODO Auto-generated method stub
  super.onCreate(savedInstanceState);
  setContentView(R.layout.infoactivity);
  //初始化数据
  initdata();
 }
 private void initdata() {
  // TODO Auto-generated method stub
  mPullToRefreshListView=(PullToRefreshListView) findViewById(R.id.pull_refresh_list);
  mPullToRefreshListView.setMode(Mode.BOTH);
  MyData(1, 0);
  mPullToRefreshListView.setOnRefreshListener(new OnRefreshListener2<ListView>() {

   @Override
   public void onPullDownToRefresh(PullToRefreshBase refreshView) {
    // TODO Auto-generated method stub
    MyData(2, 0);
   }

   @Override
   public void onPullUpToRefresh(PullToRefreshBase refreshView) {
    // TODO Auto-generated method stub
    MyData(3, p++);
   }
  });
  
 }
 public void MyData(final int what,final int id){
 new Thread(){
  public void run() {
   String data = NotworkUtil.getData(id);
   Gson gson=new Gson();
   MyResult myResult = gson.fromJson(data, MyResult.class);
   List<Rows> list=myResult.getResult().getRows();
   handler.sendMessage(handler.obtainMessage(what, list));
  };
 }.start();
 }

}


//notworkutil

package combwie.map;

import java.io.IOException;

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 com.baidu.location.b.e;

public class NotworkUtil {
 public static String url = "
http://api.fang.anjuke.com/m/android/1.3/shouye/recInfosV3/?city_id=14&lat=40.04652&lng=116.306033&api_key=androidkey&sig=9317e9634b5fbc16078ab07abb6661c5&macid=45cd2478331b184ff0e15f29aaa89e3e&app=a-ajk&_pid=11738&o=PE-TL10-user+4.4.2+HuaweiPE-TL10+CHNC00B260+ota-rel-keys%2Crelease-keys&from=mobile&m=Android-PE-TL10&cv=9.5.1&cid=14&i=864601026706713&v=4.4.2&qtime=20160411091603&pm=b61&uuid=1848c59c-185d-48d9-b0e9-782016041109&_chat_id=";

 public static String getData(int id) {
  String rs="";
  HttpGet httpGet=new HttpGet(url+id);
  HttpParams params=new BasicHttpParams();
  HttpConnectionParams.setConnectionTimeout(params, 5*1000);
  HttpConnectionParams.setSoTimeout(params, 5*1000);
  HttpClient httpClient=new DefaultHttpClient();
  try {
   HttpResponse execute = httpClient.execute(httpGet);
   
   int statusCode = execute.getStatusLine().getStatusCode();
   if(statusCode==200){
    HttpEntity entity = execute.getEntity();
    rs=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 rs;
  
  
 }

}



0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:922次
    • 积分:86
    • 等级:
    • 排名:千里之外
    • 原创:8篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    文章存档