/** 获取配置好的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;
  }
Example #2
0
    public static InstagramService create() {
      Retrofit retrofit =
          new Retrofit.Builder()
              .baseUrl(API_BASE_URL)
              .addConverterFactory(GsonConverterFactory.create())
              .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
              .build();

      return retrofit.create(InstagramService.class);
    }
Example #3
0
 public static APIService getInstance() {
   Retrofit retrofit =
       new Retrofit.Builder()
           .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
           .addConverterFactory(JacksonConverterFactory.create())
           .baseUrl("http://api.map.baidu.com/")
           .build();
   apiService = retrofit.create(APIService.class);
   return apiService;
 }
  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 Retrofit getInstance() {
   if (instance == null) {
     instance =
         new Retrofit.Builder()
             .baseUrl(NetworkConstants.BASE_URL)
             .addConverterFactory(GsonConverterFactory.create())
             .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
             .build();
   }
   return instance;
 }
Example #6
0
 public ApisRetrofit() {
   OkHttpClient client = new OkHttpClient();
   client.setReadTimeout(30, TimeUnit.SECONDS);
   Retrofit retrofit =
       new Retrofit.Builder()
           .client(client)
           .baseUrl("http://apistore.baidu.com")
           .addConverterFactory(GsonConverterFactory.create())
           .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
           .build();
   service = retrofit.create(ApisService.class);
 }
 public static GitHub getGitHubApi() {
   if (gitHubApi == null) {
     synchronized (SimpleService.class) {
       if (gitHubApi == null) {
         gitHubApi =
             new Retrofit.Builder()
                 .baseUrl(API_URL)
                 .client(getOkHttpClient())
                 .addConverterFactory(GsonConverterFactory.create())
                 .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                 .build()
                 .create(GitHub.class);
       }
     }
   }
   return gitHubApi;
 }
Example #8
0
  public Retrofit provideRetrofit(Application app, Context context) {

    isNetworkAvailable = isNetworkAvailable(context);
    Cache cache = provideOkHttpCache(app);
    OkHttpClient okHttpClient = provideOkHttpClient(cache);

    Retrofit retrofit =
        new Retrofit.Builder()
            .baseUrl(StaticValue.BASE_URL)
            .addCallAdapterFactory(
                RxJavaCallAdapterFactory
                    .create()) // In order to convert the API response type Observable, we have to
                               // set the call adapter to RxJavaCallAdapter.
            .addConverterFactory(GsonConverterFactory.create())
            .client(okHttpClient)
            .build();
    return retrofit;
  }
  @Override
  public void onCreate() {
    super.onCreate();

    context = getApplicationContext();
    client.setConnectTimeout(30, TimeUnit.SECONDS);
    client.setReadTimeout(120, TimeUnit.SECONDS);
    client.setWriteTimeout(120, TimeUnit.SECONDS);
    client.setCache(getDiskCache("httpclient"));

    picasso = new Picasso.Builder(this).downloader(new OkHttpDownloader(client)).build();
    retrofit =
        new Retrofit.Builder()
            .baseUrl("http://private-524e0-ecommerce4.apiary-mock.com/")
            .client(client)
            .addConverterFactory(GsonConverterFactory.create())
            .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
            .build();
  }
Example #10
0
  private Api newRestAdapter() {
    Gson gson = ApiGsonBuilder.builder().create();

    BaseUrl baseUrl =
        () -> {
          if (BuildConfig.DEBUG && settings.mockApi()) {
            // activate this to use a mock
            return HttpUrl.parse("http://10.1.1.56:8888");
          } else {
            return HttpUrl.parse(UriHelper.of(context).base().toString());
          }
        };

    return new Retrofit.Builder()
        .baseUrl(baseUrl)
        .addConverterFactory(new OkHttpAwareConverterFactory(GsonConverterFactory.create(gson)))
        .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
        .client(this.client)
        .build()
        .create(Api.class);
  }