我在这一个的枪下(大约6个小时得到这个工作),我知道我错过了这里完全简单的东西。Android - 解析单个项目响应JSONObject
我想解析一个单一的数据的JSON响应,但我的解析代码不拾起它。
下面是完整的JSON响应...
{ “ID”: “4480”}
的 “4480” 是一个潜在的字母数字数据的反应,所以它可能是像“ A427“。
这是我用来解析单个响应的代码。问题在于userID为空 - 它不会在JSON响应中拾取4480。有人可以指出我搞砸了吗?非常感谢我提前获得任何帮助!
InputStream is = null;
//http post
try{
String postQuery = "my api post goes here";
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(postQuery);
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
}
//convert response to string
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result=sb.toString();
}catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}
//parse json data
try {
JSONObject userObject = new JSONObject(result);
JSONObject jsonid = userObject.getJSONObject("id");
userID = jsonid.getString("id");
}
什么是搞砸了?你期望的事情没有发生?你有例外吗?如果是这样,请发布堆栈跟踪。 – nhaarman 2012-02-05 13:30:59
userID为空,所以我的解析代码实际上并没有抓取4480 – Rmilligan2372 2012-02-05 13:33:35
因此,如果您尝试记录结果字符串,它是否正确检索数据? – nhaarman 2012-02-05 13:35:26