2017-06-20 51 views
0

這個翻新有什麼問題?請求是成功的,但沒有值獲得網絡服務翻新解析

這是我的JSON看起來像

[ 
    { 
     "msg": "test", 
     "time": "2017-06-20 00:39:31", 
     "kind": "txt" 
    }, 
    { 
     "msg": "مرحبا يا برو", 
     "time": "2017-06-20 00:40:02", 
     "kind": "txt" 
    }, 
    { 
     "msg": "url", 
     "time": "2017-06-20 01:57:12", 
     "kind": "img" 
    }, 
    { 
     "msg": "url", 
     "time": "2017-06-20 01:58:54", 
     "kind": "video" 
    } 
] 

AllMessagesResponse類

public class AllMessagesResponse { 

    public ArrayList<MessagesInfo> getMessagesInfos() { 
     return messagesInfos; 
    } 

    private ArrayList<MessagesInfo>messagesInfos=new ArrayList<>(); 

} 

MessageInfo類

public class MessagesInfo{ 

    @SerializedName("time") 
    private String time; 

    @SerializedName("msg") 
    private String msgs; 

    @SerializedName("kind") 
    private String kind; 

    @SerializedName("sender") 
    private String senderID; 

    public String getTime() { 
     return time; 
    } 

    public String getMsgs() { 
     return msgs; 
    } 

    public String getKind() { 
     return kind; 
    } 

    public String getSenderID() { 
     return senderID; 
    } 
} 

API類

@POST("chat/veiwPeerToPeer.php") 
    Call<AllMessagesResponse>getMessages(@Body AllMessages allMessages); 

主要活動類

AllMessages allMessages=new AllMessages(); 
     allMessages.senderID=MainActivity.userId; 
     allMessages.receiverID=receiverId; 
     WebService.getInstance().getApi().getMessages(allMessages).enqueue(new Callback<AllMessagesResponse>() { 
      @Override 
      public void onResponse(Call<AllMessagesResponse> call, Response<AllMessagesResponse> response) { 
       AllMessagesResponse allMessagesResponse=response.body(); 
       setTitle(String.valueOf(allMessagesResponse.getMessagesInfos().size())); 
      } 

      @Override 
      public void onFailure(Call<AllMessagesResponse> call, Throwable t) { 

      } 
     }); 

onResponse是工作,但我沒有得到任何值

回答

0

你並不需要另一個POJO的列表,你可以對列表改造直接請求直接提供名單。

Call<List<POJO>> 

我像你一樣獲取相同的JSON響應。 SEE URL

apiInterface = new Retrofit.Builder() 
         .baseUrl(BASE_URL_ANDROIDHIVE) 
         .addConverterFactory(GsonConverterFactory.create()) 
         .build().create(ApiInterface.class); 

       final Call<List<AndroidHive>> hiveCall = apiInterface.getAllMovies(); 

       hiveCall.enqueue(new Callback<List<AndroidHive>>() { 
        @Override 
        public void onResponse(Call<List<AndroidHive>> call, Response<List<AndroidHive>> response) { 

         if (response.isSuccessful()) { 
          progressDialog.dismiss(); 
          List<AndroidHive> hiveList = response.body(); 

          tv_retrofit.setText("Title : " + hiveList.get(3).getTitle() + "\n Image : " + hiveList.get(3).getImage() + "\n Rating : " + hiveList.get(3).getRating() + " \n Release Year : " + hiveList.get(3).getReleaseYear() + "\n Gener : " + hiveList.get(3).getGenre()); 

         } else { 
          Toast.makeText(getActivity(), "Response Failed Code : " + response.message(), Toast.LENGTH_SHORT).show(); 
          progressDialog.dismiss(); 
         } 
        } 

        @Override 
        public void onFailure(Call<List<AndroidHive>> call, Throwable t) { 
         System.out.println(t.getStackTrace().toString()); 
         progressDialog.dismiss(); 
        } 
       });