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

how can i get status code 204 on failure in retrofit 2 and empty body #2494

Closed
1 of 3 tasks
umeshpatel046 opened this issue Sep 23, 2017 · 5 comments
Closed
1 of 3 tasks

Comments

@umeshpatel046
Copy link

umeshpatel046 commented Sep 23, 2017

What kind of issue is this?

  • Question. This issue tracker is not the place for questions. If you want to ask how to do
    something, or to understand why something isn't working the way you expect it to, use Stack
    Overflow. https://stackoverflow.com/questions/tagged/retrofit

  • Bug report. If you’ve found a bug, spend the time to write a failing test. Bugs with tests
    get fixed. Here’s an example: https://gist.github.com/swankjesse/6608b4713ad80988cdc9

  • Feature Request. Start by telling us what problem you’re trying to solve. Often a solution
    already exists! Don’t send pull requests to implement new features without first getting our
    support. Sometimes we leave features out on purpose to keep the project small.

@umeshpatel046 umeshpatel046 changed the title how can i get status code 204 on failure in retrofit 2 and body null how can i get status code 204 on failure in retrofit 2 and empty body Sep 23, 2017
@JakeWharton
Copy link
Member

JakeWharton commented Sep 24, 2017 via email

@JakeWharton
Copy link
Member

Without a failing test or example it's impossible to know what's going on.

@lianshengzhang001
Copy link

A 204 should not go to onFailure. Your converter and declared body type
should be prepared to handle this code should the server return it.

I've got the same problem.
I am using retrofit2.3.0

OkHttpCall parseResponse

if (code == 204 || code == 205) {
  rawBody.close();
  return Response.success(null, rawResponse);
}

ExceptionCatchingRequestBody catchingBody = new ExceptionCatchingRequestBody(rawBody);
try {
  T body = serviceMethod.toResponse(catchingBody);
  return Response.success(body, rawResponse);
} catch (RuntimeException e) {
  // If the underlying source threw an exception, propagate that rather than indicating it was
  // a runtime exception.
  catchingBody.throwIfCaught();
  throw e;
}

my converter

@Override
public T convert(ResponseBody value) throws IOException {
    JsonReader jsonReader = gson.newJsonReader(value.charStream());
    try {
        T result = adapter.read(jsonReader);
        if (result == null && type != Void.class) {
            throw new RuntimeException("response cannot be null");
        }
        return result;
    } finally {
        value.close();
    }
}

some callback

                @Override
                public void onSuccess(List<Model> lst) {
                    if (list!=null) //here is the crash point, so many pointer if 
                    {
                    }
                }

thinking about the following case
api request some data, server return 204 or 205,client parseResponse hit 204 or 205 branch, onSuccess is called, my onSuccess get a null reference crash. my converter doesn't have chance to handle the status, never get the 204 or 205 RuntimeException("response cannot be null").
I think this is a bug.

@JakeWharton
Copy link
Member

If you have the same problem then perhaps you can write a failing test case for it?

@umeshpatel046
Copy link
Author

umeshpatel046 commented Oct 31, 2017 via email

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

No branches or pull requests

3 participants