动态从下拉列表中更改基本 URL。

huangapple go评论69阅读模式
英文:

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&lt;?&gt; parent, View view, int position, long id) {
                    SharedPreferences sharedPreferences = getSharedPreferences(Cons.PRIVATE_PREF, Context.MODE_PRIVATE);

            sharedPreferences.edit().putString(&quot;url_selected&quot;, spinner.getSelectedItem().toString()).apply();
}
           @Override
           public void onNothingSelected(AdapterView&lt;?&gt; 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(&quot;url_selected&quot;, &quot;&quot;);  

        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;

huangapple
  • 本文由 发表于 2020年9月30日 18:01:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/64135204.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定