解析json的刷新数据
2016-06-01 12:18
513 查看
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;
}
}
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;
}
}
相关文章推荐
- assets+json+下拉+上拉
- JavaScript:语法、关键保留字及变量
- JavaScript:数据类型
- javascript Date转化为24小时格式
- JavaScript:原型链、继承
- JSON解析的成长史——原来还可以这么简单
- JSON解析的成长史——原来还可以这么简单
- js中的原型——__proto__和prototype
- JavaScript:创建对象
- JS 日期工具类-基于yDate
- 时分秒倒计时的js实现
- 判断js对象是否拥有某属性
- FCK在jsp页面中应用
- js登录界面判断输入的用户名和密码是否为空
- js判断登录界面用户名和密码是否为空
- 百度音乐,购物车的全选功能
- 用js获取当前页面的url
- Gson解析Json数据内有非法字符("",回车,etc)时的解决办法
- JS中回调函数的写法
- JS中回调函数的写法