3
protected class saveBtnClickHandler implements OnClickListener{
    @Override
    public void onClick(View v) {
           String jsonRest = loadJsonDataFromURL("http://thirddhaba.appspot.com/api/v1/circle/condensed/data/?circle_id=1");
    try {
            JSONObject jsonObj = new JSONObject(jsonRest);  

        } catch (JSONException e) {
              android.util.Log.e("JSON Parser", "Error parsing data " + e.toString());
        }
    }
}

protected String loadJsonDataFromURL(String url){
     String jsonStr = null;
    try {
         HttpClient httpclient = new DefaultHttpClient();
            HttpResponse response = httpclient.execute(new HttpGet(url));
            StatusLine statusLine = response.getStatusLine();
            if(statusLine.getStatusCode() == HttpStatus.SC_OK){
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                response.getEntity().writeTo(out);
                out.close();
                 jsonStr = out.toString();

            } else{
                //Closes the connection.
                response.getEntity().getContent().close();
                throw new IOException(statusLine.getReasonPhrase());
            }
    } catch (Exception e) {
        // TODO: handle exception
    }
    return jsonStr;
}

在此处输入图像描述 上面的代码工作正常。但是这个 JSON ( Link ) URL 字符串没有转换成 JSON 对象。我认为这是大字符串还添加错误屏幕截图。

4

2 回答 2

6

您的测试数据包含在基于 [] 的数组中。您需要将其解析为 json 数组而不是 json 对象。

JSONArray jsonArr = new JSONArray(jsonRest);  
于 2013-08-20T12:29:21.023 回答
4

代替

JSONObject jsonObj = new JSONObject(jsonRest); 

JSONArray jsonArray = new JSONArray(jsonRest);  

我检查JSONhttp://thirddhaba.appspot.com/api/v1/circle/condensed/data/?circle_id=1

在 JSONLint 中,我注意到它是 JSONArray。

编辑:仅供参考

[ ] 用于 JSONArray

{ } 用于 JSONObject。

于 2013-08-20T12:35:40.320 回答