0

我正在使用 json 对象解析数据,并且数据也在从服务器解析,它显示在控制台中,但它在模拟器中崩溃了..

这是我的代码:

public class jsonUtil {
    static HttpURLConnection urlConnection;

    public static HttpURLConnection openConnection() throws Exception {
        int responseCode = -1;
        try {
            URL url = new URL("url display");
            urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setRequestMethod("POST");
            urlConnection.setRequestProperty("Content-Type",
                    "application/x-www-form-encoded");
            urlConnection.setRequestProperty(
                    "Content-Length",
                    "" + Integer.toString("category=breaking_news&latitude=&longitude="
                                            .getBytes().length));
            urlConnection.setRequestProperty("Content-Language", "en-US");
            urlConnection.setRequestProperty("Accept-Encoding", "identity");
            urlConnection.setUseCaches(false);
            urlConnection.setDoInput(true);
            urlConnection.setDoOutput(true);
            urlConnection.setReadTimeout(10 * 1000);
            urlConnection.setConnectTimeout(10 * 1000);

            DataOutputStream wr = new DataOutputStream(
                    urlConnection.getOutputStream());
            wr.writeBytes("category=breaking_news&latitude=&longitude=");
            wr.flush();
            wr.close();
            responseCode = urlConnection.getResponseCode();
            if (responseCode != HttpURLConnection.HTTP_OK) {
                throw new Exception("Server not responding");
            }
        } catch (SocketException e) {
            throw new Exception("Connection Time Out");
        } catch (java.net.UnknownHostException unknownHostException) {
            // TODO: handle exception
            throw new Exception("unknownHostException");
        } catch (Exception e) {
            // TODO: handle exception
            throw new Exception("Error Occured");
        }
        return urlConnection;
    }

    public static ArrayList<String> jsonParseList = new ArrayList<String>();

    public static ArrayList<String> jsonParsing() {
        StringBuffer buffer = new StringBuffer();
        JSONArray array;

        try {
            try {
                urlConnection = openConnection();
            } catch (Exception e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }

            BufferedReader bufferedReader = new BufferedReader(
                new InputStreamReader(urlConnection.getInputStream()));
            String line = bufferedReader.readLine();
            while (line != null) {
                buffer.append(line);
                line = bufferedReader.readLine();
            }
            bufferedReader.close();
            if (buffer.toString() != null) {
                try {
                    array = new JSONArray(buffer.toString());
                    JSONObject jsonObject;
                    for (int i = 0; i < array.length(); i++) {
                        jsonObject = array.getJSONObject(i);
                        String conv_title = jsonObject.getString("conv_title");
                        String content = jsonObject.getJSONObject("first_post")
                            .getString("content");
                        String creator = jsonObject.getJSONObject("first_post")
                            .getJSONObject("creator").getString("name");
                        Log.e("List Values", conv_title);
                        jsonParseList.add(conv_title);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return jsonParseList;
    }
}

日志猫:

' 06:35:21.637: E/List Activity(795): [] 05-30 06:35:21.687: W/Trace(795): 来自 nativeGetEnabledTags 的意外值: 0 05-30 06:35:21.697: W/ Trace(795):来自 nativeGetEnabledTags 的意外值:0 05-30 06:35:21.717:W/Trace(795):来自 nativeGetEnabledTags 的意外值:0 05-30 06:35:21.927:I/Choreographer(795):跳过144帧!(795):来自 nativeGetEnabledTags 的意外值:0 05-30 06:35:22.158:I/Choreographer(795):跳过 48 帧!应用程序可能在其主线程上做了太多工作。05-30 06:35:22.317:W/Trace(795):来自 nativeGetEnabledTags 的意外值:0 05-30 06:35:22.317:W/Trace(795):来自 nativeGetEnabledTags 的意外值:0 05-30 06:35 :22.337:W/Trace(795):来自 nativeGetEnabledTags 的意外值:0 05-30 06:35:23.485:D/dalvikvm(795):GC_CONCURRENT 释放 276K,12% 释放 2894K/3288K,

4

2 回答 2

2

我不喜欢通过提供代码来向您提供信息,因此请查看以下链接 Android JSON Parsing tutorial 可在以下链接中找到您的问题将使用此 http://www.androidhive.info/2012/01/android-json-parsing-得到解决教程/

于 2013-05-30T06:34:31.953 回答
0

您可以检查此链接并将您的步骤与此进行比较...

我认为您正在为任何未初始化的控件设置响应数据..

https://github.com/AndroidBegin/Android-JSON-Parse-Images-and-Texts-Tutorial

于 2013-05-30T06:46:00.167 回答