HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(); interceptor.setLevel(HttpLoggingInterceptor.Level.BODY); OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build(); Retrofit retrofit = new Retrofit.Builder() .baseUrl("https://backend.example.com") .client(client) .addConverterFactory(GsonConverterFactory.create()) .build(); return retrofit.create(ApiClient.class);
Retrofit retrofit = new Retrofit.Builder() .baseUrl(server.url("/")) .addConverterFactory(GsonConverterFactory.create()) .build(); Service service = retrofit.create(Service.class); retrofit.responseBodyConverter(Error.class, new Annotation[0]);
@Provides ImgurService getImgurService(Retrofit retrofit) { return retrofit.create(ImgurService.class); }
this.hostName = parseHost(url); this.loggingInterceptor = new HttpLoggingInterceptor(); setLogLevel(LOG_LEVEL); OkHttpClient.Builder clonedOkHttpBuilder = okHttpBuilder.build().newBuilder(); clonedOkHttpBuilder.addInterceptor(loggingInterceptor).addInterceptor(gzipRequestInterceptor). addInterceptor(new BasicAuthInterceptor(username, password)); Factory converterFactory = null; Retrofit.Builder clonedRetrofitBuilder = retrofitBuilder.baseUrl(url).build().newBuilder(); this.retrofit = clonedRetrofitBuilder.client(clonedOkHttpBuilder.build()) .addConverterFactory(converterFactory).build(); this.influxDBService = this.retrofit.create(InfluxDBService.class);
private static <ResponseT> Converter<ResponseBody, ResponseT> createResponseConverter( Retrofit retrofit, Method method, Type responseType) { Annotation[] annotations = method.getAnnotations(); try { return retrofit.responseBodyConverter(responseType, annotations); } catch (RuntimeException e) { // Wide exception range because factories are user code. throw methodError(method, e, "Unable to create converter for %s", responseType); } }
@Test public void build_armeriaGroupAuthority() throws Exception { assertThat(new ArmeriaRetrofitBuilder().baseUrl("http://group:myGroup/").build().baseUrl() .toString()) // NB: lower-cased by OkHttp .isEqualTo("http://group_mygroup/"); assertThat(new ArmeriaRetrofitBuilder().baseUrl("http://group:myGroup").build().baseUrl() .toString()) .isEqualTo("http://group_mygroup/"); } }
Retrofit retrofit = new Retrofit.Builder() .baseUrl("https://api.github.com") .build(); GitHubService service = retrofit.create(GitHubService.class);
@Override public @Nullable Converter<ResponseBody, ?> responseBodyConverter( Type type, Annotation[] annotations, Retrofit retrofit) { if (getRawType(type) != Optional.class) { return null; } Type innerType = getParameterUpperBound(0, (ParameterizedType) type); Converter<ResponseBody, Object> delegate = retrofit.responseBodyConverter(innerType, annotations); return new OptionalConverter<>(delegate); } }
@Test public void build() throws Exception { final Retrofit retrofit = new ArmeriaRetrofitBuilder().baseUrl("http://example.com:8080/").build(); assertThat(retrofit.baseUrl().toString()).isEqualTo("http://example.com:8080/"); }
class Factory { public static APIService create(Context context) { OkHttpClient.Builder builder = new OkHttpClient().newBuilder(); builder.readTimeout(10, TimeUnit.SECONDS); builder.connectTimeout(5, TimeUnit.SECONDS); if (BuildConfig.DEBUG) { HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(); interceptor.setLevel(HttpLoggingInterceptor.Level.BASIC); builder.addInterceptor(interceptor); } builder.addInterceptor(chain -> { Request request = chain.request().newBuilder().addHeader("key", "value").build(); return chain.proceed(request); }); builder.addInterceptor(new UnauthorisedInterceptor(context)); OkHttpClient client = builder.build(); Retrofit retrofit = new Retrofit.Builder().baseUrl(APIService.ENDPOINT).client(client).addConverterFactory(GsonConverterFactory.create()).addCallAdapterFactory(RxJavaCallAdapterFactory.create()).build(); return retrofit.create(APIService.class); } }
public static void main(String... args) throws IOException { // Create a very simple REST adapter which points the GitHub API. Retrofit retrofit = new Retrofit.Builder() .baseUrl(API_URL) .addConverterFactory(GsonConverterFactory.create()) .build(); // Create an instance of our GitHub API interface. GitHub github = retrofit.create(GitHub.class); // Create a call instance for looking up Retrofit contributors. Call<List<Contributor>> call = github.contributors("square", "retrofit"); // Fetch and print a list of the contributors to the library. List<Contributor> contributors = call.execute().body(); for (Contributor contributor : contributors) { System.out.println(contributor.login + " (" + contributor.contributions + ")"); } } }
private JiraRestClient(String jiraEndpoint) { Retrofit retrofit = new Retrofit.Builder() .baseUrl(jiraEndpoint) .addConverterFactory(GsonConverterFactory.create()) .build(); converter = retrofit.responseBodyConverter(new TypeToken<ResponseBody>() { }.getType(), new Annotation[0]); authService = retrofit.create(JiraRestAuthService.class); apiService = retrofit.create(JiraRestApiService.class); }
@Override public @Nullable Converter<ResponseBody, ?> responseBodyConverter( Type type, Annotation[] annotations, Retrofit retrofit) { if (getRawType(type) != Optional.class) { return null; } Type innerType = getParameterUpperBound(0, (ParameterizedType) type); Converter<ResponseBody, Object> delegate = retrofit.responseBodyConverter(innerType, annotations); return new OptionalConverter<>(delegate); }
@Test public void build_withoutSlashAtEnd() throws Exception { final Retrofit retrofit = new ArmeriaRetrofitBuilder().baseUrl("http://example.com:8080").build(); assertThat(retrofit.baseUrl().toString()).isEqualTo("http://example.com:8080/"); }
private static <T> T initWeatherService(String baseUrl, Class<T> clazz) { OkHttpClient.Builder builder = new OkHttpClient().newBuilder(); if (BuildConfig.DEBUG) { HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor(); httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY); builder.addInterceptor(httpLoggingInterceptor); // builder.addNetworkInterceptor(new StethoInterceptor()); BuildConfig.STETHO.addNetworkInterceptor(builder); } OkHttpClient client = builder.build(); Retrofit retrofit = new Retrofit.Builder() .baseUrl(baseUrl) .addConverterFactory(FastJsonConverterFactory.create()) .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) .client(client) .build(); return retrofit.create(clazz); }
public QuestionsLoader(Context context) { super(context); Retrofit retrofit= new Retrofit.Builder() .baseUrl("https://api.stackexchange.com") .addConverterFactory(GsonConverterFactory.create()) .build(); so=retrofit.create(StackOverflowInterface.class); }
private JiraRestClient(String jiraEndpoint) { Retrofit retrofit = new Retrofit.Builder() .baseUrl(jiraEndpoint) .addConverterFactory(GsonConverterFactory.create()) .build(); converter = retrofit.responseBodyConverter(new TypeToken<ResponseBody>() { }.getType(), new Annotation[0]); authService = retrofit.create(JiraRestAuthService.class); apiService = retrofit.create(JiraRestApiService.class); }
@Override public @Nullable Converter<ResponseBody, ?> responseBodyConverter( Type type, Annotation[] annotations, Retrofit retrofit) { if (getRawType(type) != Optional.class) { return null; } Type innerType = getParameterUpperBound(0, (ParameterizedType) type); Converter<ResponseBody, Object> delegate = retrofit.responseBodyConverter(innerType, annotations); return new OptionalConverter<>(delegate); } }
@Test public void build_withNonRootPath() throws Exception { assertThat(new ArmeriaRetrofitBuilder().baseUrl("http://example.com:8080/a/b/c/") .build().baseUrl().toString()) .isEqualTo("http://example.com:8080/a/b/c/"); }
builder.readTimeout(10, TimeUnit.SECONDS); builder.connectTimeout(5, TimeUnit.SECONDS); HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(); interceptor.setLevel(HttpLoggingInterceptor.Level.BASIC); builder.addInterceptor(interceptor); new Retrofit.Builder().baseUrl(APIService.ENDPOINT).client(client).addConverterFactory(GsonConverterFactory.create()).addCallAdapterFactory(RxJavaCallAdapterFactory.create()).build(); return retrofit.create(APIService.class);