Upgrade: add runtime parameter to interface?

I would like to always add a parameter to my re-settings. For values ​​that I can hard code, I can just use

@POST("/myApi?myParam=myValue") 

but what if i want to add android.os.Build.MODEL ?

 @POST("/myApi?machineName="+ Build.MODEL) 

does not work. It would be useful to be able to distract this part of the network call from the implementing code.

EDIT

I can add Build.MODEL to all my api calls using RequestInterceptor . However, it still eludes me, how to add it selectively only to some of my api calls, but it uses the same RestAdapter .

EDIT 2

Fixed header that was incorrect.

EDIT 3

Current implementation:

 RestAdapter restAdapter = new RestAdapter.Builder() .setEndpoint("myapi") .setRequestInterceptor(new RequestInterceptor() { @Override public void intercept(RequestInterceptor.RequestFacade request) { request.addQueryParam("machineName", Build.MODEL); } }) .build(); API_SERVICE = restAdapter.create(ApiService.class); 
+5
source share
1 answer

Build.MODEL not available for use in annotations because it cannot be resolved at compile time. It is available only at runtime (because it is loaded from a property).

There are two ways to do this. The first uses the RequestInterceptor that you specify in the question.

The second uses the @Query parameter for the method.

 @POST("/myApi") Response doSomething(@Query("machineName") String machineName); 

This requires you to pass Build.MODEL when calling the API. If you want, you can wrap the Retrofit interface in an API that will be more application-friendly, which will do it for you.

+7
source

Source: https://habr.com/ru/post/1201771/


All Articles