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 Above code is working fine. But this JSON (Link)URL string is not convert into JSON Object. I think this is big String also add error screen shot.

有帮助吗?

解决方案

Your test data is enclosed in an array based on the []. You need to parse it as a json array and not as a json object.

JSONArray jsonArr = new JSONArray(jsonRest);  

其他提示

Replace

JSONObject jsonObj = new JSONObject(jsonRest); 

With

JSONArray jsonArray = new JSONArray(jsonRest);  

I checked the JSON at http://thirddhaba.appspot.com/api/v1/circle/condensed/data/?circle_id=1

in JSONLint and I noticed that it is JSONArray.

EDIT: FYI

[ ] is used for JSONArray

{ } is used for JSONObject.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top