android讀取json array

來源:互聯網
上載者:User

前一段時間寫了一個讀取網頁返回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;    }}

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.