Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrofit2 - response body null on success request

I'm having a problem with Retrofit2.

When I make a call to my webservice, even in case of success, the body of my response is null.

I tried to make the same call with RESTClient for Firefox and the body contains the correct answer.

This is my stack trace:

D/Call request: Request{method=POST, url=http://myNeo4jServer.com, tag=null}
D/Call request header: Authorization: Basic xxxxxx
                       Accept: application/json; charset=UTF-8
D/Response raw header: Server: nginx
                       Date: Tue, 17 May 2016 10:55:13 GMT
                       Content-Type: application/json
                       Content-Length: 21549
                       Connection: keep-alive
                       Access-Control-Allow-Origin: *
                       OkHttp-Sent-Millis: 1463482513167
                       OkHttp-Received-Millis: 1463482513354
D/Response raw: retrofit2.OkHttpCall$NoContentResponseBody@e6c7df
D/Response code: 200
D/Response body: null

As you can see:

  • Content-Length: 21549 (I think that counts also the characters in the body)

  • Response code: 200

  • Response body: null

What am I doing wrong?

Edit

My interface:

public interface PhonebookContactAPI {
    @Headers({
        "Authorization: Basic xxxxxx",
        "Accept: application/json; charset=UTF-8",
        "Content-Type: application/json"
    })
    @POST("/db/data/transaction/commit")
    Call<ResponseBody> get(@Body RequestBody body);
}

My request:

String json = "{\"statements\": [{\"statement\": \"MATCH (n) RETURN n\"}]}";

Retrofit client = new Retrofit.Builder()
        .baseUrl(Globals.BASE_CLIENT_URL)
        .addConverterFactory(GsonConverterFactory.create())
        .build();

PhonebookContactAPI service = client.create(PhonebookContactAPI.class);

RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), json);

Call<ResponseBody> call = service.get(body);
call.enqueue(new Callback<ResponseBody>() {
    @Override
    public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {

        Log.d("Call request", call.request().toString());
        Log.d("Call request header", call.request().headers().toString());

        Log.d("Response raw header", response.headers().toString());
        Log.d("Response raw", String.valueOf(response.raw().body()));
        Log.d("Response code", String.valueOf(response.code()));

        if(response.isSuccessful()) {
            Log.d("Response body", new Gson().toJson(response.body()));
        }

        else  {
            Log.d("Response errorBody", String.valueOf(response.errorBody()));
        }

    }

    @Override
    public void onFailure(Call<ResponseBody> call, Throwable t) {
        Log.d("onFailure", t.toString());
    }
});
like image 882
Lucas Puerari Avatar asked May 17 '16 11:05

Lucas Puerari


1 Answers

Since you are getting response which is not null (based on your latest command). I think you need to change your code as follows:

public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
        Log.d("Call request", call.request().toString());
        Log.d("Call request header", call.request().headers().toString());
        Log.d("Response raw header", response.headers().toString());
        Log.d("Response raw", String.valueOf(response.raw().body()));
        Log.d("Response code", String.valueOf(response.code()));

        if(response.isSuccessful()) {
            //the response-body is already parseable to your ResponseBody object
            ResponseBody responseBody = (ResponseBody) response.body();
            //you can do whatever with the response body now... 
            String responseBodyString= responseBody.string();
            Log.d("Response body", responseBodyString);
        }
        else  {
            Log.d("Response errorBody", String.valueOf(response.errorBody()));
        }
    }

Please give it a try and let me know if this helps.

like image 71
ishmaelMakitla Avatar answered Oct 04 '22 02:10

ishmaelMakitla