使用动态键改造gson

时间:2018-11-14 08:59:06

标签: android gson retrofit2

我设法使其在没有动态键的情况下可以正常工作。在此tutorial之后加上此json

当前,我正在使用Retrofit并尝试通过动态键获得响应。但是,响应正文始终为null:

enter image description here

这是响应格式:

{
    "dynamic1": {
        "cityID": "id1",
        "priceRange": 15
    },
    "dynamic2": {
        "cityID": "id2",
        "priceRange": 15
    }
}

在APIUtils.java

    public static CityService getCitiesService() {
        return RetrofitClient.getClient(BASE_URL).create(CityService.class);
    }

在CityService.java

public interface CityService {

    @GET("/SAGetStrollAwayCity")
    Call<CityResponse> getCities();

    @GET("/SAGetStrollAwayCity")
    Call<CityResponse> getCities(@Query("tagged") String tags);
}

在CityResponse.java中:

public class CityResponse {
    /*@SerializedName("results")
    @Expose*/ // is this the correct way?
    private Map<String, CityDetails> city = new HashMap<>();

    public Map<String, CityDetails> getCity() {
        return city;
    }

    public void setCity(Map<String, CityDetails> elemDetails) {
        this.city = elemDetails;
    }

}

在CityDetails.java

public class CityDetails {

    @SerializedName("cityID")
    @Expose
    private String cityID;
    @SerializedName("priceRange")
    @Expose
    private Integer priceRange;

    public String getCityID() {
        return cityID;
    }

    public void setCityID(String cityID) {
        this.cityID = cityID;
    }

    public Integer getPriceRange() {
        return priceRange;
    }

    public void setPriceRange(Integer priceRange) {
        this.priceRange = priceRange;
    }

}

在HomeFragment.java

public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_home, null);
        // 2nd api call
        mServiceCity = ApiUtils.getCitiesService();
        loadCities();

        return rootView;
}

public void loadCities() {
    mServiceCity.getCities().enqueue(new Callback<CityResponse>() {
        @Override
        public void onResponse(Call<CityResponse> call, Response<CityResponse> response) {
         // !!! WHY IS THE RESPONSE BODY EMPTY???
            if(response.isSuccessful()) {

//                    adapter.updateCities(response.body().getCity());

                Log.d("MainActivity", "posts loaded from API"+ response.body().getCity());
            }else {
                int statusCode  = response.code();
                // handle request errors depending on status code
            }
        }

        @Override
        public void onFailure(Call<CityResponse> call, Throwable t) {
            //showErrorMessage();
            Log.d("MainActivity", "error loading from API");

        }
    });
}

1 个答案:

答案 0 :(得分:2)

我个人会放弃CityResponse模型,而只使用Map<String, CityDetails>

改装界面可以是:

public interface CityService {

  @GET("/SAGetStrollAwayCity")
  Call<Map<String, CityDetails>> getCities();

  @GET("/SAGetStrollAwayCity")
  Call<Map<String, CityDetails>> getCities(@Query("tagged") String tags);
}

在您的情况下,响应主体为空,因为CityResponse会映射到以下json:

{
  "city": {
    "dynamic1": {
      "cityID": "id1",
      "priceRange": 15
    },
    "dynamic2": {
      "cityID": "id2",
      "priceRange": 15
    }
  }
}

但是,您没有名为city的根元素。 json本身已经可以映射到Java Map

希望这会有所帮助。