Retrofit with QueryMap

2019-07-03 13:33发布

问题:

I have some request with the same endpoint but parameter and return type are different.

I used @QueryMap for the parameter but I don't know how to write the return type:

Must I write:

@GET("xxx")
Call<List<A1>> groupList1(@QueryMap Map<String, String> options);
@GET("xxx")
Call<List<A2>> groupList2(@QueryMap Map<String, String> options);
@GET("xxx")
Call<List<A3>> groupList3(@QueryMap Map<String, String> options);
....

or there is a shorter solution?

回答1:

You can use JsonElement response type

 @GET("xxx")
    Call<JsonElement> groupList(@QueryMap Map<String, String> options);

Every call you will receive JsonElement which you can convert to JsonObject or JsonArray or even String. You can parse/deserealize it according to the your content

public void onResponse(Call<JsonElement> call, Response<JsonElement> response) {
      JsonElement jsonElement = response.body();
      //JsonArray array = jsonElement.getAsJsonArray();
      //JsonObject Obj = jsonElement .getAsJsonObject();
}