【问题标题】:Retrofit dynamic base url change using singleton class使用单例类改造动态基本网址更改
【发布时间】:2017-01-23 20:38:05
【问题描述】:

这是我的单身课程。

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 需要在运行时更改此 url http://192.168.1.50:8008/inventory/Apiv1

如何在运行时动态更改这两个 url。请帮帮我。

【问题讨论】:

标签: android singleton retrofit2


【解决方案1】:

以下代码将显示一种在运行时更改改造的基本 url 的方法,但代价是稍微破坏了单例模式。该模式仍然部分适用。我稍后会解释。

检查GetRetrofit的这个修改版本。

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。如果您不需要更新默认的基本 url,那么您可以调用,

Retrofit retrofit = GetRetrofit.getInstance();

但是当我们需要将默认 url 更改为其他内容(例如http://192.168.1.50:8008/inventory/Apiv1)时,我们需要先更新我们的基本 url。

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

仅当您更新 url 时,才会基于此新 url 构建一个新的 retrofit 实例。只要不需要更新base url,直接调用就可以得到单例改造实例,

Retrofit retrofit = GetRetrofit.getInstance();

所以它在某种程度上保留了单例模式的特点。

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-29
    • 2019-08-07
    相关资源
    最近更新 更多