【ListViewJSON】【com.demo.app.api】【JSONProvider】源碼分析及其在工程中作用

來源:互聯網
上載者:User

標籤:android   style   blog   http   ar   io   color   os   sp   

源碼如下:

package com.demo.app.api;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.HashMap;import org.apache.http.HttpEntity;import org.apache.http.HttpResponse;import org.apache.http.client.ClientProtocolException;import org.apache.http.client.HttpClient;import org.apache.http.client.methods.HttpGet;import org.apache.http.impl.client.DefaultHttpClient;import org.json.JSONArray;import org.json.JSONException;import org.json.JSONObject;import android.util.Log;public class JSONProvider {    /**     * 解析     *      * @throws JSONException     */    private static ArrayList<HashMap<String, Object>> Analysis(String jsonStr)            throws JSONException {        /******************* 解析 ***********************/        JSONArray jsonArray = null;        // 初始化list數組對象        ArrayList<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();        jsonArray = new JSONArray(jsonStr);        for (int i = 0; i < jsonArray.length(); i++) {            JSONObject jsonObject = jsonArray.getJSONObject(i);            // 初始化map數組對象            HashMap<String, Object> map = new HashMap<String, Object>();            map.put("title", jsonObject.getString("title"));            list.add(map);        }        return list;    }    public static String getJSONData(String url)            throws ClientProtocolException, IOException {        String result = "";        HttpGet httpGet = new HttpGet(url);        HttpClient httpClient = new DefaultHttpClient();        HttpResponse httpResponse = null;        try {            httpResponse = httpClient.execute(httpGet);            HttpEntity httpEntity = httpResponse.getEntity();            if (httpEntity != null) {                InputStream inputStream = httpEntity.getContent();                result = convertStreamToString(inputStream);            }        } catch (ClientProtocolException e) {            // TODO Auto-generated catch block            e.printStackTrace();        } catch (IOException e) {            throw e;        } finally {            httpClient.getConnectionManager().shutdown();            httpResponse = null;        }        return result;    }    public static String convertStreamToString(InputStream is) {        BufferedReader reader = null;        try {            reader = new BufferedReader(new InputStreamReader(is, "GBK"),// 防止模擬器上的亂碼                    512 * 1024);        } catch (UnsupportedEncodingException e1) {            // TODO Auto-generated catch block            e1.printStackTrace();        }        StringBuilder sb = new StringBuilder();        String line = null;        try {            while ((line = reader.readLine()) != null) {                sb.append(line + "/n");            }        } catch (IOException e) {            Log.e("DataProvier convertStreamToString", e.getLocalizedMessage(),                    e);        } finally {            try {                is.close();            } catch (IOException e) {                e.printStackTrace();            }        }        return sb.toString();    }}

 

【ListViewJSON】【com.demo.app.api】【JSONProvider】源碼分析及其在工程中作用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.