1

I'm getting this error:

Expected BEGIN_OBJECT but was STRING at line 1 column 1 path

In another stackoverflow question, they say I am receiving an array when it should be an object. But my intuition is that I am receiving nothing, but I'm not able to see my json to debug.

I'm trying to log my received json to see what's happening, because I don't see the error in my code.

I coded is mentioned here:

OkHttpClient client = new OkHttpClient();

    client.interceptors().add(new Interceptor() {
        @Override
        public com.squareup.okhttp.Response intercept(Chain chain) throws IOException {
            Request request = chain.request();
            Buffer buffer = new Buffer();
            request.body().writeTo(buffer);
            String body = buffer.readUtf8();
            Request.Builder builder = chain.request().newBuilder();
            String credential = Credentials.basic(user, pass);
            builder.addHeader("Authorization", credential).build();
            Response response = chain.proceed(builder.build());
            return response;
        }
    });
    return client;

But I get a NullPointerException in request.body()...

What's my next step for debugging???

Community
  • 1
  • 1
Juliatzin
  • 18,455
  • 40
  • 166
  • 325
  • I would simply run the request through a proxy? Something like Charles would do the trick. – MH. Oct 01 '15 at 19:31
  • But communication is between Cel and Server. My webserver works well in my computer. So where do I install charles? – Juliatzin Oct 01 '15 at 20:57

2 Answers2

0

I would setup the retrofit interceptor with a log statement like the following:

Request request = chain.request();
Log.e(String.format("Request: %s", request.body().toString()));

That should give you a error log with the json body.

Ray Hunter
  • 15,137
  • 5
  • 53
  • 51
  • but request.body() is null! – Juliatzin Oct 01 '15 at 20:21
  • So you are not getting anything back from the server then as a response or are you getting a response code other than 200? – Ray Hunter Oct 01 '15 at 20:22
  • code 200, and my webservice is working when I test it with Postman – Juliatzin Oct 01 '15 at 20:42
  • You could run it through a proxy like MH suggested above and see if you are getting the body of the request set from your web service. Charles would be a great tool to use for that. Are there any headers that you might be missing that might return the json you are looking for? – Ray Hunter Oct 01 '15 at 20:49
  • But communication is between Cel and Server. My webserver works well in my computer. So where do I install charles? – Juliatzin Oct 01 '15 at 20:57
  • You would install Charles on your computer and then setup your phone to talk to charles on your computer. Then you can see the traffic between your device and the server. This would allow you to see what the web server is sending back. It sounds like postman is getting different data than your device; so that seems to be the issue. – Ray Hunter Oct 02 '15 at 14:34
0

You are modifying your request, but it sounds like you are trying to log the server response. To do that, you need to call chain.proceed() to get the response, and then log the body of that.

client.interceptors().add(new Interceptor() {
    @Override
    public com.squareup.okhttp.Response intercept(Chain chain) throws IOException {
        com.squareup.okhttp.Response response = chain.proceed(chain.request());
        String content = response.body().string();
        Log.d("interceptor", "intercepted res = " + content);
        return response.newBuilder()
                .body(ResponseBody.create(response.body().contentType(), content))
                .build();
    }
});
iagreen
  • 31,470
  • 8
  • 76
  • 90