Retrofit dynamic HTTP methods

百般思念 提交于 2021-01-28 13:48:31

问题


I'm planning to replace Apache HTTP client with retrofit in my project. The problem I'm facing is that retrofit didn't(I couldn't find) support setting HTTP method to request at runtime. In my Web Service I don't know what HTTP method to call in advance, so annotations @GET, @POST, ... are useless.


回答1:


Retrofit is not designed for dynamic url requests. You configure retrofit with your API base URL then make specific requests.

For a more flexible dynamic option use out OkHttp. It is the HTTP Client used by Retrofit and it easy to work with.




回答2:


You can use Retrofit 2 for dynamic URL request with the new @Url annotation:

public interface CarService {  
    @GET
    public Call<ImageResponse> getPicture(@Url String url);
}

Then just also create @POST, @PUT etc. You are going to have to make the choice somewhere.



来源:https://stackoverflow.com/questions/37544722/retrofit-dynamic-http-methods

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!