3

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?

halfer
  • 19,824
  • 17
  • 99
  • 186
Hesam
  • 52,260
  • 74
  • 224
  • 365

1 Answers1

7

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?

Community
  • 1
  • 1
Sharad Chauhan
  • 4,821
  • 2
  • 25
  • 50
  • Thanks Sharad, I actually tested that and the problem is I cannot get HTTP status code from `ResponseBody`. However, if I call the API, Its interface has a method, public void onResponse(Call call, Response response) {...}, that I'm able to get status code from `response`. This is confusing me that how can I get Response in synchronous way. – Hesam May 25 '16 at 06:47
  • @Hesam I have not much idea about that, and ofcourse not more than you. You may check it for synchronous way : http://stackoverflow.com/questions/36425488/how-to-get-http-response-status-in-synchronous-retrofit-2 and this for HTTP code : http://stackoverflow.com/questions/33774940/get-response-status-code-using-retrofit-2-0-and-rxjava – Sharad Chauhan May 25 '16 at 06:53
  • thanks bro, in asynchronous way I have no problem to get it as Response class works fine, same as your docs. – Hesam May 25 '16 at 06:56
  • I am having he same issue. I want to make a synchronous call, and I just want the response status. I don't care about the body. – Zapnologica Jan 08 '17 at 12:59