【问题标题】:how to print requestBody in logcat如何在 logcat 中打印 requestBody
【发布时间】:2019-05-17 12:02:06
【问题描述】:

我正在使用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

并且不打印所有参数。

【问题讨论】:

标签: android android-studio request okhttp okhttp3


【解决方案1】:

打印requestBody,需要通过retrofit使用Logging Interceptor,它会打印logcat中与request相关的日志。

像这样改变你的改造对象 ->

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'

希望这会有所帮助。

【讨论】:

  • @mishti,通过这样做,您甚至不需要使用 log.e,因为它将所有请求详细信息打印为 OkHttp 标记。
  • @我应该在哪里使用它
  • 当你创建改造对象时,只需添加这个 .addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-27
  • 1970-01-01
相关资源
最近更新 更多