2017-01-12 9 views
-1
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 кодошибка в JSON ответ Модифицированная

$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 и его рабочей штрафа.

помогите пожалуйста.

+0

Как вы разбираете свой json? –

+0

Вы уверены, что это json, который вы отправляете в json_encode? И как json_encode выбрасывает эту ошибку? Разве это не переоснащение? можете ли вы поделиться своим Java-кодом? –

+0

проверить сейчас что-нибудь еще? –

ответ

0

Проблема может быть связана с тем, что вы получаете как ответ, и тем, что вы пытаетесь проанализировать. Вы думаете, что он начнет с объекта в корне вашего ответа, в то время как на самом деле данные могут быть массивом. Я предлагаю вам использовать список в качестве типа обратного вызова. как может быть, или что-то подобное

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

редактировать: только что видело some better explanation in the thread

Надеются, что это помогает

+0

Я добавил код android и php, пожалуйста, проверьте, что я новичок в разработке приложений –

0

Спасибо за помощь проблема теперь решена. json в порядке, но скрытый персонаж есть Я изменил charset на utf8 без bom и теперь его работа.