我正在使用返回JSON的REST Web服务。但是JSON对象值是
" [12]"
当我打印时。该值必须仅为12
,而不是"[]"
我的代码错了吗? 我的代码:
HttpClient httpClient = new DefaultHttpClient();
HttpPost post = new HttpPost("http://applocalize.com.br/rest/rest.php");
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("pg","categorias"));
nameValuePairs.add(new BasicNameValuePair("serv","buscar"));
nameValuePairs.add(new BasicNameValuePair("dt_atualizacao","0"));
try{
post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
}catch (UnsupportedEncodingException e){
}
HttpResponse resp = httpClient.execute(post);
String respStr = EntityUtils.toString(resp.getEntity());
if (resp.getStatusLine().getStatusCode() == 200)
{
System.out.println(respStr);
JSONArray temp1 = new JSONArray(respStr);
for (int i=0; i < temp1.length(); i++) {
JSONObject obj = temp1.getJSONObject(i);
System.out.println(obj.getString("id"));
}
}
System.out.println("OKAY!");
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
当我打印id
时,我的logcat02-20 13:45:32.560 31135-31135/com.localizeapp I/System.out﹕ ["52"]
02-20 13:45:32.560 31135-31135/com.localizeapp I/System.out﹕ ["53"]
02-20 13:45:32.560 31135-31135/com.localizeapp I/System.out﹕ ["54"]
编辑:
respStr:
02-20 13:45:32.210 31135-31135 / com.allapps.localizeapp I / System.out:[ {&#34; id&#34;:[&#34; 51&#34;],&# 34; titulo&#34;:[&#34; Chaveiro&#34;],&#34; icone&#34;:[&#34;这里的基地64 ......太长了&#34;] < / p>
答案 0 :(得分:2)
问题是您的网络服务正在将值返回为JSONArray
。您可以更改Web服务以发送数据
"id":"51"
或者你可以替换
System.out.println(obj.getString("id"));
与
System.out.println(obj.getJSONArray("id").get(0));