【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();
    }

}
时间: 2024-10-09 03:13:57

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

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

源码如下: package com.demo.app.api; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.util.Map; import org.apache.commons.httpclient.Cookie; import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler; import

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

首先源码如下: package com.demo.app.common; import java.io.File; import java.io.IOException; import java.lang.ref.SoftReference; import java.util.Collections; import java.util.HashMap; import java.util.Map; import java.util.WeakHashMap; import java.util.con

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

源码如下: package com.demo.app.common; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.regex.Pattern; import org.json.JSONArray; import org.json.JSONException; import

【ListViewJson】【com.demo.app】【AppConfig】源码分析及其在工程中作用

源码如下: package com.demo.app; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.Properties; import android.content.Context; import android.content.SharedPreferences; import android.preference.Prefere

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

源码如下: package com.demo.app.common; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import android.content.Context; imp

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

AppManager.java源码如下: package com.demo.app; import java.util.Stack; import android.app.Activity; import android.app.ActivityManager; import android.content.Context; /** * 应用程序Activity管理类:用于Activity管理和应用程序退出 * @version 1.0 * @created 2012-3-21 */ publi

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

首先源码: package com.demo.app.common; import java.io.BufferedOutputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.I

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

源码如下: package com.demo.app.common; import java.util.regex.Pattern; import android.app.Activity; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.grap

【ListViewJson】【com.demo.app】【AppException】源码分析及其在工程中作用

源码如下: package com.demo.app; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.lang.Thread.UncaughtExceptionHandler; import java.net.ConnectException; import java.net.SocketException; i