1

OK- so I'm a bit confused by this.

I've setup an OKHTTP3 POST. When I get the response I've been trying to put the body into a string (it's a string response) but something is going wonky:

try {
        Response response = client.newCall(request).execute();

        String rep = response.body().string();
        if(rep.length() > 0){
            Log.i(TAG, "Got Response");
            Log.i(TAG, rep);
        }

    }catch (Exception e){
        e.printStackTrace();
    }

I get a Log saying "Got Response" but (the length is 80) then it just stops. It doesn't say my rep string is null, or empty... It just never calls that second Log.

Anyone have any kind of idea what's up?

Jason Stewart
  • 123
  • 1
  • 8

3 Answers3

0

Try using this..

      OkHttpClient client = new OkHttpClient.Builder().build();          

      Request request = new Request.Builder()
            .url("")
            .build();

      client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                e.printStackTrace();
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                try {

                    Log.d("Response",response.body().string());

                    }
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    response.body().close();
                }
            }

        });
Shreyas Ponkshe
  • 116
  • 1
  • 7
  • Try to check whether request is sucessfull with`if(response.isSucessful()){ }` – Shreyas Ponkshe Apr 11 '16 at 16:25
  • I added isSuccessfull, and it indicates it was... I added: `Log.i("Got", "Response"); Log.d("Response", response.body().string()); Log.i("Should have Logged", "Response");` it calls the first log, the third log, then goes right to body.close. :-/ – Jason Stewart Apr 11 '16 at 16:30
  • Maybe it's how I'm formatting my response on the Servlet? `resp.setStatus(HttpServletResponse.SC_OK); PrintWriter out = resp.getWriter(); out.println(servingURL);` (servingURL is a string) – Jason Stewart Apr 11 '16 at 16:33
  • Do you get response when you access url through browser? – Shreyas Ponkshe Apr 11 '16 at 16:46
  • I'll have to pull something together to do that with. – Jason Stewart Apr 11 '16 at 16:52
0

Right... So apparently I just needed to put the part that gets the string in an AsyncTask, and it started working.

There wasn't any error message or anything indicating to do so, but I went back and actually looked at the documentation for the Callback, and it mentions consuming the body on another thread.

new SendPictureClass().execute(response);

Probably should have started there to begin with... ¯_(ツ)_/¯

Thanks for the help guys!

Jason Stewart
  • 123
  • 1
  • 8
0

I also met this problem, Log can't work.
And I found a temporary solution.
I try to use Toast to show the response, then I found that the response poped up, but with a lot of spaces(not sure if those characters are spaces).

So I am using Log.i(TAG, rep.trim());, which works well. I don't know if this solution is good for this problem, but it works for me after all.

KiBa1215
  • 87
  • 6