改造响应中的错误json

时间:2017-01-12 13:35:28

标签: php android json retrofit

private void CallItemApi() {

        final Call<CategoryJoke_Model> itemMainResponse = APIHandler.getApiService().InCategoryJoke(getFiledMapForItemList());

        itemMainResponse.enqueue(new Callback<CategoryJoke_Model>() {
            @Override
            public void onResponse(Response<CategoryJoke_Model> response, Retrofit retrofit) {
                CategoryJoke_Model itemResponse = null;
                itemResponse = response.body();

                if (itemResponse == null) {
                    UIUtils.showToast(CategoryJokes.this, "Server Error !");
                    Logger.logMessage("Item Message", "Server Error !");
                } else {

                    userList = itemResponse.getCategoryjoke();
                    categoryJokeAdapter = new CategoryJoke_Adapter(CategoryJokes.this, userList);
                    listview.setAdapter(categoryJokeAdapter);
                }
            }

            @Override
            public void onFailure(Throwable t) {

                Logger.logMessage("loginResponse", "ResponseFailure");

                t.printStackTrace();
                t.getMessage();

                UIUtils.dismissDialog();    
                Logger.logMessage("Login Execption --> ", t.getMessage());    
            }
        });
    }

    private Map<String, String> getFiledMapForItemList() {

        Map<String, String> map = new HashMap<>();    
        map.put("catid", ""+catid);

        Log.v("log", " Item Map " + map.toString());    
        return map;    
    }

PHP代码

    $catid = $_REQUEST['catid'];
    $result = mysql_query("select * from joke where category_id = '$catid'");
    while($row = mysql_fetch_array($result))
    {
        $results[] = array("id"=>$row['id'],
                            "joke_text"=>$row['joke'],
                            "category_id"=>$row['category_id'],
                            "user_id"=>$row['user_id'],
                            "is_active"=>$row['is_active'],
                            "guest_email"=>$row['guest_email'],
                            "rating_total"=>$row['rating_total'],
                            "rating_count"=>$row['rating_count'],
                            "rating"=>$row['rating'],
                            "guest_name"=>$row['guest_name'],
                            "created_at"=>$row['created_at'],
                            );
    }
    $json = array("categoryjoke"=>$results);
    echo json_encode($json);
    ?>   

这是我的json

    {
       "jokeincategory":"JokeInCategory",
       "categoryjoke":[
          {
             "id":"2507",
             "joke_text":"My mate just spent an hour and a half sat in one spot desperately trying to put up a tent.\r\nCamping noob.",
             "category_id":"8",
             "user_id":"1",
             "is_active":"1",
             "guest_email":"",
             "rating_total":"0",
             "rating_count":"0",
             "rating":"0",
             "guest_name":"",
             "created_at":"2012-08-17 13:57:47"
          },
          {
             "id":"2508",
             "joke_text":"I think we should get some polar bears to help with the evictions at Dale Farm, I heard that they're pretty good at clearing out campsites.",
             "category_id":"8",
             "user_id":"1",
             "is_active":"1",
             "guest_email":"",
             "rating_total":"0",
             "rating_count":"0",
             "rating":"0",
             "guest_name":"",
             "created_at":"2012-08-17 13:57:47"
          },
          {
             "id":"2509",
             "joke_text":"I promised my romantic girlfriend the other day that I would stay up with until we see the sun rise in the British countryside.\r\nA week later I'm starting to fall asleep.",
             "category_id":"8",
             "user_id":"1",
             "is_active":"1",
             "guest_email":"",
             "rating_total":"0",
             "rating_count":"0",
             "rating":"0",
             "guest_name":"",
             "created_at":"2012-08-17 13:57:47"
          },
          {
             "id":"2510",
             "joke_text":"After we all finished our main meal I asked the party if they'd like Baby Jellies?\r\nOne young lady laughed 'they're Jelly Babies!'\r\nConfusion was soon lifted when I brought out the dessert",
             "category_id":"8",
             "user_id":"1",
             "is_active":"1",
             "guest_email":"",
             "rating_total":"0",
             "rating_count":"0",
             "rating":"0",
             "guest_name":"",
             "created_at":"2012-08-17 13:57:47"
          },
          {
             "id":"2511",
             "joke_text":"I despise bears so imagine my horror when out camping in the wilds, I ran into one.\r\nKnowing it was me or him, I didn't give him time to think and blasted him three times in the head with my hunting rifle.\r\nEven though I had escaped this time, my ordeal got a whole lot worse.\r\nMrs.Grylls reported me to the Police and I'm now facing a murder charge.",
             "category_id":"8",
             "user_id":"1",
             "is_active":"1",
             "guest_email":"",
             "rating_total":"0",
             "rating_count":"0",
             "rating":"0",
             "guest_name":"",
             "created_at":"2012-08-17 13:57:47"
          }
       ]
    }

这是我得到的错误

  01-12 18:57:59.277 6847-6847/sk.infotech.golmaaljokes W/System.err: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $
    01-12 18:57:59.277 6847-6847/sk.infotech.golmaaljokes W/System.err:     at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:221)
    01-12 18:57:59.277 6847-6847/sk.infotech.golmaaljokes W/System.err:     at com.google.gson.Gson.fromJson(Gson.java:814)
    01-12 18:57:59.277 6847-6847/sk.infotech.golmaaljokes W/System.err:     at com.google.gson.Gson.fromJson(Gson.java:779)
    01-12 18:57:59.277 6847-6847/sk.infotech.golmaaljokes W/System.err:     at retrofit.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:36)
    01-12 18:57:59.277 6847-6847/sk.infotech.golmaaljokes W/System.err:     at retrofit.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:24)
    01-12 18:57:59.277 6847-6847/sk.infotech.golmaaljokes W/System.err:     at retrofit.OkHttpCall.parseResponse(OkHttpCall.java:148)
    01-12 18:57:59.277 6847-6847/sk.infotech.golmaaljokes W/System.err:     at retrofit.OkHttpCall.access$100(OkHttpCall.java:29)
    01-12 18:57:59.277 6847-6847/sk.infotech.golmaaljokes W/System.err:     at retrofit.OkHttpCall$1.onResponse(OkHttpCall.java:94)
    01-12 18:57:59.277 6847-6847/sk.infotech.golmaaljokes W/System.err:     at com.squareup.okhttp.Call$AsyncCall.execute(Call.java:168)
    01-12 18:57:59.277 6847-6847/sk.infotech.golmaaljokes W/System.err:     at com.squareup.okhttp.internal.NamedRunnable.run(NamedRunnable.java:33)
    01-12 18:57:59.277 6847-6847/sk.infotech.golmaaljokes W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
    01-12 18:57:59.277 6847-6847/sk.infotech.golmaaljokes W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
    01-12 18:57:59.277 6847-6847/sk.infotech.golmaaljokes W/System.err:     at java.lang.Thread.run(Thread.java:818)
    01-12 18:57:59.277 6847-6847/sk.infotech.golmaaljokes W/System.err: Caused by: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $
    01-12 18:57:59.277 6847-6847/sk.infotech.golmaaljokes W/System.err:     at com.google.gson.stream.JsonReader.beginObject(JsonReader.java:387)
    01-12 18:57:59.277 6847-6847/sk.infotech.golmaaljokes W/System.err:     at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:210)
    01-12 18:57:59.277 6847-6847/sk.infotech.golmaaljokes W/System.err:     ... 12 more

当我回显json_encode($ json)时会出现错误,但是我的输出在 file.json 中复制并且工作正常。

请帮忙。

2 个答案:

答案 0 :(得分:0)

问题可能与您获得的响应以及您要解析的内容有关。您正在考虑从响应的根开始使用Object,而实际上数据可能是一个数组。 我建议你使用列表作为回调中的类型。喜欢或许类似的东西

@GET("/persons.json") 
void contacts(Callback<List<person>> cb);

编辑:刚看到some better explanation in the thread

希望有所帮助

答案 1 :(得分:0)

感谢您的帮助 问题现在解决了。 json很好,但隐藏的角色就在那里 我把charset变成了utf8而没有bom,现在正在工作。