改造@GET - 如何显示请求字符串?

Jac*_*ble 19 java android retrofit

我正在开发一个使用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);
                        }
                        stopTask();
                    }

                    @Override
                    public void failure(RetrofitError error) {
                        handleFailure(new CostFailedEvent(), null);
                    }
                });
            }
Run Code Online (Sandbox Code Playgroud)

小智 47

call.request().url(),哪种call类型retrofit2.Call.


Epi*_*rce 9

RetrofitError有一个getUrl()返回URL 的方法.

在回调中也Response有一个getUrl()方法.

那,你也可以根据这个问题指定日志级别:

RestAdapter adapter = (new RestAdapter.Builder()).
//...
           setLogLevel(LogLevel.FULL).setLog(new AndroidLog("YOUR_LOG_TAG"))              
Run Code Online (Sandbox Code Playgroud)

虽然基于文档,但LogLevel.BASIC应该做你需要的.

BASIC
Log only the request method and URL and the response status code and execution time.
Run Code Online (Sandbox Code Playgroud)


Tan*_*.7x 6

是的,您可以通过调用setLogLevel()RestAdapter 来启用调试日志记录.

我通常将日志记录设置LogLevel.FULL为调试版本,如下所示:

RestAdapter adapter = builder.setEndpoint("example.com")
    .setLogLevel(BuildConfig.DEBUG ? RestAdapter.LogLevel.FULL : RestAdapter.LogLevel.NONE)
    .build();
Run Code Online (Sandbox Code Playgroud)

这将自动打印出与您的HTTP请求相关的所有信息,包括您要访问的URL,标头以及请求和响应的正文.