改造@GET - 如何显示请求字符串?
Retrofit @GET - how to display request string?
我正在开发一个 Android 应用程序,该应用程序使用 Retrofit 创建 restful 客户端。为了调试网络调用,我想显示或转储实际被调用的 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
}
使用生成的客户端的服务:
// 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);
}
stopTask();
}
@Override
public void failure(RetrofitError error) {
handleFailure(new CostFailedEvent(), null);
}
});
}
RetrofitError
有一个 getUrl()
方法 returns URL.
此外 Response
在回调中也有一个 getUrl()
方法。
那个,你也可以按照this question:
指定日志级别
RestAdapter adapter = (new RestAdapter.Builder()).
//...
setLogLevel(LogLevel.FULL).setLog(new AndroidLog("YOUR_LOG_TAG"))
尽管基于 docs,LogLevel.BASIC
应该可以满足您的需求。
BASIC
Log only the request method and URL and the response status code and execution time.
是的,您可以通过在 RestAdapter 上调用 setLogLevel()
来启用调试日志记录。
对于调试版本,我通常将日志记录设置为 LogLevel.FULL
,如下所示:
RestAdapter adapter = builder.setEndpoint("example.com")
.setLogLevel(BuildConfig.DEBUG ? RestAdapter.LogLevel.FULL : RestAdapter.LogLevel.NONE)
.build();
这将自动打印出与您的 HTTP 请求相关的所有信息,包括您正在点击的 URL、headers 以及请求和请求的 body响应。
call.request().url()
,其中 call
是 retrofit2.Call
.
的类型
我正在开发一个 Android 应用程序,该应用程序使用 Retrofit 创建 restful 客户端。为了调试网络调用,我想显示或转储实际被调用的 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
}
使用生成的客户端的服务:
// 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);
}
stopTask();
}
@Override
public void failure(RetrofitError error) {
handleFailure(new CostFailedEvent(), null);
}
});
}
RetrofitError
有一个 getUrl()
方法 returns URL.
此外 Response
在回调中也有一个 getUrl()
方法。
那个,你也可以按照this question:
指定日志级别RestAdapter adapter = (new RestAdapter.Builder()).
//...
setLogLevel(LogLevel.FULL).setLog(new AndroidLog("YOUR_LOG_TAG"))
尽管基于 docs,LogLevel.BASIC
应该可以满足您的需求。
BASIC
Log only the request method and URL and the response status code and execution time.
是的,您可以通过在 RestAdapter 上调用 setLogLevel()
来启用调试日志记录。
对于调试版本,我通常将日志记录设置为 LogLevel.FULL
,如下所示:
RestAdapter adapter = builder.setEndpoint("example.com")
.setLogLevel(BuildConfig.DEBUG ? RestAdapter.LogLevel.FULL : RestAdapter.LogLevel.NONE)
.build();
这将自动打印出与您的 HTTP 请求相关的所有信息,包括您正在点击的 URL、headers 以及请求和请求的 body响应。
call.request().url()
,其中 call
是 retrofit2.Call
.