Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.lang.IllegalArgumentException: 'retrofit2.Response' is not a valid response body type. Did you mean ResponseBody?

My question is so similar to Retrofit 2.0 beta 4 response get IllegalArgumentException but its answer didn't help me.

I'm migrating from Retrofit 1.9 to 2.0.2. I'm registering my app for messaging once launches.

    /**
     * Synchronous method to register GCM Token on Backend Server
     *
     * @return true, in case of success response, false otherwise.
     */
    public boolean registerGCMToken()
    {
        ...

        try
        {
            Call<Response> call = mService.registerGCMToken(sessionId, this.ADDITIONAL_QUERY);
            final Response response = call.execute().body(); // <<< Error points here
            final DefaultResponse defaultResponse = DefaultResponse.newInstance(response);
            return defaultResponse.isSuccess();
        }
        catch (IOException ignored)
        {

        }

        return false;
    }

My interface looks like:

    @GET("my/url")
    Call<Response> registerGCMToken(@Header(Constant.HEADER_WILDCARD) String accessToken,
                    @QueryMap Map<String, String> additionalQuery);

Once I launch the app I'm getting this error:

java.lang.IllegalArgumentException: 'retrofit2.Response' is not a valid response body type. Did you mean ResponseBody?

and points to the line I mentioned above.

My newInstance method looks like:

    public static final DefaultResponse newInstance(final Response response)
    {
        final DefaultResponse defaultResponse = new DefaultResponse();
        if (response != null)
        {
            defaultResponse.status = response.code();
        }

        return defaultResponse;
    }

So as you see, I need to know what is the HTTP status code. By changing Response generic to ResponseBody I'm not able to get http status code. What is your recommendation?

like image 954
Hesam Avatar asked Nov 23 '25 10:11

Hesam


1 Answers

What i got while reading answers from the post suggested by you is that instead of this :

@GET("my/url")
    Call<Response> registerGCMToken(@Header(Constant.HEADER_WILDCARD) String accessToken,
                    @QueryMap Map<String, String> additionalQuery);

Try :

@GET("my/url")
    Call<ResponseBody> registerGCMToken(@Header(Constant.HEADER_WILDCARD) String accessToken,
                    @QueryMap Map<String, String> additionalQuery);

Because you are getting ResponseBody and not Response here :

        final Response response = call.execute().body();

Like he did : How can I handle empty response body with Retrofit 2?

like image 129
Sharad Chauhan Avatar answered Nov 25 '25 23:11

Sharad Chauhan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!