大佬教程收集整理的这篇文章主要介绍了android – 使用Retrofit 2.0 POST方法获取请求正文内容,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
httpLoggingInterceptor logging = new httpLoggingInterceptor(); logging.setLevel(Level.bODY); OkhttpClient httpClient = new OkhttpClient(); httpClient.interceptors().add(logging); Retrofit retrofit = new Retrofit.builder() .baseUrl(baseURL) .addConverterFactory(GsonConverterFactory.create()) .build(); return retrofit.create(apiClass);
>请求体只能在默认的Android Logcat上看到@H_502_5@
02-04 01:35:59.235 5007-5035 / com.example.retrofitdemo D / Okhttp:{“nameValuePairs”:{“id”:“1”,“name”:“chandru”,“type”:“user” }}@H_502_5@
但是上述方法只返回logcat中的响应体,我无法将其作为String或JSONObject.尽管如果我能够使用httpLoggingInterceptor获取响应体,我的请求主体将在应用程序进入生产之后始终显示在Logcat中,标签为“Okhttp”(所以主要是这样一种缓解在logcat中发布数据).@H_502_5@
我的要求:@H_502_5@
我需要获取请求正文为String或JSONObject或任何方法,而不会修改Logcat中的post数据.@H_502_5@
即使在Response< T>的onResponse之后,我也试图获取请求主体回应,但尽管我无法做到这一点.请查看我为此目的使用的代码如下:@H_502_5@
Gson gson = new Gson(); String responseString = gson.toJson(response.raw().request().body());
注意:上述转换的请求体使用gson.toJson方法仅返回元数据而不是请求发布数据.@H_502_5@
请通过提供宝贵的技巧和解决方案帮助我.我不知道该怎么做过去两天我完全陷入僵局.如果我的问题太冗长,请原谅您的意见总是受到欢迎.如果我的要求不明确,请告诉我提前致谢.@H_502_5@
private String bodyToString(final requestBody request) { try { final requestBody copy = request; final Buffer buffer = new Buffer(); if (copy != null) copy.writeTo(buffer); else return ""; return buffer.readUtf8(); } catch (final IOException E) { return "did not work"; } }
我正在使用的改装依赖@H_502_5@
compile 'com.squareup.retrofit2:converter-gson:2.0.0-beta3' compile 'com.squareup.okhttp3:okhttp:3.0.0-RC1' compile 'com.squareup.okhttp3:logging-interceptor:3.0.0-RC1' compile 'com.squareup.retrofit2:retrofit:2.0.0-beta3'
以上是大佬教程为你收集整理的android – 使用Retrofit 2.0 POST方法获取请求正文内容全部内容,希望文章能够帮你解决android – 使用Retrofit 2.0 POST方法获取请求正文内容所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。