基本信息
源码名称:android Json解析网络数据并显示 例子
源码大小:2.95M
文件格式:.7z
开发语言:Java
更新时间:2015-06-15
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 1 元×
微信扫码支付:1 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
package com.example.android_json; import java.util.ArrayList; import java.util.List; import java.util.Map; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.widget.ListView; import android.widget.Toast; import com.example.android_json.Json.HttpJson; import com.example.android_json.adapter.News_Adapter; import com.handmark.pulltorefresh.library.PullToRefreshBase; import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2; import com.handmark.pulltorefresh.library.PullToRefreshListView; import com.lidroid.xutils.HttpUtils; import com.lidroid.xutils.exception.HttpException; import com.lidroid.xutils.http.ResponseInfo; import com.lidroid.xutils.http.callback.RequestCallBack; import com.lidroid.xutils.http.client.HttpRequest.HttpMethod; public class MainActivity extends Activity { private HttpUtils http=null; private List<Map<String, Object>> news_list=new ArrayList<Map<String,Object>>(); private ListView list; public PullToRefreshListView pulllist; private News_Adapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); loadinit(); pulltolist(); } public void initAdapter(){ adapter=new News_Adapter(this); adapter.setList(news_list); list.setAdapter(adapter); } private void init(){ pulllist=(PullToRefreshListView) findViewById(R.id.list); // list=(ListView) findViewById(R.id.list); list=pulllist.getRefreshableView(); } private void pulltolist(){ pulllist.setOnRefreshListener(new OnRefreshListener2<ListView>() { @Override public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) { // TODO Auto-generated method stub Toast.makeText(MainActivity.this, "下拉刷新!", Toast.LENGTH_SHORT).show(); pulllist.onRefreshComplete(); } @Override public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) { // TODO Auto-generated method stub Toast.makeText(MainActivity.this, "上拉刷新!", Toast.LENGTH_SHORT).show(); refresh(HttpJson.more); } }); } private void loadinit(){ http=new HttpUtils(); String url="http://zhbj.qianlong.com/static/api/news/10006/list_1.json"; http.send(HttpMethod.GET, url,new RequestCallBack<String>() { @Override public void onFailure(HttpException arg0, String arg1) { // TODO Auto-generated method stub handler.sendEmptyMessage(-1); } @Override public void onSuccess(ResponseInfo<String> arg0) { // TODO Auto-generated method stub String str=arg0.result; Message msg=handler.obtainMessage(); getdate(HttpJson.getnews(str)); msg.what=1; handler.sendMessage(msg); } }); } //上拉刷新 private void refresh(String url){ http=new HttpUtils(); http.send(HttpMethod.GET, url,new RequestCallBack<String>() { @Override public void onFailure(HttpException arg0, String arg1) { // TODO Auto-generated method stub handler.sendEmptyMessage(-1); } @Override public void onSuccess(ResponseInfo<String> arg0) { // TODO Auto-generated method stub String str=arg0.result; Message msg=handler.obtainMessage(); getdate(HttpJson.getnews(str)); msg.what=2; handler.sendMessage(msg); } }); } Handler handler=new Handler(){ public void handleMessage(android.os.Message msg) { switch (msg.what) { case -1: Toast.makeText(MainActivity.this, "网络连接失败!", Toast.LENGTH_LONG).show(); break; case 1: initAdapter(); break; case 2: adapter.notifyDataSetChanged(); pulllist.onRefreshComplete(); break; } }; }; public void getdate(List<Map<String, Object>> list){ for (int i = 0; i <list.size(); i ) { Map<String, Object> map=list.get(i); news_list.add(map); } } }