android读取json array

前一段时间写了一个读取网页返回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 block
			e1.printStackTrace();
		}
		/* 创建HttpResponse对象 */
		HttpResponse response = null;
		try {
			response = httpclient.execute(httppost);
		} catch (ClientProtocolException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		/* 获取这次回应的消息实体 */
		HttpEntity entity = response.getEntity();
		/* 创建一个指向对象实体的数据流 */
		try {
			is = entity.getContent();
		} catch (IllegalStateException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.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 block
				e.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)