1

I'm extending the default Retrofit 2 Callback:

abstract class APICallback<T> implements Callback<T> {

    @Override
    public void onResponse(Call<T> call, Response<T> response) {
        if (!response.isSuccessful()) {
            // Cast response.body() to my POJO class, results in null
            MyClass myClass = (MyClass) response.body();
        }
    }

    @Override
    public void onFailure(Call<T> call, Throwable t) {
        // TODO ...
    }
}

How can I downcast the generic type of the Response to my own class? Should this process be automatic? I'm using GsonConverter for the client.

Jumpa
  • 4,319
  • 11
  • 52
  • 100

3 Answers3

1

I think you have to Modify AdapterFactory and then register retrofit

public class APICallAdapterFactory extends CallAdapter.Factory {

@Nullable
@Override
public CallAdapter<?, ?> get(Type returnType, Annotation[] annotations, Retrofit retrofit) {
    final Type responseType = ((ParameterizedType) returnType).getActualTypeArguments()[0];
    return new CallAdapter<Object, Object>() {
        @Override
        public Type responseType() {
            return responseType;
        }

        @Override
        public Object adapt(Call<Object> call) {
            return new APICallAdapter<>(call);
        }
    };
}

and

 private static Retrofit retrofit = new Retrofit.Builder()
        .baseUrl(APIService.baseUrl)
        .client(client)
        .addConverterFactory(GsonConverterFactory.create())
        .addCallAdapterFactory(new APICallAdapterFactory())
        .build();
Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
Taz
  • 1,737
  • 1
  • 15
  • 30
  • Add summary of that link here and demonstrate with some sample code. – Satendra Nov 09 '17 at 07:46
  • Okay, I fixed it :) @Satendra – Taz Nov 09 '17 at 13:38
  • 1
    Although this is old, you recently posted one (now deleted) NAA with a link to that blog. According to your profile, that is your blog. So as a friendly reminder, remember that you need to add disclosure whenever you link to your own blog. Also, excessive self-promotion also puts your posts at risk of being flagged as spam. See [how not to be a spammer](/help/promotion) – Zoe Feb 02 '19 at 16:36
0

Replace T with MyClass

class APICallback implements Callback<MyClass> {

    @Override
    public void onResponse(Call<MyClass> call, Response<MyClass> response) {
        if (response.isSuccessful()) {
            // Cast response.body() to my POJO class, results in null
             MyClass myClass = response.body();
        }
    }

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

    }
}
Gautam
  • 1,345
  • 12
  • 30
0

You can process by this

Gson gson = new Gson();
MyClass myClass = gson.fromJson(response.body(), MyClass.class);
Nirav Joshi
  • 1,713
  • 15
  • 28
  • Should this not be automatic via the GsonConverter I've added to the Retrofit setup? – Jumpa Oct 25 '17 at 12:49
  • No. you have to add this in gradle dependecy and you should write JsonElement instead of your class in Response response – Nirav Joshi Oct 25 '17 at 12:51