首页 > 代码库 > 【ListViewJSON】【com.demo.app.api】【JSONProvider】源码分析及其在工程中作用

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

源码如下:

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】源码分析及其在工程中作用