2013-08-20 23 views
2
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; 
} 

enter image description here 上面的代码工作正常。但是这个JSON(Link)URL字符串没有转换成JSON对象。我认为这是大字符串还添加错误屏幕截图。大尺寸JSON字符串到JSONObject不起作用

+0

您能否提供更多信息? logcat的输出将非常有用。 – AitorTheRed

+0

@AitorTheRed现在我附上截图请检查 – Rajkamal

+0

如果问题是字符串的大小,你应该考虑使用流解析JSON,例如。使用http://jackson.codehaus.org/ –

回答

4

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

JSONArray jsonArr = new JSONArray(jsonRest); 
+0

我忘了看到这一点,谢谢老板。 – Rajkamal