HttpURLConnection获取JSON处理
来源:互联网 发布:地籍数据标准 编辑:程序博客网 时间:2024/06/16 13:33
package com.zhidi.httpgetjson;import java.io.BufferedReader;import java.io.InputStreamReader;import java.net.HttpURLConnection;import java.net.URL;import java.util.ArrayList;import java.util.List;import org.json.JSONArray;import org.json.JSONException;import org.json.JSONObject;import android.app.Activity;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.util.Log;import android.view.View;import android.view.View.OnClickListener;import android.widget.ArrayAdapter;import android.widget.Button;import android.widget.ListView;public class MainActivity extends Activity {private Button bt_get;private ListView lv_show;/**get请求URL地址*/private static final String URL="";String result=""; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); bt_get.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {showList();}}); } private void init(){ bt_get=(Button)findViewById(R.id.bt_get); lv_show=(ListView)findViewById(R.id.lv_show); } /** * 开启线程访问网络得到数据更行UI */ private void showList(){ final Handler handler = new Handler() { @Override public void handleMessage(Message msg) { if (msg.what == 1) { showListByResult(result); } } }; // 启动线程来执行任务 new Thread() { public void run() { //请求网络 getNoteList(); Message m = new Message(); m.what = 1; // 发送消息到Handler handler.sendMessage(m); } }.start(); } /** * 获取note列表 */ public void getNoteList(){ try { URL url = new URL(URL); HttpURLConnection conn = (HttpURLConnection)url.openConnection(); InputStreamReader is = new InputStreamReader(conn.getInputStream()); BufferedReader bufferedReader = new BufferedReader(is); StringBuffer strBuffer = new StringBuffer(); String line = null; while (null!=(line = bufferedReader.readLine())) { strBuffer.append(line); } result = strBuffer.toString(); Log.i("result:", result); is.close(); conn.disconnect(); } catch (Exception e) { e.printStackTrace(); } } /** * 根据返回结果json解析展示list * @param re */ public void showListByResult(String json){ List<String> noteList = new ArrayList<String>();//列表try {//构建JSON数组对象 JSONArray array = new JSONArray(json);//从JSON数组对象读取数据 for(int i=0 ; i < array.length() ; i++){ //获取各个属性的值 JSONObject item = array.getJSONObject(i); String id = item.getString("id"); String title = item.getString("title"); String content = item.getString("content"); String times = item.getString("times"); //例子中先用一个字段 noteList.add(title); }} catch (JSONException e) {e.printStackTrace();} ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, noteList); lv_show.setAdapter(adapter); } }
0 0
- HttpURLConnection获取JSON处理
- HttpUrlConnection获取网络json串
- HttpURLConnection和HttpClient获取Json数据
- HttpURLConnection和HttpClient获取Json数据
- Android HttpURLConnection数据获取并JSON解析
- Android用HttpURLConnection获取JSON数据
- HttpURLConnection工具类 获取图片+Json
- Android通过HttpURLConnection获取JSON并进行UI更新
- HttpURLConnection post json传参
- HttpURLConnection请求json数据
- HttpURLConnection post json
- HttpURLConnection post json
- HttpURLConnection和HttpClient分别以GET和POST获取Json数据
- 获取JSON格式字符的处理
- jquery.post获取处理json数据
- HttpClient获取服务器JSON数据问题处理
- HttpUrlConnection 获取天气预报
- HttpURLConnection获取网络图片
- 简单的通过两点坐标判断当前两点间距离 ----百度地图 苹果定位
- mongodb 持久化
- 界面控件Essential Studio for ASP.NET MVC 2015 v3正式发布[附下载]
- hdu 4417 Super Mario(划分树)
- 欢迎使用CSDN-markdown编辑器
- HttpURLConnection获取JSON处理
- 简明 Vim 练级攻略
- Android成长(一)——环境搭建
- SHA1算法
- UWP开发之StreamSocket聊天室(五)
- 【Ajax 2】封装Ajax的核心对象:XMLHttpRequest对象
- DPDK环境搭建和测试
- 【Android】解决频繁的线程请求&绘制View引发的异常问题
- Android开发性能优化大总结