解析json的刷新数据

原创 2016年06月01日 12:18:28


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;
  
  
 }

}



版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

easyui 下拉框点击ajax后台json数据 实时刷新

员工添加     $(function() {               $("#position").combobox({    &...

AJAX_JSON每秒刷新数据

ASP.NET+Ajax+JQuey+Json数据+存储过程实现无刷新分页

var pageIndex=1; //当前页码 var totalPage; //总页数 $(function () { $.ajax({ ur...

Gson解析json数据

  • 2017-10-13 16:20
  • 206KB
  • 下载

MVC使用ajax异步刷新时怎样输出从后台中传过来的JSON数据

前言 这几天在学习MVC使用AJAX异步刷,因为是新手。所以在js中传参数到后台以及后台返回数据到前台怎么接受,怎么前台遍历出JSON数据都开始不知道,相信新手在使用时跟我一样会遇到,这里我就和大家分...

解析json数据

  • 2017-06-07 14:43
  • 30KB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)