如何在logcat中打印requestBody

时间:2018-12-17 06:18:11

标签: android android-studio request okhttp okhttp3

我正在使用okhttp3分段上传图片。我正在requestBody中发送参数,但是我不明白如何在logcat中打印所有参数。请帮助

requestBody = new MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                .addFormDataPart("image_file_name", filename + ".jpg"+",", fileBody)
                .addFormDataPart("name", edtName.getText().toString())
                .addFormDataPart("type", "P")
                .addFormDataPart("password", edtPwd.getText().toString())
                .addFormDataPart("c_password", edtConfrmPwd.getText().toString())
                .addFormDataPart("cloud_token", "AND")
                .addFormDataPart("gender", "M")
                .addFormDataPart("postal_code", edtPostCode.getText().toString())
                .addFormDataPart("address", edtAddress.getText().toString())
                .addFormDataPart("image_encode_string", encodeImage)
                .addFormDataPart("fcm_id", "11212")
                .addFormDataPart("email", edtEmail.getText().toString())
                .addFormDataPart("phone", edtPhone.getText().toString())
                .build();

        Log.e("TAG", "requestBody: "+requestBody.toString());

这里

 Log.e("TAG", "requestBody: "+requestBody.toString());

打印

  

okhttp3.MultipartBody@84d3c7f

并且不打印所有参数。

1 个答案:

答案 0 :(得分:1)

对于打印requestBody,您需要通过改进使用Logging Interceptor,它将在logcat中打印与请求相关的日志。

像这样更改改造对象->

private static final Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(BASE_URL)
            .client(new OkHttpClient().newBuilder()
                    .addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
                    .readTimeout(READ_TIMEOUT_SECONDS, TimeUnit.SECONDS)
                    .writeTimeout(WRITE_TIMEOUT_SECONDS, TimeUnit.SECONDS)
                    .connectTimeout(CONNECTION_TIMEOUT_SECONDS, TimeUnit.SECONDS)
                    .build())
            .addConverterFactory(GsonConverterFactory.create())
            .build();

并在gradle中添加日志记录拦截器->

compile 'com.squareup.okhttp3:logging-interceptor:3.12.0'

希望这会有所帮助。