26

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.

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);
                    }
                });
            }
Waldmann
  • 1,563
  • 12
  • 25
Jack BeNimble
  • 35,733
  • 41
  • 130
  • 213

3 Answers3

57

call.request().url(), where call is type of retrofit2.Call.

Tunaki
  • 132,869
  • 46
  • 340
  • 423
10

RetrofitError has a getUrl() method that returns the URL.

Also the Response has a getUrl() method as well within the callback.

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.

BASIC
Log only the request method and URL and the response status code and execution time.
Community
  • 1
  • 1
EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428
6

Yes, you can enable debug logging by calling setLogLevel() on your RestAdapter.

I typically set logging to LogLevel.FULL for debug builds like so:

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.

Bryan Herbst
  • 66,602
  • 10
  • 133
  • 120