2017-01-23 29 views
0

這是我的單例類。使用單例類改進動態基礎URL更改

public class GetRetrofit { 


static volatile Retrofit retrofit = null; 

public static Retrofit getInstance() { 
    if (retrofit == null) { 
     synchronized (GetRetrofit.class) { 
      if (retrofit == null) { 
       OkHttpClient.Builder builder = new OkHttpClient().newBuilder(); 
       builder.readTimeout(30, TimeUnit.SECONDS); 
       builder.connectTimeout(30, TimeUnit.SECONDS); 

       HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(); 
       interceptor.setLevel(HttpLoggingInterceptor.Level.BODY); 
       builder.addInterceptor(interceptor); 

       // builder.addInterceptor(new UnauthorisedInterceptor(context)); 
       OkHttpClient client = builder.build(); 

       retrofit = 
         new Retrofit.Builder().baseUrl("DYNAMIC_URL") 
           .client(client).addConverterFactory(GsonConverterFactory.create()).build(); 

       //addConverterFactory(SimpleXmlConverterFactory.create()) 
      } 
     } 
    } 

    return retrofit; 

} 
} 

我想更改動態基本網址。

例如:http://192.168.1.60:8888/property/Apiv1需要在運行時更改此網址http://192.168.1.50:8008/inventory/Apiv1

如何在運行時動態更改這兩個網址。請幫幫我。

回答

1

以下代碼將顯示在運行時更改基礎網址的改進方法,但代價是稍微打破單例模式。該模式仍然部分適用。我會在稍後解釋。

查看此修改後的版本GetRetrofit out。

public class GetRetrofit { 

     static volatile Retrofit retrofit = null; 
     private static String baseUrlString = "http://192.168.1.60:8888/property/Apiv1"; 

     public static void updateBaseUrl(String url){ 
       baseUrlString = url; 

       retrofit = getRetrofitObj(); 
     } 

     public static Retrofit getInstance() { 
       if (retrofit == null) { 
         synchronized (GetRetrofit.class) { 
           if (retrofit == null) { 
             retrofit = getRetrofitObj(); 
           } 
         } 
       } 

       return retrofit; 
     } 

     public static Retrofit getRetrofitObj() { 
       OkHttpClient.Builder builder = new OkHttpClient().newBuilder(); 
       builder.readTimeout(30, TimeUnit.SECONDS); 
       builder.connectTimeout(30, TimeUnit.SECONDS); 

       HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(); 
       interceptor.setLevel(HttpLoggingInterceptor.Level.BODY); 
       builder.addInterceptor(interceptor); 

       // builder.addInterceptor(new UnauthorisedInterceptor(context)); 
       OkHttpClient client = builder.build(); 

       retrofit = new Retrofit.Builder().baseUrl(baseUrlString) 
         .client(client).addConverterFactory(GsonConverterFactory.create()).build(); 
     } 
} 

所以,GetRetrofit有這個baseUrlString初始化/保存我們的動態網址。如果您不需要更新默認的基本URL,然後你很好通話,

Retrofit retrofit = GetRetrofit.getInstance(); 

但是當我們需要的默認網址更改到別的東西(http://192.168.1.50:8008/inventory/Apiv1,例如),我們需要更新我們的基地網址。

GetRetrofit.updateBaseUrl("http://192.168.1.50:8008/inventory/Apiv1"); 
Retrofit retrofit = GetRetrofit.getInstance(); // this provides new retrofit instance with given url 

這建立在此基礎上新的網址只有當你更新網址新retrofit實例。只要你不需要更新基礎URL,您可以通過調用簡單地得到單身改造實例,

Retrofit retrofit = GetRetrofit.getInstance(); 

所以,在某種程度上,它部分保持Singleton模式的特點。

希望這會有所幫助。