douzhanshen0657 2017-01-12 13:35
浏览 86

改造响应中的错误json

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 code

    $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);
    ?>   

here is my 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.
Camping 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.
A 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?
One young lady laughed 'they're Jelly Babies!'
Confusion 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.
Knowing 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.
Even though I had escaped this time, my ordeal got a whole lot worse.
Mrs.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"
          }
       ]
    }

here is the error i am getting

  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

when i echo json_encode($json) this gives an error, but output of this i copied in file.json and its working fine.

please help.

  • 写回答

2条回答 默认 最新

  • donglu2008 2017-01-12 15:31
    关注

    Problem could be linked to what you are getting as response and what you are trying to parse. You are thinking its going to start with a Object at the root of your response while in reality the data could be an array. I would suggest you to use a list as the type in callback. like maybe or something similar

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

    edit: just saw some better explanation in the thread

    Hope it helps

    评论

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集