コード例 #1
0
  /** 获取配置好的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;
  }
コード例 #2
0
  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);
  }
コード例 #3
0
 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
 /**
  * Clones the okClient given in parameter, adds the auth interceptors and uses it to configure the
  * Retrofit
  *
  * @param okClient
  */
 public void configureFromOkclient(OkHttpClient okClient) {
   OkHttpClient clone = okClient.clone();
   addAuthsToOkClient(clone);
   adapterBuilder.client(clone);
 }
コード例 #5
0
 public <S> S createService(Class<S> serviceClass) {
   return adapterBuilder.build().create(serviceClass);
 }
コード例 #6
0
  public static PlexHttpService getService(
      String url, String username, String password, boolean debug, int timeout) {
    OkHttpClient client = new OkHttpClient();
    if (timeout > 0) client.setReadTimeout(timeout, TimeUnit.SECONDS);
    Retrofit.Builder builder =
        new Retrofit.Builder()
            .baseUrl(url)
            .client(client)
            .addConverterFactory(SimpleXmlConverterFactory.create());

    if (username != null && password != null) {
      String creds = username + ":" + password;
      final String basic = "Basic " + Base64.encodeToString(creds.getBytes(), Base64.NO_WRAP);
      client
          .interceptors()
          .add(
              new Interceptor() {
                @Override
                public com.squareup.okhttp.Response intercept(Chain chain) throws IOException {
                  Request original = chain.request();

                  Request.Builder requestBuilder =
                      original
                          .newBuilder()
                          .header("Authorization", basic)
                          .header("Accept", "text/xml")
                          .method(original.method(), original.body());

                  Request request = requestBuilder.build();
                  return chain.proceed(request);
                }
              });
    }
    if (debug) {
      client
          .interceptors()
          .add(
              new Interceptor() {
                @Override
                public com.squareup.okhttp.Response intercept(Chain chain) throws IOException {
                  try {
                    com.squareup.okhttp.Response response = chain.proceed(chain.request());
                    ResponseBody responseBody = response.body();
                    String body = response.body().string();
                    Logger.d("Retrofit@Response: (%d) %s", response.code(), body);
                    com.squareup.okhttp.Response newResponse =
                        response
                            .newBuilder()
                            .body(ResponseBody.create(responseBody.contentType(), body.getBytes()))
                            .build();
                    return newResponse;
                  } catch (Exception e) {
                  }
                  return null;
                }
              });
    }

    // Plex Media Player currently returns an empty body instead of valid XML for many calls, so we
    // must detect an empty body
    // and write our own valid XML in place of it
    client
        .interceptors()
        .add(
            new Interceptor() {
              @Override
              public com.squareup.okhttp.Response intercept(Chain chain) throws IOException {
                try {
                  com.squareup.okhttp.Response response = chain.proceed(chain.request());
                  ResponseBody responseBody = response.body();
                  String body = response.body().string();
                  if (body.equals("")) {
                    body =
                        String.format(
                            "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n"
                                + "<Response code=\"%d\" status=\"%s\" />",
                            response.code(), response.code() == 200 ? "OK" : "Error");
                  }
                  com.squareup.okhttp.Response newResponse =
                      response
                          .newBuilder()
                          .body(ResponseBody.create(responseBody.contentType(), body.getBytes()))
                          .build();
                  return newResponse;
                } catch (Exception e) {
                }

                return null;
              }
            });

    Retrofit retrofit = builder.client(client).build();
    return retrofit.create(PlexHttpService.class);
  }