Raj*_*mal 3 android json jsonobject
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;
}
Run Code Online (Sandbox Code Playgroud)
上面的代码工作正常.但是此JSON(链接)URL字符串未转换为JSON对象.我认为这是大字符串也添加错误屏幕截图.
您的测试数据基于[]包含在数组中.您需要将其解析为json数组而不是json对象.
JSONArray jsonArr = new JSONArray(jsonRest);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2585 次 |
最近记录: |