我正在尝试获取请求中发送的确切JSON.这是我的代码:
OkHttpClient client = new OkHttpClient();
client.interceptors().add(new Interceptor(){
@Override public com.squareup.okhttp.Response intercept(Chain chain) throws IOException {
Request request = chain.request();
Log.e(String.format("\nrequest:\n%s\nheaders:\n%s",
request.body().toString(), request.headers()));
com.squareup.okhttp.Response response = chain.proceed(request);
return response;
}
});
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(API_URL)
.addConverterFactory(GsonConverterFactory.create())
.client(client).build();
Run Code Online (Sandbox Code Playgroud)
但我只在日志中看到这个:
request:
com.squareup.okhttp.RequestBody$1@3ff4074d
headers:
Content-Type: application/vnd.ll.event.list+json
Run Code Online (Sandbox Code Playgroud)
我怎么做正确的记录,考虑到拆除setLog()和setLogLevel()我们使用了改造1使用哪个?
我正在开发一个使用Retrofit创建一个宁静客户端的Android应用程序.为了调试网络调用,我想显示或转储实际被调用的url.有没有办法做到这一点?我在下面列出了一些代码,其中显示了当前使用改造的应用程序.
客户端接口定义:
import retrofit.Callback;
import retrofit.http.Body;
import retrofit.http.GET;
import retrofit.http.Headers;
import retrofit.http.POST;
import retrofit.http.Path;
// etc...
public interface MyApiClient {
@Headers({
"Connection: close"
})
@GET("/{userId}/{itemId}/getCost.do")
public void get(@Path("userId") String userId, @Path("itemId") String userId, Callback<Score> callback);
//....etc
}
Run Code Online (Sandbox Code Playgroud)
使用生成的客户端的服务:
// etc...
import javax.inject.Inject;
import retrofit.Callback;
import retrofit.RetrofitError;
import retrofit.client.Response;
@Inject
MyApiClient myApiClient;
// etc...
myApiClient.getCost(myId, itemId, new Callback<Cost>() {
@Override
public void success(Cost cost, Response response) {
Log.d("Success: %s", String.valueOf(cost.cost));
if (cost.cost != -1) {
processFoundCost(cost);
} else {
processMissingCost(itemId);
} …Run Code Online (Sandbox Code Playgroud) 基本上,我的问题很简单。我使用改造作为与我无法控制的服务器进行通信的框架。我想在我的请求上设置某种标记,该标记会自动在响应中返回。关于如何实现这一点有什么想法吗?
Retrofit的先前版本使用RestAdapter并提供了启用日志的功能。为什么在Retrofit 2.0中删除了该功能?
要启用日志,我必须这样做。
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
/** Handles Log */
retrofit.client().interceptors().add(new LoggingInterceptor());
class LoggingInterceptor implements Interceptor {
@Override
public Response intercept(Interceptor.Chain chain) throws IOException {
Request request = chain.request();
long t1 = System.nanoTime();
Logger.d(String.format("Sending request %s on %s%n%s",
request.url(), chain.connection(), request.headers()));
Response response = chain.proceed(request);
long t2 = System.nanoTime();
Logger.d(String.format("Received response for %s in %.1fms%n%s",
response.request().url(), (t2 - t1) / 1e6d, response.headers()));
// Logger.d(""+new String(response.body().bytes()));
return response;
}
Run Code Online (Sandbox Code Playgroud)
这是唯一的解决方案?以前的规定非常方便...