Retrofit2 - response body null on success request

时间:2016-05-17 11:18:28

标签: android retrofit2

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());
    }
});

2 个答案:

答案 0 :(得分:2)

因为您收到的响应非空(基于您的最新命令)。我认为您需要按如下方式更改代码:

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()));
        }
    }

请试一试,如果有帮助请告诉我。

答案 1 :(得分:0)

我发现的内容:可能您的API响应对象映射错误。我遇到了同样的问题,正确地重新映射了响应后,我的对象不再为null。