使用改造不同的域
Retrofit different domains using
我使用改装库进行 Android 网络操作。我有两个域地址。我使用 SharedPreferences 存储地址。如何使用不同的端点。
例如:
www.mehmet.com/api/list
ww.deniz.com/api/list
万维网......
www.....
代码:
public class ptsApi {
public static String BASE_URL = "";
private static ptsApi instance;
private ptsService ptsService;
private ptsApi(Context c) {
SharedPreferences sharedpreferences = c.getSharedPreferences("loginPref", Context.MODE_PRIVATE);
String BASE_URL = sharedpreferences.getString("api","").toString();
final Gson gson =
new GsonBuilder().
setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
.setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ")
.create();
final Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
ptsService = retrofit.create(ptsService.class);
}
public static ptsApi getInstance(Context c) {
if (instance == null) {
instance = new ptsApi(c);
}
return instance;
}
public Observable<SearchModel> getSearch(@NonNull String plate) {
return ptsService.searchData(plate);
}
使用类似
的东西
public void getRetrofit(Sting baseDomain){
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(baseDomain)
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
return retorfit;
}
你可以更改域并创建一个新的改造实例并相应地用于 api 调用
在您的代码中,您可以像上面一样传递域和上下文。
你可以试试这个。
public class ptsApi {
enum TYPE {
MEHMENT, DENIZ
}
private static ptsApi instance;
private ptsService ptsService;
private ptsApi(Context c, TYPE type) {
final String BASE_URL = getEndpoint(type);
final Gson gson =
new GsonBuilder().
setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
.setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ")
.create();
final Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
ptsService = retrofit.create(ptsService.class);
}
public static ptsApi getInstance(Context c, TYPE type) {
if (instance == null) {
instance = new ptsApi(c, type);
}
return instance;
}
public Observable<SearchModel> getSearch(@NonNull String plate) {
return ptsService.searchData(plate);
}
public String getEndpoint(TYPE type) {
SharedPreferences sharedpreferences = c.getSharedPreferences("loginPref", Context.MODE_PRIVATE);
switch (type) {
case MEHMENT:
return sharedpreferences.getString("mehmentApi","").toString();
case DENIZ:
return sharedpreferences.getString("denizApi","").toString();
default:
return "your default";
}
}
}
或者您可以创建一些方法,例如
public static ptsApi getMehmetInstance(Context c) {
if (instance == null) {
instance = new ptsApi(c, MEHMENT);
}
return instance;
}
public static ptsApi getDenizInstance(Context c) {
if (instance == null) {
instance = new ptsApi(c, DENIZ);
}
return instance;
}
这个怎么样?有一种方法可以在更改 baseUrl
时更新 ptsService
实例
public class ptsApi {
public static String BASE_URL = "";
private static ptsApi instance;
private ptsService ptsService;
// a function you can call to change the base url
// which in turn updates the ptsService instance
// to start making calls to the new baseUrl.
public void changeBaseUrl(String newBaseUrl) {
BASE_URL = newBaseUrl;
updatePtsService();
}
// Created this function to remove code duplication
private void updatePtsService() {
final Gson gson =
new GsonBuilder().
setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
.setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ")
.create();
final Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
ptsService = retrofit.create(ptsService.class);
}
private ptsApi(Context c) {
SharedPreferences sharedpreferences = c.getSharedPreferences("loginPref", Context.MODE_PRIVATE);
String BASE_URL = sharedpreferences.getString("api","").toString();
// re-using this to remove code duplication.
updatePtsService();
}
public static ptsApi getInstance(Context c) {
if (instance == null) {
instance = new ptsApi(c);
}
return instance;
}
public Observable<SearchModel> getSearch(@NonNull String plate) {
return ptsService.searchData(plate);
}
}
您可以使用一个带有 okhttp 拦截器的 Retrofit 实例,它允许运行时更改 URL 主机名:
import java.io.IOException;
import okhttp3.HttpUrl;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
/** An interceptor that allows runtime changes to the URL hostname. */
public final class HostSelectionInterceptor implements Interceptor {
private volatile String host;
public void setHost(String host) {
this.host = host;
}
@Override public okhttp3.Response intercept(Chain chain) throws IOException {
Request request = chain.request();
String host = this.host;
if (host != null) {
HttpUrl newUrl = request.url().newBuilder()
.host(host)
.build();
request = request.newBuilder()
.url(newUrl)
.build();
}
return chain.proceed(request);
}
public static void main(String[] args) throws Exception {
HostSelectionInterceptor interceptor = new HostSelectionInterceptor();
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.addInterceptor(interceptor)
.build();
Request request = new Request.Builder()
.url("http://www.coca-cola.com/robots.txt")
.build();
okhttp3.Call call1 = okHttpClient.newCall(request);
okhttp3.Response response1 = call1.execute();
System.out.println("RESPONSE FROM: " + response1.request().url());
System.out.println(response1.body().string());
interceptor.setHost("www.pepsi.com");
okhttp3.Call call2 = okHttpClient.newCall(request);
okhttp3.Response response2 = call2.execute();
System.out.println("RESPONSE FROM: " + response2.request().url());
System.out.println(response2.body().string());
}
}
了解更多信息:
我使用改装库进行 Android 网络操作。我有两个域地址。我使用 SharedPreferences 存储地址。如何使用不同的端点。
例如:
www.mehmet.com/api/list
ww.deniz.com/api/list
万维网......
www.....
代码:
public class ptsApi {
public static String BASE_URL = "";
private static ptsApi instance;
private ptsService ptsService;
private ptsApi(Context c) {
SharedPreferences sharedpreferences = c.getSharedPreferences("loginPref", Context.MODE_PRIVATE);
String BASE_URL = sharedpreferences.getString("api","").toString();
final Gson gson =
new GsonBuilder().
setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
.setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ")
.create();
final Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
ptsService = retrofit.create(ptsService.class);
}
public static ptsApi getInstance(Context c) {
if (instance == null) {
instance = new ptsApi(c);
}
return instance;
}
public Observable<SearchModel> getSearch(@NonNull String plate) {
return ptsService.searchData(plate);
}
使用类似
的东西public void getRetrofit(Sting baseDomain){
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(baseDomain)
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
return retorfit;
}
你可以更改域并创建一个新的改造实例并相应地用于 api 调用
在您的代码中,您可以像上面一样传递域和上下文。
你可以试试这个。
public class ptsApi {
enum TYPE {
MEHMENT, DENIZ
}
private static ptsApi instance;
private ptsService ptsService;
private ptsApi(Context c, TYPE type) {
final String BASE_URL = getEndpoint(type);
final Gson gson =
new GsonBuilder().
setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
.setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ")
.create();
final Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
ptsService = retrofit.create(ptsService.class);
}
public static ptsApi getInstance(Context c, TYPE type) {
if (instance == null) {
instance = new ptsApi(c, type);
}
return instance;
}
public Observable<SearchModel> getSearch(@NonNull String plate) {
return ptsService.searchData(plate);
}
public String getEndpoint(TYPE type) {
SharedPreferences sharedpreferences = c.getSharedPreferences("loginPref", Context.MODE_PRIVATE);
switch (type) {
case MEHMENT:
return sharedpreferences.getString("mehmentApi","").toString();
case DENIZ:
return sharedpreferences.getString("denizApi","").toString();
default:
return "your default";
}
}
}
或者您可以创建一些方法,例如
public static ptsApi getMehmetInstance(Context c) {
if (instance == null) {
instance = new ptsApi(c, MEHMENT);
}
return instance;
}
public static ptsApi getDenizInstance(Context c) {
if (instance == null) {
instance = new ptsApi(c, DENIZ);
}
return instance;
}
这个怎么样?有一种方法可以在更改 baseUrl
时更新ptsService
实例
public class ptsApi {
public static String BASE_URL = "";
private static ptsApi instance;
private ptsService ptsService;
// a function you can call to change the base url
// which in turn updates the ptsService instance
// to start making calls to the new baseUrl.
public void changeBaseUrl(String newBaseUrl) {
BASE_URL = newBaseUrl;
updatePtsService();
}
// Created this function to remove code duplication
private void updatePtsService() {
final Gson gson =
new GsonBuilder().
setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
.setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ")
.create();
final Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
ptsService = retrofit.create(ptsService.class);
}
private ptsApi(Context c) {
SharedPreferences sharedpreferences = c.getSharedPreferences("loginPref", Context.MODE_PRIVATE);
String BASE_URL = sharedpreferences.getString("api","").toString();
// re-using this to remove code duplication.
updatePtsService();
}
public static ptsApi getInstance(Context c) {
if (instance == null) {
instance = new ptsApi(c);
}
return instance;
}
public Observable<SearchModel> getSearch(@NonNull String plate) {
return ptsService.searchData(plate);
}
}
您可以使用一个带有 okhttp 拦截器的 Retrofit 实例,它允许运行时更改 URL 主机名:
import java.io.IOException;
import okhttp3.HttpUrl;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
/** An interceptor that allows runtime changes to the URL hostname. */
public final class HostSelectionInterceptor implements Interceptor {
private volatile String host;
public void setHost(String host) {
this.host = host;
}
@Override public okhttp3.Response intercept(Chain chain) throws IOException {
Request request = chain.request();
String host = this.host;
if (host != null) {
HttpUrl newUrl = request.url().newBuilder()
.host(host)
.build();
request = request.newBuilder()
.url(newUrl)
.build();
}
return chain.proceed(request);
}
public static void main(String[] args) throws Exception {
HostSelectionInterceptor interceptor = new HostSelectionInterceptor();
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.addInterceptor(interceptor)
.build();
Request request = new Request.Builder()
.url("http://www.coca-cola.com/robots.txt")
.build();
okhttp3.Call call1 = okHttpClient.newCall(request);
okhttp3.Response response1 = call1.execute();
System.out.println("RESPONSE FROM: " + response1.request().url());
System.out.println(response1.body().string());
interceptor.setHost("www.pepsi.com");
okhttp3.Call call2 = okHttpClient.newCall(request);
okhttp3.Response response2 = call2.execute();
System.out.println("RESPONSE FROM: " + response2.request().url());
System.out.println(response2.body().string());
}
}
了解更多信息: