1

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 オブジェクトを取得しようとしている Web サイトは次のとおりです。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 で同じ URL を使用し、問題なく解析しました。このコードは、私が多くの JSON に使用するコードです。

4

1 に答える 1

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;
于 2013-04-27T21:53:25.970 に答える