/** 获取配置好的retrofit对象来生产Manager对象 */
  public static Retrofit getRetrofit() {
    if (retrofit == null) {
      if (baseUrl == null || baseUrl.length() <= 0)
        throw new IllegalStateException("请在调用getFactory之前先调用setBaseUrl");

      Retrofit.Builder builder = new Retrofit.Builder();

      builder
          .baseUrl(baseUrl)
          .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
          .addConverterFactory(GsonConverterFactory.create());

      if (Configuration.isShowNetworkParams()) {
        OkHttpClient client = new OkHttpClient();
        com.squareup.okhttp.logging.HttpLoggingInterceptor interceptor =
            new com.squareup.okhttp.logging.HttpLoggingInterceptor();
        interceptor.setLevel(com.squareup.okhttp.logging.HttpLoggingInterceptor.Level.BODY);
        client.interceptors().add(new HttpLoggingInterceptor());

        builder.client(client);
      }

      retrofit = builder.build();
    }

    return retrofit;
  }
  public static <S> S newService(Class<S> serviceClass) {
    Retrofit.Builder builder =
        new Retrofit.Builder()
            .baseUrl(BuildConfig.BASE_URL)
            .client(client)
            .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
            .addConverterFactory(GsonConverterFactory.create(GsonWrapper.getGson()));

    return builder.build().create(serviceClass);
  }
 public static WeatherAPI createWeatherService(boolean isLoggerOn) {
   ObjectMapper mapper = new ObjectMapper();
   Retrofit.Builder builder =
       new Retrofit.Builder()
           .baseUrl(Constants.BASE_WEATHER_API_URL)
           .addConverterFactory(JacksonConverterFactory.create(mapper));
   if (isLoggerOn) {
     builder.client(createOkHttpClient());
   }
   Retrofit retrofit = builder.build();
   return retrofit.create(WeatherAPI.class);
 }
예제 #4
0
 public <S> S createService(Class<S> serviceClass) {
   return adapterBuilder.build().create(serviceClass);
 }