android可编辑Baseurl改造
如何使此基本url动态(主机名值+协议值+端口值)这些值是我在其他活动中使用的值,我喜欢在此类ApiClient中使用它们android可编辑Baseurl改造,android,api,retrofit,Android,Api,Retrofit,如何使此基本url动态(主机名值+协议值+端口值)这些值是我在其他活动中使用的值,我喜欢在此类ApiClient中使用它们 private static Retrofit getRetrofit(){ HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor(); httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.
private static Retrofit getRetrofit(){
HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient okHttpClient = new OkHttpClient.Builder().addInterceptor(httpLoggingInterceptor).build();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://192.168.111.10:80/")
.addConverterFactory(GsonConverterFactory.create())
.client(okHttpClient)
.build();
return retrofit;
}
我喜欢在apiclient类中这样做,因为我有很多调用api,我也有同样的问题,经过大量研究,这是我找到的唯一解决方案。当你调用这个api时,你能给我看代码吗?你是怎么做到的?因为我在.build()上有erreur,当我试图像这样声明我的路由时,我在我的答案中添加了代码,about baseUrl是一个简单的字符串。我记得我遇到了一些奇怪的问题,但我记不清我到底遇到了什么,你们在控制台中遇到了一些错误吗?还有一种肮脏的方法可以是拦截器,为每个请求替换基本url
@GET("{baseUrl}/...")
Call<ObjectDto> getCall(@Path(value = "baseUrl", encoded = true) String baseUrl, @Query("param") String param);
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient.Builder httpClient = new OkHttpClient.Builder()
.readTimeout(60, TimeUnit.SECONDS).connectTimeout(60, TimeUnit.SECONDS);
httpClient.addInterceptor(logging);
Retrofit retrofit =
new Retrofit.Builder().baseUrl("http://useless.it/").client(httpClient.build())
.addConverterFactory(JacksonConverterFactory.create()).build();
retrofitApi = retrofit.create(RetrofitApi.class);