Joo*_*lah 40 java android http android-activity retrofit
我现在从Volley转到Retrofit 2.0版本.
如何打印完整的json响应代码?
包括:
compile 'com.squareup.retrofit:converter-gson:2.0.0-beta2'
compile 'com.squareup.retrofit:retrofit:2.0.0-beta2'
Run Code Online (Sandbox Code Playgroud)
RestClient:
OkHttpClient client = new OkHttpClient();
client.interceptors().add(new Interceptor() {
@Override
public Response intercept(Interceptor.Chain chain) throws IOException {
Response response = chain.proceed(chain.request());
return response;
}
});
Gson gson = new GsonBuilder()
.setDateFormat("yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'SSS'Z'")
.create();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(ROOT)
.addConverterFactory(GsonConverterFactory.create(gson))
.client(client)
.build();
REST_CLIENT = retrofit.create(APIService.class);
Run Code Online (Sandbox Code Playgroud)
APIService:
@GET("my/json")
Call<Model> getFeed();
Run Code Online (Sandbox Code Playgroud)
在活动中 - 调用API:
Call<Model> call = RestClient.get().getFeed();
call.enqueue(new Callback<Model>() {
@Override
public void onResponse(Response<Model> response, Retrofit retrofit) {
Log.w("2.0 getFeed > response.raw() => ", response.raw().toString());//DONT WORK
Log.w("2.0 getFeed > retrofit => ", retrofit.toString());//DONT WORK
Log.w("2.0 getFeed > body => ", response.body().toString()); //DONT WORK
Log.w("2.0 getFeed > getStatus => ", response.body().getStatus());
}
@Override
public void onFailure(Throwable t) {
t.printStackTrace();
Log.e("2.0 getFeed > onFailure => ", t.toString());
}
});
Run Code Online (Sandbox Code Playgroud)
Mah*_*raa 53
要在json中打印完整的响应:
Log.w("2.0 getFeed > Full json res wrapped in gson => ",new Gson().toJson(response));
Run Code Online (Sandbox Code Playgroud)
如果您想拥有漂亮的打印功能,请使用:
Log.w("2.0 getFeed > Full json res wrapped in pretty printed gson => ",new GsonBuilder().setPrettyPrinting().create().toJson(response));
Run Code Online (Sandbox Code Playgroud)
请注意,这会打印反序列化的数据(而不是从服务器返回的Raw响应).要获得原始响应,您可以使用以下方法之一:
HttpLoggingInterceptor请参阅:https://stackoverflow.com/a/33256827/2267723或拥有自己的拦截器版本Stetho.见:http://facebook.github.io/stetho/或Charles Web Debugging Proxy.请参阅:https://www.charlesproxy.comDGN*_*DGN 18
像这样插入以下拦截器类
OkHttpClient client = new OkHttpClient();
client.interceptors().add(new LoggingInterceptor());
Run Code Online (Sandbox Code Playgroud)
//////拦截器类
public static class LoggingInterceptor implements Interceptor {
@Override
public com.squareup.okhttp.Response intercept(Chain chain) throws IOException {
Log.i("LoggingInterceptor","inside intercept callback");
Request request = chain.request();
long t1 = System.nanoTime();
String requestLog = String.format("Sending request %s on %s%n%s",
request.url(), chain.connection(), request.headers());
if(request.method().compareToIgnoreCase("post")==0){
requestLog ="\n"+requestLog+"\n"+bodyToString(request);
}
Log.d("TAG","request"+"\n"+requestLog);
com.squareup.okhttp.Response response = chain.proceed(request);
long t2 = System.nanoTime();
String responseLog = String.format("Received response for %s in %.1fms%n%s",
response.request().url(), (t2 - t1) / 1e6d, response.headers());
String bodyString = response.body().string();
Log.d("TAG","response only"+"\n"+bodyString);
Log.d("TAG","response"+"\n"+responseLog+"\n"+bodyString);
return response.newBuilder()
.body(ResponseBody.create(response.body().contentType(), bodyString))
.build();
}
public static String bodyToString(final Request request) {
try {
final Request copy = request.newBuilder().build();
final Buffer buffer = new Buffer();
copy.body().writeTo(buffer);
return buffer.readUtf8();
} catch (final IOException e) {
return "did not work";
}
}`
Run Code Online (Sandbox Code Playgroud)
礼貌:https://github.com/square/retrofit/issues/1072#
ald*_*dok 18
实际上Square已经为此创建了一个类,只需添加即可
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder()
.addInterceptor(interceptor).build();
Run Code Online (Sandbox Code Playgroud)
而且,在Retrofit中
Retrofit retrofit = new Retrofit.Builder()
.client(client)
.baseUrl("https://yourapi.com/api/")
.build();
Run Code Online (Sandbox Code Playgroud)
拦截器类位于maven中心
compile 'com.squareup.okhttp3:logging-interceptor:3.5.0'
Run Code Online (Sandbox Code Playgroud)
您可以在HttpLoggingInterceptor类中设置日志记录级别.BODY是冗长的(它将所有内容打印到Body).有关更多信息,请访问OkHttp github
警告!
不要忘记在生产中删除拦截器(或将记录级别更改为NONE)!否则,人们将能够在Log Cat上看到您的请求和响应.
要在Retrofit2.3.0中获得Json的完整响应,请使用以下内容。
这对我有用。
call.enqueue(new Callback<someList>() {
@Override
public void onResponse(Call<someList> call, Response<someList> response) {
if (response.isSuccessful())
Log.e("Success", new Gson().toJson(response.body()));
else
Log.e("unSuccess", new Gson().toJson(response.errorBody()));
}
@Override
public void onFailure(Call<someList> call, Throwable t) {
Log.e("onFailure", t.toString());
}
});
Run Code Online (Sandbox Code Playgroud)
您可以像下面一样setLogLevel访问您的Retrofit适配器,并查看响应和其他数据,例如标头、响应代码与响应代码。
setLogLevel(LogLevel.FULL)
Run Code Online (Sandbox Code Playgroud)
如果您使用的是 Retrofit 版本 2+,则必须设置OkHttpLoggingInterceptor为查看日志。
首先添加OkHttpLoggingInterceptor到您的项目中:
com.squareup.okhttp3:logging-interceptor:${Versions.okHttpLoggingInterceptorVersion}
Run Code Online (Sandbox Code Playgroud)
然后创建 init 你的拦截器:
HttpLoggingInterceptor().apply { level = HttpLoggingInterceptor.Level.BODY }
Run Code Online (Sandbox Code Playgroud)
最后将其添加到您的 OkHttpClient
with(OkHttpClient.Builder()) {
if (BuildConfig.DEBUG) addInterceptor(loggingInterceptor)
build()
}
Run Code Online (Sandbox Code Playgroud)