How to get the response URL from Retrofit?

前端 未结 6 852
感情败类
感情败类 2021-01-05 15:48

I\'m using Retrofit for REST web services my android project. It works fine overall untill the point I\'m trying to read several parameters from the return URL. For exapmle

相关标签:
6条回答
  • 2021-01-05 16:07

    You can try to get this data from Retrofit Callback. In your Retrofit Service declare method with Callback.

    public interface YourService {
      @GET("/url")
      JsonElement get(Callback<JsonElement> cb);
    }
    
    RestAdapter adapter = new RestAdapter.Builder().setEndpoint("http://foobar.com").build();
    YourService service = adapter.create(YourService.class)
    

    And then handle response url in Callback

    service.createUser(new Callback<JsonElement>() {
        @Override
        public void success(JsonElement jsonElement, retrofit.client.Response response) {
            String url = response.getUrl();
            // handle url
        }
        @Override
        public void failure(RetrofitError error) {
        }
    });
    

    Try to use OkHttpClient. Add dependency in build.gradle and modify RestAdapter

    compile 'com.squareup.okhttp:okhttp:2.0.0'
    compile 'com.squareup.okhttp:okhttp-urlconnection:2.0.0'
    
    RestAdapter restAdapter = new RestAdapter.Builder()
       ...
    
       .setClient(new OkClient(new OkHttpClient().setFollowSslRedirects(false /* or try true*/)))
    
    0 讨论(0)
  • 2021-01-05 16:11

    I have solve the problem.Here is my solution:

    Define a request

    @GET 
    Call<String> getSend(@Url String url);
    

    and send

    Api.get(WXApi.class).getSend(ip).enqueue(new Callback<String>() {
    
        @Override
        public void onResponse(Call<String> call, Response<String> response) {
    
            Log.d(TAG,"response.raw().request().url();"+response.raw().request().url());}
    
        @Override
        public void onFailure(Call<String> call, Throwable t) {
    
        }
    });
    
    0 讨论(0)
  • 2021-01-05 16:13

    I didn't test it, but such redirects are sent via the HTTP-Header "Location:".

    You might be able to retrieve the Url with the following code:

    Callback<Void> cb = new Callback<Void>() {
        @Override
        public void success(Void aVoid, Response response) {
            String url = null;
            List<Header> headers = response.getHeaders();
            for (Header header : headers) {
                if (header.getName() == "Location") {
                    url = header.getValue();
                    break;
                }
            }
            // Do something with url
        }
    
        @Override
        public void failure(RetrofitError error) {
    
        }
    }
    

    One requirement is, that the HTTP client doesn't follow the redirect automatically, but I'm not that familiar with retrofit.

    0 讨论(0)
  • 2021-01-05 16:15

    First create a call then print. See example below:

    ApiInterface apiService = ApiClient.getClient().create(ApiInterface.class);
    Call<LoginResponse> call = apiService.getLoginData(phone_number.getText().toString());
    Log.e(" Login url", call.request().url().toString());
    
    0 讨论(0)
  • 2021-01-05 16:27

    Below is my way: (using retrofit2)

    • First: Create an instance of Interceptor:

      public class LoggingInterceptor implements Interceptor {
      
      private String requestUrl;
      
      public String getRequestUrl() {
          return requestUrl;
      }
      
      public void setRequestUrl(String requestUrl) {
          this.requestUrl = requestUrl;
      }
      
      @Override
      public Response intercept(Chain chain) throws IOException {
          Request request = chain.request();
          Response response = chain.proceed(request);
          setRequestUrl(response.request().url().toString());
          return response;
      
      }
      }
      
    • Add Interceptor to retrofit

      Retrofit retrofit = new Retrofit.Builder().baseUrl(baseUrl)
              .addConverterFactory(GsonConverterFactory.create())
              .build();
      retrofit.client().interceptors().add(new LoggingInterceptor());
      
    • Get url from onResponse() method

      @Override
      public void onResponse(Response<T> response, Retrofit retrofit) {
          List<Interceptor> data = retrofit.client().interceptors();
          if (data != null && data.size() > 0) {
              for (int i = 0; i < data.size(); i++) {
                  if (data.get(i) instanceof LoggingInterceptor) {
                      LoggingInterceptor intercept = (LoggingInterceptor) data.get(i);
                      String url = intercept.getRequestUrl();
                      // todo : do what's ever you want with url
                      break;
                  } else {
                      continue;
                  }
              }
          }
      
      }
      
    0 讨论(0)
  • 2021-01-05 16:30

    Response.raw().request().url().query();

    worked for me.

    0 讨论(0)
提交回复
热议问题