Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/224.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 无法解码任何JSON对象错误_Android_Json - Fatal编程技术网

Android 无法解码任何JSON对象错误

Android 无法解码任何JSON对象错误,android,json,Android,Json,我需要以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\"

我需要以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):{“错误消息”:“无法解码JSON对象”,“回溯”:“回溯(最近一次调用):\n\n File\”/app/.heroku/python/lib/python2.7/site packages/tastypie/resources.py\”,第202行,在包装器响应=回调(请求,*args,**kwargs)\n File\”/app/.heroku/python/lib/python2.7/site packages/tastypie/resources.py\”,第440行,在dispatch\u list\n返回self.dispatch('list',request,**kwargs)\n\n File\“/app/.heroku/python2.7/site packages/tastypie/resources.py\”,第472行,在dispatch\n response=method(request,**kwargs)\n File\”中/app/.heroku/python/lib/python2.7/site packages/tastypi/resources.py\”,第1325行,在post\u list\n反序列化=self.deserialize(request,request.raw\u post\u data,format=request.META.get('CONTENT\u TYPE',application/json'))\n File\”/app/.heroku/python2.7/site-packages/tastypi/resources.py\“,第389行,在反序列化\n deserialized=self.\u meta.serializer.deserialize(数据,格式=request.meta.get('CONTENT\u TYPE','application/json'))\n\n File\”/app/.heroku/python/lib/python2.7/site packages/tastypie/serializers.py\”,第205行,在反序列化\n反序列化的文件中\“/app/.heroku/python/lib/python2.7/site packages/tastypie/serializers.py\”,第359行,在from\u json\n return simplejson.loads(content)\n\n File\“/app/.heroku/python/lib/python2.7/json/init.py\”,第338行,在load\n return\n默认的解码器.decoder.decode(s)\n\n\n文件\“/app/.heroku/python2.7/python/python/decoder.py\“,第365行,在decode\n obj中,end=self.raw\u decode(s,idx=\u w(s,0.end())\n\n File\”/app/.heroku/python/lib/python2.7/json/decoder.py\”,第383行,在raw\u decode\n raise ValueError(\“无法解码任何json对象”)\n\n\nValueError:无法解码任何json对象”

我尝试获取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中使用了相同的url,他解析时没有任何问题,而这段代码正是我在许多JSON中使用的代码。

不确定在这种情况下是否需要发布,但这已经起到了作用:

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;

不确定在这种情况下您是否需要post,但这已经奏效了:

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;

1.请将stacktrace格式化为多行。2.发布jsonStr的内容。字符串编码如何?dutt,它们都是在Post中完成的。Sergey,你的意思是jsonStr的内容也是吗?试试这个:
string str=EntityUtils.toString(response.getEntity(),HTTP.utf8);试试{jobj=new JSONObject(str);}catch(JSONException e){e.printStackTrace();}…如果您需要这些导入,则这些导入如下所示
import org.apache.http.protocol.http;import org.apache.http.util.EntityUtils;
1.请将stacktrace格式化为多行。2.发布jsonStr的内容。字符串编码如何?dutt,它们都是在Post.Sergey中完成的,您是指jsonStr的cont吗ent也可以?试试这个:
String str=EntityUtils.toString(response.getEntity(),HTTP.utf8);试试{jobj=new JSONObject(str);}catch(JSONException e){e.printStackTrace();}…
import org.apache.http.protocol.http;import org.apache.http.util.EntityUtils;
我按照你说的那样做了,但是我现在得到了“org.json.JSONException:No value for title”异常。没关系,这是关于getString部分的。效果很好,谢谢!我看不到title“在这个特定的json中的一部分,但它可能在其他地方,然后取决于您如何从json对象/数组中获取值。我按照您所说的做了,但我现在得到了“org.json.JSONException:No value for title”异常。没关系,这是关于getString部分的。这很好,谢谢!我没有看到“title”“这是特定json的一部分,但它可能在其他地方,然后取决于您如何从json对象/数组中获取值。”