Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrofit GET without a value in Android

I use Retrofit for most of my calls but in one of the cases, I have the full path provided in arguments. My URL is like this http://www.example.com/android.json. This URL is provided in full so I have to path it at runtime. I implement endpoint as suggested here https://medium.com/@kevintcoughlin/dynamic-endpoints-with-retrofit-a1f4229f4a8d but in the @GET I need to be able to put @GET(""). This does not work as I get an error saying I should provide at least one "/".

If I add the slash the URL becomes http://www.example.com/android.json/ and it does not work, the server returns forbidden. I also tried creating a custom GET interface similar to here https://github.com/square/retrofit/issues/458 but with GET and without providing a value method in the interface. Then I get another error saying value is missing.

Basically I need to be able to provide an empty or null value but retrofit does not allow that. How could I solve this problem? For now I am doing the JSON request manually but is there a way I could use retrofit for this case? I need to pass the full URL there is no way I can do endpoint http://www.example.com and @GET("/android.json"). Thanks

like image 490
vallllll Avatar asked Aug 12 '15 10:08

vallllll


2 Answers

You can use @GET(".") to indicate that your url is the same as the base url.

@GET(".")
Observable<Result> getData(@Query("param") String parameter);
like image 163
Dawit Abraham Avatar answered Oct 04 '22 21:10

Dawit Abraham


I've tried this approach, however didn't work for me.

Workaround for this issue is:

//Retrofit interface
public interface TestResourceClient {
    @GET
    Observable<Something> getSomething(@Url String anEmptyString);
}

//client call
Retrofit.Builder().baseUrl("absolute URL").build()
.create(TestResourceClient.class).getSomething("");

The downside of this solution is that you have to supply empty string in getSomething("") method call.

like image 41
Tomas Bartalos Avatar answered Oct 04 '22 22:10

Tomas Bartalos