java.lang.Long无法转换为JSONArray

时间:2013-12-06 05:44:06

标签: android jsonp

HttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(
                    "http://gateway.ceylonlinux.com/hayleys2/services/getUserStokDetail?token=40da9b9ed74f672c3871d76a2c87857b&timestamp=0");
            ResponseHandler<String> responseHandler = new BasicResponseHandler();
            String responseBody = httpClient.execute(httpPost, responseHandler);
            JSONObject posts = new JSONObject(responseBody);
            JSONArray jArray = posts.getJSONArray("timestamp");
            Log.i("Tag", jArray.toString());

我尝试上面的代码从服务器检索数据但是当我尝试我得到以下错误

12-06 11:12:27.539: W/System.err(4870): org.json.JSONException: Value 1386308549000 at timestamp of type java.lang.Long cannot be converted to JSONArray

3 个答案:

答案 0 :(得分:2)

timestamp是值,而不是JSONArraystock是JSONArray。得到两个值:

 JSONArray jArray = posts.getJSONArray("stock"); /// get stock JSONArray
 Log.i("Tag", jArray.toString());

 long timestamp=posts.getLong("timestamp"); /// get timestamp

答案 1 :(得分:0)

替换此行

 JSONArray jArray = posts.getJSONArray("timestamp");

有了这个:

 long mTimeStamp = posts.getLong("timestamp");

因为JSON String timestamp中的long value不是array

JSON String stockarraystock,因此您可以使用此 JSONArray jArray = posts.getJSONArray("stock"); 获取

{{1}}

答案 2 :(得分:0)

检查你的Json字符串是否有名为timestamp的jsonArray,或者它只有一个名为timestamp的长变量。你也可以查看这个link的json结构。