Retrofit 2 运行时改变BaseUrl

(转载)http://www.jianshu.com/p/bd0632cf15e4

多API下的调试,如果每次都改变API然后去重新打包.apk,会比较痛苦,在调试过程中,如果要进行验证API是否成功,可以在运行时改变API达到运行一次验证各API。栗子API采用聚合数据(历史上的今天)(微信精选)

/*
 * Serice生成器
 */
public class ServiceGenerator {

    private static String BASE_URL = "http://api.juheapi.com/";

    private static Retrofit.Builder builder =
            new Retrofit.Builder()
                    .addConverterFactory(GsonConverterFactory.create())
                    .baseUrl(BASE_URL);

    public static <T> T createServiceFrom(Class<T> serviceClass) {
        return builder.build().create(serviceClass);
    }
}

在上述类中,加入改变BASE_URL的操作:

public static void changeApiBaseUrl(String newApiBaseUrl) {
    BASE_URL = newApiBaseUrl;

    builder = new Retrofit.Builder()
            .addConverterFactory(GsonConverterFactory.create())
            .baseUrl(BASE_URL);
}

调用栗子:

public void getToh(String key, String version, String month, String day, Callback<Toh> callBack) {
    mWeChatQuery = ServiceGenerator.createServiceFrom(WeChatQueryService.class);
    Call<Toh> call = mWeChatQuery.getToh(key,version,month, day);
    call.enqueue(callBack);
}

public void getWeChatQuery(String key, Callback<WeChatQuery> callback) {
    ServiceGenerator.changeApiBaseUrl(ApiAddressPool.API_JUHE_WECHAT);//改变API
    mWeChatQuery = ServiceGenerator.createServiceFrom(WeChatQueryService.class);
    Call<WeChatQuery> call = mWeChatQuery.getWeChatQuery(key, "", "", "");
    call.enqueue(callback);
}

运行后结果:(输出的两次BaseUrl)

I/System.out: http://api.juheapi.com/
I/System.out: http://v.juhe.cn/

总结:
好像没有什么卵用...也有可能我没理解Retrofit 2 — How to Change API Base Url at Runtime 的原意..



作者:河婆墟邓紫棋
链接:http://www.jianshu.com/p/bd0632cf15e4
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。



https://stackoverflow.com/questions/34907858/android-retrofit-how-to-override-baseurl

you can use the @Url annotation to provide the full complete url. E.G.

@GET
Call<GitHubUser> getUser(@Url String url);
share improve this answer
 
 
i am using 'com.squareup.retrofit:retrofit:1.8.0' will it work, nowo it is saying it cant find that annotation. i dont see it in the docs: static.javadoc.io/com.squareup.retrofit/retrofit/1.8.0/… –  j2emanue  Jan 20 '16 at 18:45 
1  
no it wont. I think it was added with retrofit 2. –  Blackbelt  Jan 20 '16 at 18:47
 
I am getting an exception: java.lang.IllegalArgumentException: \@Url cannot be used with \@GET URL (parameter #1) –  sativa  May 11 at 7:05
 
@sativa it is hard to say what and why without seeing how you are using it –  Blackbelt  May 11 at 7:20
 
Like this (It's kotlin not java) \@GET("api/1/route") fun route(\@Url url: String = "myurl.com"): Observable<Route> –  sativa  May 11 at 7:33

In retrofi2 the path in @GET overrides the base URL.

 @GET("https://someurl/api/supermarkets")
 Observable<List<TechIndex>> getTechIndexList();

The request will be send to "someurl/api/.." no matter what base url is. Hope it helps

share improve this answer
 
 
thats a good point, thanks –  j2emanue  Jul 7 at 11:20

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值