android读取json array
来源:互联网 发布:防止sql注入的原理 编辑:程序博客网 时间:2024/05/29 02:03
前一段时间写了一个读取网页返回json array的代码,在这里整理下,以供以后快速的开发类似的程序。
定义一个数据对象类:
package com.example.jsonreadtest;public class MessageItemData {public String mMessageUrl = null;public String mMessageTitle = null;public String mMessageIndex = null;public String mMessageBitmapLocalPosition = null;}
将这个方法写在一个单独的类中,方便程序内各个地方的使用,这个函数的思想是得到网页返回的结果result后将其转化为JSONArray(以前在这个地方弄了很久才弄出来,因为我不清楚是应该将result转成JSONArray还是转成JSONObject),然后循环遍历整个Array,将对象的数据信息存储在MessageItemData中。
package com.example.jsonreadtest;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.UnsupportedEncodingException;import java.util.ArrayList;import java.util.List;import org.apache.http.HttpEntity;import org.apache.http.HttpResponse;import org.apache.http.NameValuePair;import org.apache.http.client.ClientProtocolException;import org.apache.http.client.HttpClient;import org.apache.http.client.entity.UrlEncodedFormEntity;import org.apache.http.client.methods.HttpPost;import org.apache.http.impl.client.DefaultHttpClient;import org.apache.http.message.BasicNameValuePair;import org.json.JSONArray;import org.json.JSONException;import org.json.JSONObject;import android.util.Log;public class DataSource {public static String IMAGEURL = "ImageUrl";public static String MESSAGETITLE = "MessageTitle";public static String MESSAGEINDEX = "MessageIndex";public static String MESSAGECONTENT = "MessageContent";public static String MAINURL = "http://120.11.181.104/android.php?act=three";public static String COLUMNURL = "http://120.11.181.104/android.php?act=two";public static String SUBJECTURL = "http://120.11.181.104/android.php?act=one";public static void getJsonArrayContent(String pUrl) {List<MessageItemData> messageItemDataList = new ArrayList<MessageItemData>();/* 存放http请求得到的结果 */String result = "";String ss = null;JSONObject jObject = null;/* 将要发送的数据封包 */ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();nameValuePairs.add(new BasicNameValuePair("year", "1980"));InputStream is = null;// http post/* 创建一个HttpClient的一个对象 */HttpClient httpclient = new DefaultHttpClient();/* 创建一个HttpPost的对象 */HttpPost httppost = new HttpPost(pUrl);/* 设置请求的数据 */try {httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));} catch (UnsupportedEncodingException e1) {// TODO Auto-generated catch blocke1.printStackTrace();}/* 创建HttpResponse对象 */HttpResponse response = null;try {response = httpclient.execute(httppost);} catch (ClientProtocolException e1) {// TODO Auto-generated catch blocke1.printStackTrace();} catch (IOException e1) {// TODO Auto-generated catch blocke1.printStackTrace();}/* 获取这次回应的消息实体 */HttpEntity entity = response.getEntity();/* 创建一个指向对象实体的数据流 */try {is = entity.getContent();} catch (IllegalStateException e1) {// TODO Auto-generated catch blocke1.printStackTrace();} catch (IOException e1) {// TODO Auto-generated catch blocke1.printStackTrace();}try {BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);StringBuilder sb = new StringBuilder();String line = null;while ((line = reader.readLine()) != null) {sb.append(line + "/n");}is.close();result = sb.toString();JSONArray jArray;try {jArray = new JSONArray(result);for (int i = 0; i < jArray.length(); i++) {MessageItemData messageItemData = new MessageItemData();JSONObject json_data = jArray.getJSONObject(i);messageItemData.mMessageUrl = json_data.getString(IMAGEURL);messageItemData.mMessageIndex = json_data.getString(MESSAGEINDEX);messageItemData.mMessageTitle = json_data.getString(MESSAGETITLE);messageItemDataList.add(messageItemData);}} catch (JSONException e) {// TODO Auto-generated catch blocke.printStackTrace();}} catch (Exception e) {}// parse json data}}
主程序调用:
public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); /*connecting();*/ new ReadTextMessageThread().start(); } class ReadTextMessageThread extends Thread {public void run() {DataSource.getJsonArrayContent(DataSource.MAINURL);}} @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; }}
- android读取json array
- android 读取JSON格式文件
- Android读取JSON格式数据
- Android - 读取JSON文件数据
- android 分析读取JSON对象
- Android xml文件里读取string array
- android读取服务器json格式数据
- android 读取本地的json文件
- Android 读取app内json配置文件
- Android 读取assets中的Json文件
- 【实训】Android读取学校信息json文件
- Jsp读取数据库返回json数据,Android客户端接收json
- Jsp读取数据库返回json数据,Android客户端接收json
- Jsp读取数据库返回json数据,Android客户端接收json
- Android Apps 网络通信(http json/array请求 )
- Android 传参需要List/Array,用json转换一下
- Json 读取
- 读取Json
- 程序员面试什么最重要?
- 使用m2eclipse插件创建web maven项目
- UVa 10946 Do You What Filled? (DFS)
- js object
- boost库在工作(9)引用计数的智能指针shared_ptr之二
- android读取json array
- TSM实施怪异考
- 自己做了一款新浪博客自动访客工具
- sfs2x安装
- 常见开源协议介绍
- SQL查询刚插入的自增长列
- Analysis and implement some simple sorting algorithm.(2)——heapsort
- C++内存泄露检测(Visual studio 2010)
- 设计模式之零:开篇