java 改造@GET - 如何显示请求字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30086356/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Retrofit @GET - how to display request string?
提问by Hyman BeNimble
I'm working on an Android application that uses Retrofit to create a restful client. In order to debug networks calls, I would like to display or dump the url that's actually being invoked. Is there a way to do this? I've included some code below which shows how the app currently using retrofit.
我正在开发一个使用 Retrofit 创建一个安静客户端的 Android 应用程序。为了调试网络调用,我想显示或转储实际调用的 url。有没有办法做到这一点?我在下面包含了一些代码,这些代码显示了应用程序当前如何使用改造。
Client interface definition:
客户端接口定义:
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
}
Service which uses generated client:
使用生成的客户端的服务:
// 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);
}
});
}
采纳答案by EpicPandaForce
RetrofitError
has a getUrl()
method that returns the URL.
RetrofitError
有一个getUrl()
返回 URL的方法。
Also the Response
has a getUrl()
method as well within the callback.
在回调中也Response
有一个getUrl()
方法。
That, and you can also specify the log level as per this question:
那,您还可以根据此问题指定日志级别:
RestAdapter adapter = (new RestAdapter.Builder()).
//...
setLogLevel(LogLevel.FULL).setLog(new AndroidLog("YOUR_LOG_TAG"))
Although based on the docs, LogLevel.BASIC
should do what you need.
虽然基于docs,LogLevel.BASIC
应该做你需要的。
BASIC
Log only the request method and URL and the response status code and execution time.
回答by Aragats Amirkhanyan
call.request().url()
, where call
is type of retrofit2.Call
.
call.request().url()
,call
的类型在哪里retrofit2.Call
。
回答by Bryan Herbst
Yes, you can enable debug logging by calling setLogLevel()
on your RestAdapter.
是的,您可以通过调用setLogLevel()
您的 RestAdapter来启用调试日志记录。
I typically set logging to LogLevel.FULL
for debug builds like so:
我通常将日志记录设置LogLevel.FULL
为调试版本,如下所示:
RestAdapter adapter = builder.setEndpoint("example.com")
.setLogLevel(BuildConfig.DEBUG ? RestAdapter.LogLevel.FULL : RestAdapter.LogLevel.NONE)
.build();
This will automatically print out all of the information associated with your HTTP requests, including the URL you are hitting, the headers, and the body of both the request and the response.
这将自动打印出与您的 HTTP 请求相关的所有信息,包括您点击的 URL、标头以及请求和响应的正文。