This question already has answers here:
How to fetch Data From JSON
                                
                                    (6个答案)
                                
                        
                6年前关闭。
            
        

我正在使用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;
    }
}


Logcat:

'06:35:21.637:E / List活动(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,暂停69ms + 3ms,总计134ms
05-30 06:35:23.642:E / List Values(795):总理曼莫汉·辛格今天将出示UPA II报告卡
:E / List Values(795):美国,印度承诺合作打击恐怖主义
05-30 06:35:23.652:E / List Values(795):美国,印度承诺合作打击恐怖主义
05-30 06:35:23.652:E / List Values(795):暴露了巴基斯坦的真正动机
05-30 06:35:23.652:E / List Values(795):暴露了巴基斯坦的真正动机
05-30 06:35:23.657:E / List值(795):卢比小幅回升,早盘上涨6派萨
05-30 06:35:23.657:E / List值(795):卢比小幅回升,早盘上涨6派萨
05-30 06:35:23.657:E / List Values(795):UP中的43名IPS人员在其职业生涯中转移了40次
05-30 06:35:23.657:E / List Values(795):UP中的43名IPS人员在其职业生涯中转移了40次
05-30 06:35:23.657:E / List Values(795):阿拉哈巴德HC拒绝塔尔沃斯检查14位证人的请求
05-30 06:35:23.657:E / List Values(795):国防部长Shashi Kant Sharma将接任新的CAG
05-30 CAG
05-30 06:35:23.691:E / List Values(795):奥克拉荷马州龙卷风的力量使广岛炸弹相形见war
05-30 06:35:23.691:电子/清单值(795):银行因未偿还定期存款而被罚款50,000卢比
05-30 06:35:58.607:W / Trace(795):nativeGetEnabledTags中的意外值:0

最佳答案

我不喜欢通过提供代码来欺骗您,因此请检查以下链接
以下链接提供了Android JSON解析教程,您的问题将通过此链接得到解决
http://www.androidhive.info/2012/01/android-json-parsing-tutorial/

09-26 18:09