英文:
Change base url dynamically from spinner
问题
我在登录活动中有一个旋转器,其中包含URL列表。我想根据所选的URL动态更改基本URL。我该如何做呢?以下是我的Retrofit客户端:
public class RetrofitClient {
private static Retrofit retrofit;
private static final String BASE_URL="Dynamically_URL";
public static Retrofit getRetrofitInstance(){
if(retrofit==null){
OkHttpClient okHttpClient = new OkHttpClient().newBuilder()
.connectTimeout(25, TimeUnit.SECONDS)
.readTimeout(25, TimeUnit.SECONDS)
.writeTimeout(25, TimeUnit.SECONDS)
.build();
retrofit= new retrofit2.Retrofit.Builder()
.baseUrl(BASE_URL)
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
}
英文:
I've got spinner in my login activity with url list. I would like to change base url dynamically, based on selected url. How can i do it? Below is my Retrofit client:
public class RetrofitClient {
private static Retrofit retrofit;
private static final String BASE_URL="Dynamically_URL";
public static Retrofit getRetrofitInstance(){
if(retrofit==null){
OkHttpClient okHttpClient = new OkHttpClient().newBuilder()
.connectTimeout(25, TimeUnit.SECONDS)
.readTimeout(25, TimeUnit.SECONDS)
.writeTimeout(25, TimeUnit.SECONDS)
.build();
retrofit= new retrofit2.Retrofit.Builder()
.baseUrl(BASE_URL)
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
}
答案1
得分: 1
首先,将您选择的旋转器项目保存到共享首选项中。
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
SharedPreferences sharedPreferences = getSharedPreferences(Cons.PRIVATE_PREF, Context.MODE_PRIVATE);
sharedPreferences.edit().putString("url_selected", spinner.getSelectedItem().toString()).apply();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {}
});
然后,在您的 Retrofit 客户端上调用保存在共享首选项中的 URL。
public static Retrofit getRetrofitInstance(Context context) {
SharedPreferences sharedPreferences = context.getSharedPreferences(Cons.PRIVATE_PREF, Context.MODE_PRIVATE);
baseurl = sharedPreferences.getString("url_selected", "");
OkHttpClient okHttpClient = new OkHttpClient().newBuilder()
.connectTimeout(25, TimeUnit.SECONDS)
.readTimeout(25, TimeUnit.SECONDS)
.writeTimeout(25, TimeUnit.SECONDS)
.build();
retrofit = new retrofit2.Retrofit.Builder()
.baseUrl(baseurl)
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create())
.build();
return retrofit;
}
英文:
First save your selected spinner item to Shared Preferences
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
SharedPreferences sharedPreferences = getSharedPreferences(Cons.PRIVATE_PREF, Context.MODE_PRIVATE);
sharedPreferences.edit().putString("url_selected", spinner.getSelectedItem().toString()).apply();
}
@Override
public void onNothingSelected(AdapterView<?> parent){}
});
Then call saved url on shared Preferences on your retrofit Client
public static Retrofit getRetrofitInstance(Context context) {
SharedPreferences sharedPreferences = context.getSharedPreferences(Cons.PRIVATE_PREF, Context.MODE_PRIVATE);
baseurl = sharedPreferences.getString("url_selected", "");
OkHttpClient okHttpClient = new OkHttpClient().newBuilder()
.connectTimeout(25, TimeUnit.SECONDS)
.readTimeout(25, TimeUnit.SECONDS)
.writeTimeout(25, TimeUnit.SECONDS)
.build();
retrofit= new retrofit2.Retrofit.Builder()
.baseUrl(baseurl)
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论