How to get JSON values that OKhttp generated?

-1

This is my code

public void loginRequestAsync() {

    OkHttpClient client = new OkHttpClient();

    HttpUrl.Builder urlBuilder = HttpUrl.parse("xxx.xxxx.xx.x.xx.xxx.x").newBuilder();
    String url = urlBuilder.build().toString();
    RequestBody requestBody = new MultipartBody.Builder()
            .setType(MultipartBody.FORM)
            .addFormDataPart("ddi", String.valueOf(ddi.getText()))
            .addFormDataPart("numero", String.valueOf(numero.getText()))
            .build();

    Request request = new Request.Builder()
            .url(url)
            .post(requestBody)
            .build();

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

        }

        @Override
        public void onResponse(Call call, Response response) throws IOException {
            Log.d("Response: ",response.body().string());

        }
    });
}
    
asked by anonymous 21.09.2017 / 15:48

1 answer

0

You can use the HttpLoggingInterceptor . After adding the library to your project, just add it as a Interceptor to your OkHttpClient :

HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(Level.BODY);
OkHttpClient client = new OkHttpClient.Builder()
  .addInterceptor(logging)
  .build();

When using Level.BODY , the body of requests and responses will appear in your log. Output example:

--> POST /greeting http/1.1
Host: example.com
Content-Type: plain/text
Content-Length: 3

Hi?
--> END POST

<-- 200 OK (22ms)
Content-Type: plain/text
Content-Length: 6

Hello!
<-- END HTTP

This tool should be used with caution, since it can expose sensitive data.

    
21.09.2017 / 20:48