我需要以JSON格式获取一些数据,但是我收到了错误消息。在一行中:
04-27 23:49:38.480: E/Lan(19316): {"error_message": "No JSON object could be decoded", "traceback": "Traceback (most recent call last):\n\n File \"/app/.heroku/python/lib/python2.7/site-packages/tastypie/resources.py\", line 202, in wrapper\n response = callback(request, *args, **kwargs)\n\n File \"/app/.heroku/python/lib/python2.7/site-packages/tastypie/resources.py\", line 440, in dispatch_list\n return self.dispatch('list', request, **kwargs)\n\n File \"/app/.heroku/python/lib/python2.7/site-packages/tastypie/resources.py\", line 472, in dispatch\n response = method(request, **kwargs)\n\n File \"/app/.heroku/python/lib/python2.7/site-packages/tastypie/resources.py\", line 1325, in post_list\n deserialized = self.deserialize(request, request.raw_post_data, format=request.META.get('CONTENT_TYPE', 'application/json'))\n\n File \"/app/.heroku/python/lib/python2.7/site-packages/tastypie/resources.py\", line 389, in deserialize\n deserialized = self._meta.serializer.deserialize(data, format=request.META.get('CONTENT_TYPE', 'application/json'))\n\n File \"/app/.heroku/python/lib/python2.7/site-packages/tastypie/serializers.py\", line 205, in deserialize\n deserialized = getattr(self, \"from_%s\" % desired_format)(content)\n\n File \"/app/.heroku/python/lib/python2.7/site-packages/tastypie/serializers.py\", line 359, in from_json\n return simplejson.loads(content)\n\n File \"/app/.heroku/python/lib/python2.7/json/__init__.py\", line 338, in loads\n return _default_decoder.decode(s)\n\n File \"/app/.heroku/python/lib/python2.7/json/decoder.py\", line 365, in decode\n obj, end = self.raw_decode(s, idx=_w(s, 0).end())\n\n File \"/app/.heroku/python/lib/python2.7/json/decoder.py\", line 383, in raw_decode\n raise ValueError(\"No JSON object could be decoded\")\n\nValueError: No JSON object could be decoded\n"}
这是相同的,但更容易看到消息的所有部分:
04-27 23:49:38.480: E/Lan(19316): {"error_message": "No JSON object could be decoded", "traceback": "Traceback (most recent call last):\n\n File \"/app/.heroku/python/lib/python2.7/site-packages/tastypie/resources.py\", line 202, in wrapper\n response = callback(request, *args, **kwargs)\n\n File \"/app/.heroku/python/lib/python2.7/site-packages/tastypie/resources.py\", line 440, in dispatch_list\n return self.dispatch('list', request, **kwargs)\n\n File \"/app/.heroku/python/lib/python2.7/site-packages/tastypie/resources.py\", line 472, in dispatch\n response = method(request, **kwargs)\n\n File \"/app/.heroku/python/lib/python2.7/site-packages/tastypie/resources.py\", line 1325, in post_list\n deserialized = self.deserialize(request, request.raw_post_data, format=request.META.get('CONTENT_TYPE', 'application/json'))\n\n File \"/app/.heroku/python/lib/python2.7/site-packages/tastypie/resources.py\", line 389, in deserialize\n deserialized = self._meta.serializer.deserialize(data, format=request.META.get('CONTENT_TYPE', 'application/json'))\n\n File \"/app/.heroku/python/lib/python2.7/site-packages/tastypie/serializers.py\", line 205, in deserialize\n deserialized = getattr(self, \"from_%s\" % desired_format)(content)\n\n File \"/app/.heroku/python/lib/python2.7/site-packages/tastypie/serializers.py\", line 359, in from_json\n return simplejson.loads(content)\n\n File \"/app/.heroku/python/lib/python2.7/json/init.py\", line 338, in loads\n return _default_decoder.decode(s)\n\n File \"/app/.heroku/python/lib/python2.7/json/decoder.py\", line 365, in decode\n obj, end = self.raw_decode(s, idx=_w(s, 0).end())\n\n File \"/app/.heroku/python/lib/python2.7/json/decoder.py\", line 383, in raw_decode\n raise ValueError(\"No JSON object could be decoded\")\n\nValueError: No JSON object could be decoded\n"}
我尝试获取JSON对象的网站是:http://etrafimdakietkinlikler.herokuapp.com/api/v1/event/?format=json
我的JSONParser类
public class JSONParser {
private static InputStream is = null;
private static String jsonStr = "";
private static JSONObject jObj = null;
public static JSONObject parse(String url) {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
try {
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity entity = httpResponse.getEntity();
is = entity.getContent();
String line = "";
StringBuilder sb = new StringBuilder();
BufferedReader br = new BufferedReader(new InputStreamReader(is), 8);
while ((line = br.readLine()) != null) {
Log.e("Lan",line); //This is where I get the error message
sb.append(line);
}
jsonStr = sb.toString();
jObj = new JSONObject(jsonStr);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return jObj;
}
}
应用程序在jsonStr部分之前提供异常。错误日志就像这样
04-28 00:06:39.650: W/System.err(23208): org.json.JSONException: No value for objects
04-28 00:06:39.655: W/System.err(23208): at org.json.JSONObject.get(JSONObject.java:354)
04-28 00:06:39.655: W/System.err(23208): at org.json.JSONObject.getJSONArray(JSONObject.java:544)
04-28 00:06:39.655: W/System.err(23208): at com.myapp.myapp.Events.onCreate(Events.java:40)
04-28 00:06:39.655: W/System.err(23208): at android.app.Activity.performCreate(Activity.java:5206)
04-28 00:06:39.655: W/System.err(23208): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
04-28 00:06:39.655: W/System.err(23208): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
04-28 00:06:39.655: W/System.err(23208): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
04-28 00:06:39.655: W/System.err(23208): at android.app.ActivityThread.access$600(ActivityThread.java:140)
04-28 00:06:39.655: W/System.err(23208): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
04-28 00:06:39.655: W/System.err(23208): at android.os.Handler.dispatchMessage(Handler.java:99)
04-28 00:06:39.655: W/System.err(23208): at android.os.Looper.loop(Looper.java:137)
04-28 00:06:39.655: W/System.err(23208): at android.app.ActivityThread.main(ActivityThread.java:4898)
04-28 00:06:39.655: W/System.err(23208): at java.lang.reflect.Method.invokeNative(Native Method)
04-28 00:06:39.655: W/System.err(23208): at java.lang.reflect.Method.invoke(Method.java:511)
04-28 00:06:39.655: W/System.err(23208): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
04-28 00:06:39.655: W/System.err(23208): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
04-28 00:06:39.655: W/System.err(23208): at dalvik.system.NativeStart.main(Native Method)
有趣的是我的朋友在iOS中使用了相同的网址并且他解析没有任何问题,而且这个代码是我用于许多JSON的代码。
答案 0 :(得分:1)
不确定你是否需要在这种情况下发帖,但这有效:
JSONObject jobj = null;
try
{
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet("http://etrafimdakietkinlikler.herokuapp.com/api/v1/event/?format=json");
HttpResponse response = httpclient.execute(httpget);
String str = EntityUtils.toString(response.getEntity(), HTTP.UTF_8);
try{
jobj=new JSONObject (str);}
catch(JSONException e){e.printStackTrace();}
}
catch(Exception e)
{
Log.i("NetworkTest", "Network Error: " + e);
}
return jobj;