Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot cast response body to Custom class with Void as response type #1963

Closed
arushijain45 opened this issue Aug 8, 2016 · 6 comments
Closed

Comments

@arushijain45
Copy link

I am using retrofit2, my use case calls an api which can either return with a response class (say User) or with null. So I am using Void response type. My onReponse() is shown below:

public void onResponse(Call<Void> call, Response<Void> response) {
    if (response.isSuccessful()) {
        if(response.raw().body() != null){
            //returns user response
            User user = (User) response.raw().body().source();
        }
        else{
            //returns null response
        }
    }
}

But I am getting IllegalStateException. Is it something wrong with the code.

@JakeWharton
Copy link
Collaborator

Using Void tells Retrofit to discard the response body entirely. If the API returns a User or the JSON literal null then just declaring the response type as User should work.

@arushijain45
Copy link
Author

But when I was declaring User as Response Type, I was getting onResponse() callback when api returns user but when api returns null I am getting onFailure() callback (where I could not find the reason of failure which might be deserialising issue).

@JakeWharton
Copy link
Collaborator

In that case the failure exception indicates the problem that needs corrected. Please include its entire stacktrace.

@arushijain45
Copy link
Author

Here is the stack trace:

java.io.EOFException: End of input at line 1 column 1 path $
08-09 12:24:55.540 25080-25080/com.x.y I/System.out:     at com.google.gson.stream.JsonReader.nextNonWhitespace(JsonReader.java:1393)
08-09 12:24:55.540 25080-25080/com.x.y I/System.out:     at com.google.gson.stream.JsonReader.doPeek(JsonReader.java:549)
08-09 12:24:55.541 25080-25080/com.x.y I/System.out:     at com.google.gson.stream.JsonReader.peek(JsonReader.java:425)
08-09 12:24:55.541 25080-25080/com.x.y I/System.out:     at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:205)
08-09 12:24:55.542 25080-25080/com.x.y I/System.out:     at retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:37)
08-09 12:24:55.543 25080-25080/com.x.y I/System.out:     at retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:25)
08-09 12:24:55.543 25080-25080/com.x.y I/System.out:     at retrofit2.ServiceMethod.toResponse(ServiceMethod.java:117)
08-09 12:24:55.543 25080-25080/com.x.y I/System.out:     at retrofit2.OkHttpCall.parseResponse(OkHttpCall.java:211)
08-09 12:24:55.543 25080-25080/com.x.y I/System.out:     at retrofit2.OkHttpCall$1.onResponse(OkHttpCall.java:106)
08-09 12:24:55.543 25080-25080/com.x.y I/System.out:     at okhttp3.RealCall$AsyncCall.execute(RealCall.java:133)
08-09 12:24:55.543 25080-25080/com.x.y I/System.out:     at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
08-09 12:24:55.543 25080-25080/com.x.y I/System.out:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
08-09 12:24:55.543 25080-25080/com.x.y I/System.out:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
08-09 12:24:55.544 25080-25080/com.x.y I/System.out:     at java.lang.Thread.run(Thread.java:838)

@JakeWharton
Copy link
Collaborator

Looks like your server returns a 0-byte body in that case. You can use a custom converter that returns null in this case such as #1554 (comment). Make sure you add it before the Gson converter in your Retrofit.Builder.

@arushijain45
Copy link
Author

arushijain45 commented Aug 10, 2016

In my case responseBody.contentLength() is giving me -1 though responseBody.string() is correct (as sent from api). On adding this converter my service ends with onFailure() callback.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants