10

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 on the web browser:

  1. type "http://foobar.com/"
  2. Hit enter
  3. it becomes "http://foobar2.com/abc=123"

Now the question is how do I get that "abc=123" value from retrofit response. Thanks heaps!

7 Answers 7

24

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) { } }); 
Sign up to request clarification or add additional context in comments.

1 Comment

response.raw().request().url(); is not giving me the query parameters "abc=123" of op's question. It's just giving me the full URL
6

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; } } } } 

1 Comment

response.raw().request().url().toString()
1

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()); 

Comments

0

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*/))) 

1 Comment

Hi, getUrl() returns the original url, not the one that is redirected to.
0

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.

1 Comment

Hi, I had traced the network activity on the browser, the first response was a 302 and retrofit wasn't able to catch it for some reason, perhaps I will just wait for the future updates. Thanks anyway.
0

when using okhttp
okhttp-logging-interceptor
In Logcat use tag OkHttp

Comments

-1

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

worked for me.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.