static IterableService newInstance(String apiKey) {
   // all of this intercepter/chain stuff is just so callers don't
   // have to pass the API key into every single method/api call
   OkHttpClient client = new OkHttpClient();
   client
       .interceptors()
       .add(
           chain ->
               chain.proceed(
                   chain
                       .request()
                       .newBuilder()
                       .url(
                           chain
                               .request()
                               .httpUrl()
                               .newBuilder()
                               .addQueryParameter(IterableService.PARAM_API_KEY, apiKey)
                               .build())
                       .build()));
   HttpUrl url =
       new HttpUrl.Builder()
           .scheme("https")
           .host(IterableService.HOST)
           .addQueryParameter(IterableService.PARAM_API_KEY, apiKey)
           .build();
   Retrofit retrofit =
       new Retrofit.Builder()
           .baseUrl(url)
           .client(client)
           .addConverterFactory(GsonConverterFactory.create())
           .build();
   return retrofit.create(IterableService.class);
 }
  /** 获取配置好的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;
  }
Пример #3
0
public class EventRESTClient {

  private Retrofit retrofit =
      new Retrofit.Builder()
          .baseUrl("http://eskimo-cmpe277.rhcloud.com")
          .addConverterFactory(GsonConverterFactory.create())
          .build();

  private EventAPI service = retrofit.create(EventAPI.class);

  public Call<Event> createEvent(Event e) {
    return service.createEvent(
        e.getUser_id(),
        e.getEvent_details(),
        e.getEvent_name(),
        e.getLocation(),
        e.getStart_time(),
        e.getEnd_time());
  }

  public Call<List<Event>> getAttendingEvents(String userId) {
    return service.getAttendingEvents(userId);
  }

  public Call<Event> getEventById(String id) {
    return service.getEventById(id);
  }
}
 public MovieAPIService() {
   mRetrofit =
       new Retrofit.Builder()
           .baseUrl(MovieAPIService.API_URL)
           .addConverterFactory(GsonConverterFactory.create())
           .build();
 }
Пример #5
0
  @NonNull
  private static Retrofit getRetrofit() {

    return new Retrofit.Builder()
        .baseUrl("http://test.boloid.com:9000")
        .addConverterFactory(GsonConverterFactory.create())
        .client(CLIENT)
        .build();
  }
 protected void registerJsonAdapter() {
   // Add the interceptor to OkHttpClient
   api =
       new Retrofit.Builder()
           .baseUrl(getBaseEndpoint())
           .addConverterFactory(GsonConverterFactory.create())
           .client(createClient())
           .build();
 }
Пример #7
0
  protected Retrofit buildJsonOauthRetrofitClient(OAuthClientParameters params) {
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.setDateFormat(Twitter.DATE_FORMAT);

    return new Retrofit.Builder()
        .baseUrl("https://api.twitter.com")
        .client(new OAuthHttpClient(params))
        .addConverterFactory(GsonConverterFactory.create(gsonBuilder.create()))
        .build();
  }
Пример #8
0
  private DataManager() {

    Retrofit retrofit =
        new Retrofit.Builder()
            .baseUrl(BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .build();

    mApiInterface = retrofit.create(ApiInterface.class);
  }
Пример #9
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);
    }
  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);
  }
Пример #11
0
 @Provides
 @Singleton
 Retrofit provideRetrofit(Gson gson, OkHttpClient okHttpClient) {
   Retrofit retrofit =
       new Retrofit.Builder()
           .addConverterFactory(GsonConverterFactory.create(gson))
           .baseUrl(mBaseUrl)
           .client(okHttpClient)
           .build();
   return retrofit;
 }
 public static Retrofit getInstance() {
   if (instance == null) {
     instance =
         new Retrofit.Builder()
             .baseUrl(NetworkConstants.BASE_URL)
             .addConverterFactory(GsonConverterFactory.create())
             .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
             .build();
   }
   return instance;
 }
  @Provides
  PostsAPI providePostsApi() {
    OkHttpClient okHttpClient = new OkHttpClient();

    okHttpClient.setConnectTimeout(5, TimeUnit.SECONDS); // connect timeout
    okHttpClient.setReadTimeout(5, TimeUnit.SECONDS);

    okHttpClient
        .interceptors()
        .add(
            new Interceptor() {
              @Override
              public Response intercept(Chain chain) throws IOException {

                Request originalRequest = chain.request(); // Current Request
                Log.i("Body", bodyToString(originalRequest));
                Response response = chain.proceed(originalRequest); // Get response of the request

                // I am logging the response body in debug mode. When I do this I consume the
                // response (OKHttp only lets you do this once) so i have re-build a new one using
                // the cached body
                String bodyString = response.body().string();
                Log.i("Response Body", bodyString);

                Log.i(
                    "NetworkModule",
                    String.format(
                        "Sending request %s with headers %s ",
                        originalRequest.url(), originalRequest.headers()));
                Log.i(
                    "",
                    (String.format(
                        "Got response HTTP %s %s \n\n with body %s \n\n with headers %s ",
                        response.code(), response.message(), bodyString, response.headers())));
                response =
                    response
                        .newBuilder()
                        .body(ResponseBody.create(response.body().contentType(), bodyString))
                        .build();

                return response;
              }
            });

    Retrofit retrofit =
        new Retrofit.Builder()
            .baseUrl("http://jsonplaceholder.typicode.com")
            .client(okHttpClient)
            .addConverterFactory(GsonConverterFactory.create())
            .build();

    return retrofit.create(PostsAPI.class);
  }
Пример #14
0
  public static <S> S createService(Class<S> serviceClass, int api_type) {

    OkHttpClient client = new OkHttpClient();
    Retrofit retrofit =
        new Retrofit.Builder()
            .baseUrl(RestConfig.getBaseUrl(api_type))
            .addConverterFactory(GsonConverterFactory.create(GsonFactory.createGson(api_type)))
            .client(client)
            .build();

    return retrofit.create(serviceClass);
  }
Пример #15
0
  @Override
  public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    retrofit =
        new Retrofit.Builder()
            .baseUrl("http://api.themoviedb.org/")
            .addConverterFactory(GsonConverterFactory.create())
            .build();

    service = retrofit.create(MovieService.class);
  }
Пример #16
0
 static {
   HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
   logging.setLevel(HttpLoggingInterceptor.Level.BASIC);
   OkHttpClient httpClient = new OkHttpClient();
   httpClient.interceptors().add(logging);
   retrofitInstance =
       new Retrofit.Builder()
           .baseUrl(MovieUtils.BASE_URL)
           .addConverterFactory(GsonConverterFactory.create())
           .client(httpClient)
           .build();
 }
Пример #17
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);
 }
Пример #18
0
  public BaseApi() {

    String strUserName = CustomPreferences.getPreferences(Constants.PREF_USERNAME, "");
    String strPassword = CustomPreferences.getPreferences(Constants.PREF_PASSWORD, "");
    Gson gson = new GsonBuilder().disableHtmlEscaping().create();
    OkHttpClient httpClient = new OkHttpClient();
    final String credential = Credentials.basic(strUserName, strPassword);
    httpClient
        .interceptors()
        .add(
            new Interceptor() {
              @Override
              public com.squareup.okhttp.Response intercept(Interceptor.Chain chain)
                  throws IOException {

                com.squareup.okhttp.Request original = chain.request();
                com.squareup.okhttp.Request request =
                    original
                        .newBuilder()
                        .header("Authorization", credential)
                        .header("Accept", "application/vnd.textus-v2+json")
                        .method(original.method(), original.body())
                        .build();
                LogUtil.e("Url : ", request.httpUrl().toString());
                com.squareup.okhttp.Response response = chain.proceed(request);
                if (request.body() != null) {

                  Buffer buffer = new Buffer();
                  request.body().writeTo(buffer);
                  String body = buffer.readUtf8();
                  LogUtil.e("body : ", body);
                }

                String strBody = response.body().string();
                LogUtil.e("Response", strBody);
                return response
                    .newBuilder()
                    .body(ResponseBody.create(response.body().contentType(), strBody))
                    .build();
              }
            });

    mRetrofit =
        new Retrofit.Builder()
            .baseUrl(Constants.BASE_URL)
            .client(httpClient)
            .addConverterFactory(GsonConverterFactory.create(gson))
            .build();
  }
  private NWClient() {

    okHttpClient = new OkHttpClient();
    okHttpClient.setFollowRedirects(false);

    builder =
        new Retrofit.Builder()
            .baseUrl("https://admin.citruspay.com/")
            .client(okHttpClient)
            .addConverterFactory(GsonConverterFactory.create())
            .build();

    initUMAPIClient();
    initPGAPIClient();
  }
 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;
 }
  @Override
  public void onCreate() {
    super.onCreate();
    Gson gson = new GsonBuilder().create();

    Retrofit retrofit =
        new Retrofit.Builder()
            .baseUrl(service_url)
            .addConverterFactory(GsonConverterFactory.create(gson))
            .build();
    donationService = retrofit.create(DonationServiceProxy.class);

    timer = new Timer();

    // this.refreshDonationList();

    Log.v("Donation", "Donation App Started");
  }
Пример #22
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();
  }
 /** Initializes the Retrofit network adapter and creates the API interface. */
 private void initNetworking() {
   OkHttpClient client = new OkHttpClient();
   if (BuildConfig.DEBUG) {
     HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
     interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
     client.interceptors().add(interceptor);
   }
   Gson gson =
       new GsonBuilder()
           .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
           .create();
   Retrofit retrofit =
       new Retrofit.Builder()
           .baseUrl(String.format("https://%s.auth0.com/", mAuthDomain))
           .client(client)
           .addConverterFactory(GsonConverterFactory.create(gson))
           .build();
   mLoginApi = retrofit.create(LoginAPI.class);
 }
Пример #25
0
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Retrofit retrofit =
        new Retrofit.Builder()
            .baseUrl("https://api.stackexchange.com/2.2/")
            .addConverterFactory(GsonConverterFactory.create())
            .build();

    stackExchangeService = retrofit.create(StackExchangeService.class);

    questionsAdapter =
        new QuestionsAdapter(MainActivity.this, R.layout.questions_list_row, new ArrayList<Item>());

    initRefreshButton();
    initQuestionsList();
  }
Пример #26
0
  public static FreshmanApiInterface getClient() {
    if (feApiInterface == null) {

      OkHttpClient okClient = new OkHttpClient();
      HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
      interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
      okClient.interceptors().add(interceptor);

      Retrofit client =
          new Retrofit.Builder()
              .baseUrl(baseUrl)
              .addConverter(String.class, new ToStringConverter())
              .client(okClient)
              .addConverterFactory(GsonConverterFactory.create())
              .build();
      feApiInterface = client.create(FreshmanApiInterface.class);
    }
    return feApiInterface;
  }
Пример #27
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);
  }
Пример #28
0
  public boolean cancelOrder(int id) {

    Retrofit retrofit =
        new Retrofit.Builder()
            .baseUrl("https://app.bb-evacuator.ru/api/")
            .addConverterFactory(GsonConverterFactory.create())
            .build();

    Api api = retrofit.create(Api.class);
    Log.d("SD11", "OK");
    Call<CancelResponse> call = api.cancelOrder(id);
    call.enqueue(
        new Callback<CancelResponse>() {

          @Override
          public void onResponse(Response<CancelResponse> response, Retrofit retrofit) {
            CancelResponse re = response.body();
            Log.d("SD11", response.message());
            if ((response.code() != 404 && response.code() != 401)) {
              isOk = re.getError() == 0;
              Log.d("SD11", String.valueOf(response.code()));
              Log.d("SD11", re.getErrorMessage());
              Log.d("SD11", String.valueOf(re.getError()));
              re.getErrorMessage();
              re.getError();
            } else {
              isOk = false;
            }
            Log.d("SD11", String.valueOf(isOk));
          }

          @Override
          public void onFailure(Throwable t) {
            Log.d("SD11", "eror" + t.toString());
          }
        });

    return isOk;
  }
Пример #29
0
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.create_trip_screen);
    ButterKnife.bind(this);

    // Get tracker.
    t = ((Ntrip) getApplication()).getTracker(Ntrip.TrackerName.APP_TRACKER);

    retrofit =
        new Retrofit.Builder()
            .baseUrl(Utilitys.baseUrl)
            .addConverterFactory(GsonConverterFactory.create())
            .build();
    reqCall = retrofit.create(RequestCallBack.class);

    AdView mAdView = (AdView) findViewById(R.id.adView);
    AdRequest adRequest =
        new AdRequest.Builder().addTestDevice(AdRequest.DEVICE_ID_EMULATOR).build();

    mAdView.loadAd(adRequest);
  }
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    /*
            setListAdapter(new RecetaAdapter(
                    getActivity(),
                    RepoRecetas.getInstance().getRecetas(null,10)));

    */
    final String BASE_URL = "http://192.168.247.112:8080/";

    Retrofit retrofit =
        new Retrofit.Builder()
            .baseUrl(BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .build();

    recetaService = retrofit.create(RecetasService.class);

    Call<List<Receta>> recetasCall = recetaService.getRecetas();

    recetasCall.enqueue(
        new Callback<List<Receta>>() {
          @Override
          public void onResponse(Response<List<Receta>> response, Retrofit retrofit) {
            List<Receta> recetasList = response.body();

            setListAdapter(new RecetaAdapter(getActivity(), recetasList));
          }

          @Override
          public void onFailure(Throwable t) {
            t.printStackTrace();
            Log.e("RecetasApp", t.getMessage());
          }
        });
  }