2017-10-05 212 views
0

我發現的帖子都沒有幫助我理解我應該做什麼。在此停留了很長一段時間,所以在這裏問。我希望IN和OUT名稱列爲「in」和「out」是json數組。該錯誤在OnFailure響應中顯示爲例外。 getUsers()是主要活動。

private void getUsers() { 


     Retrofit retrofit = new Retrofit.Builder() 
       .baseUrl(BASE_URL) 
       .addConverterFactory(GsonConverterFactory.create()) 
       .build(); 
    APIInterface api = retrofit.create(APIInterface.class); 

    Call<List<In>> call1 = api.getInUsers(); 
    call1.enqueue(new Callback<List<In>>() { 
     @Override 
     public void onResponse(Call<List<In>> call1, Response<List<In>> response) { 
      List<In> inUsers = response.body(); 
      Log.v("InUsers",String.valueOf(inUsers)); 
      data.add(new Vote(Vote.HEADER_TYPE,"IN")); 
      mAdapter.notifyDataSetChanged(); 
      for(In vote : inUsers) { 
       data.add(new Vote(Vote.ITEM_TYPE,String.valueOf(vote))); 
      } 


     } 

     @Override 
     public void onFailure(Call<List<In>> call1, Throwable t) { 
      Toast.makeText(getActivity().getApplicationContext(), t.getMessage() + "IN LIST", Toast.LENGTH_LONG).show(); 
     } 
    }); 
} 

JSON是:

{ 
"message": "Hello World.", 
"planData": { 
    "planId":"44444410", 
    "votesData": { 
     "votes": { 
      "in":[ 
       { 
        "firstName" : "Raj", 
        "lastName" : "Taj" 
       }, 
       { 
        "firstName" : "Badger", 
        "lastName" : "Tagger" 
       }, 
        { 
        "firstName" : "Candle", 
        "lastName" : "Bangle" 
       } 
       ] , 
       "out" : [ 
        { 
        "firstName" : "Bonnie", 
        "lastName" : "Clooney" 
       }, 
       { 
        "firstName" : "Clyde", 
        "lastName" : "Hyde" 
       } 
       ] 
     } 
    } 
} 
} 

接口:

public interface APIInterface { 
@GET("/") 
Call<List<In>> getInUsers(); 

    @GET("/") 
    Call<List<Out>> getOutUsers(); 
} 

我得到的模型從http://www.jsonschema2pojo.org/

在示例類有唯一的消息和planId。我最初在界面中調用示例類。但認爲錯誤可能是因爲這樣,所以將其更改爲列表,但沒有。從網站

POJO代碼:

> 
> -----------------------------------com.example.Example.java----------------------------------- 
> 
> package com.example; 
> 
> import com.google.gson.annotations.Expose; import 
> com.google.gson.annotations.SerializedName; 
> 
> public class Example { 
> 
> @SerializedName("message") @Expose private String message; 
> @SerializedName("planData") @Expose private PlanData planData; 
> 
> public String getMessage() { return message; } 
> 
> public void setMessage(String message) { this.message = message; } 
> 
> public PlanData getPlanData() { return planData; } 
> 
> public void setPlanData(PlanData planData) { this.planData = planData; 
> } 
> 
> } 
> -----------------------------------com.example.In.java----------------------------------- 
> 
> package com.example; 
> 
> import com.google.gson.annotations.Expose; import 
> com.google.gson.annotations.SerializedName; 
> 
> public class In { 
> 
> @SerializedName("firstName") @Expose private String firstName; 
> @SerializedName("lastName") @Expose private String lastName; 
> 
> public String getFirstName() { return firstName; } 
> 
> public void setFirstName(String firstName) { this.firstName = 
> firstName; } 
> 
> public String getLastName() { return lastName; } 
> 
> public void setLastName(String lastName) { this.lastName = lastName; } 
> 
> } 
> -----------------------------------com.example.Out.java----------------------------------- 
> 
> package com.example; 
> 
> import com.google.gson.annotations.Expose; import 
> com.google.gson.annotations.SerializedName; 
> 
> public class Out { 
> 
> @SerializedName("firstName") @Expose private String firstName; 
> @SerializedName("lastName") @Expose private String lastName; 
> 
> public String getFirstName() { return firstName; } 
> 
> public void setFirstName(String firstName) { this.firstName = 
> firstName; } 
> 
> public String getLastName() { return lastName; } 
> 
> public void setLastName(String lastName) { this.lastName = lastName; } 
> 
> } 
> -----------------------------------com.example.PlanData.java----------------------------------- 
> 
> package com.example; 
> 
> import com.google.gson.annotations.Expose; import 
> com.google.gson.annotations.SerializedName; 
> 
> public class PlanData { 
> 
> @SerializedName("planId") @Expose private String planId; 
> @SerializedName("votesData") @Expose private VotesData votesData; 
> 
> public String getPlanId() { return planId; } 
> 
> public void setPlanId(String planId) { this.planId = planId; } 
> 
> public VotesData getVotesData() { return votesData; } 
> 
> public void setVotesData(VotesData votesData) { this.votesData = 
> votesData; } 
> 
> } 
> -----------------------------------com.example.Votes.java----------------------------------- 
> 
> package com.example; 
> 
> import java.util.List; import com.google.gson.annotations.Expose; 
> import com.google.gson.annotations.SerializedName; 
> 
> public class Votes { 
> 
> @SerializedName("in") @Expose private List<In> in = null; 
> @SerializedName("out") @Expose private List<Out> out = null; 
> 
> public List<In> getIn() { return in; } 
> 
> public void setIn(List<In> in) { this.in = in; } 
> 
> public List<Out> getOut() { return out; } 
> 
> public void setOut(List<Out> out) { this.out = out; } 
> 
> } 
> -----------------------------------com.example.VotesData.java----------------------------------- 
> 
> package com.example; 
> 
> import com.google.gson.annotations.Expose; import 
> com.google.gson.annotations.SerializedName; 
> 
> public class VotesData { 
> 
> @SerializedName("votes") @Expose private Votes votes; 
> 
> public Votes getVotes() { return votes; } 
> 
> public void setVotes(Votes votes) { this.votes = votes; } 
> 
> } 
+0

提供您的POJO的代碼 –

回答

0

你必須做如下圖所示。

public interface APIInterface { 
@GET("/") 
Call<Example> getInUsers(); 

    @GET("/") 
    Call<List<Out>> getOutUsers(); 
} 

您的活動如下。

Call<Example> call1 = api.getInUsers(); 
    call1.enqueue(new Callback<Example>() { 
     @Override 
     public void onResponse(Call<Example> call1, Response<Example> response) { 
      List<In> inUsers = response. getPlanData(). getVotesData().getVotes().getIn(); 
      Log.v("InUsers",String.valueOf(inUsers)); 
      data.add(new Vote(Vote.HEADER_TYPE,"IN")); 
      mAdapter.notifyDataSetChanged(); 
      for(In vote : inUsers) { 
       data.add(new Vote(Vote.ITEM_TYPE,String.valueOf(vote))); 
      } 


     } 

     @Override 
     public void onFailure(Call<Example> call1, Throwable t) { 
      Toast.makeText(getActivity().getApplicationContext(), t.getMessage() + "IN LIST", Toast.LENGTH_LONG).show(); 
     } 
    }); 
+0

真棒!我看到了「response.getPlanData()。getVotes .....」這行,並且知道這是它。但只是添加,「response.get ...」不起作用..「response.body()。get ...」的作品。 – L3G3NDj

+0

然後投票回答。 –

+0

String.valueOf(vote)的值在我的手機上打印爲「com」。[email protected]「等等我將如何解決這個問題? – L3G3NDj

0

你正試圖從響應得到JSON數組但rsponse包含的JSONObject

+0

我到底是什麼問題,你剛剛輸入的花花公子。我如何得到該死的名字?開始翻新昨天所以沒有完全理解的工作和概念 – L3G3NDj

+0

它易於理解簡單的JSON與一個主要對象,然後只是嵌套鍵和電影的另一個例子。但是這個json似乎更復雜一點。所以即時變得困惑 – L3G3NDj

1

權的方式來解析服務器響應

更改接口

@GET("/") 
Call<Example> getInUsers(); 

更改呼叫

Call<Example> call1 = api.getInUsers(); 
    call1.enqueue(new Callback<Example>() { 
     @Override 
     public void onResponse(Call<Example> call1, Response<Example> response) { 
      List<In> inUsers = response.body().getPlanData().getVotesData().getVotes().getIn(); 
      ...your other code here... 
0

的API不返回List<In>List<Out>。相反,它返回一個Example對象。因此,而不是

public interface APIInterface { 
@GET("/") 
Call<List<In>> getInUsers(); 

    @GET("/") 
    Call<List<Out>> getOutUsers(); 
} 

你應該使用

public interface APIInterface { 
    @GET("/") 
    Call<Example> getUsers(); 
} 

所以你的電話應該是這樣的:

Call<Example> call = api.getInUsers(); 
call.enqueue(new Callback<Example>() { 
    @Override 
    public void onResponse(Call<Example> call1, Response<Example> response) { 
     // ... whatever you want to do with that data. 
     // E.g. you can access inUsers via response.body().getPlanData().getVotesData().getVotes().getIn(); 
    } 

    // ... 
}); 
+0

真的很棒你。 – L3G3NDj

相關問題