retrofit2.Retrofit类的使用及代码示例

x33g5p2x  于2022-01-28 转载在 其他  
字(9.6k)|赞(0)|评价(0)|浏览(207)

本文整理了Java中retrofit2.Retrofit类的一些代码示例,展示了Retrofit类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Retrofit类的具体详情如下:
包路径:retrofit2.Retrofit
类名称:Retrofit

Retrofit介绍

[英]Retrofit adapts a Java interface to HTTP calls by using annotations on the declared methods to define how requests are made. Create instances using Builder and pass your interface to #create to generate an implementation.

For example,

Retrofit retrofit = new Retrofit.Builder() 
.baseUrl("https://api.example.com/") 
.addConverterFactory(GsonConverterFactory.create()) 
.build(); 
MyApi api = retrofit.create(MyApi.class); 
Response<User> user = api.getUser().execute();

[中]改型通过在声明的方法上使用注释来定义如何发出请求,从而使Java接口适应HTTP调用。使用Builder创建实例,并将界面传递给#Create以生成实现。
例如

Retrofit retrofit = new Retrofit.Builder() 
.baseUrl("https://api.example.com/") 
.addConverterFactory(GsonConverterFactory.create()) 
.build(); 
MyApi api = retrofit.create(MyApi.class); 
Response<User> user = api.getUser().execute();

代码示例

代码示例来源:origin: stackoverflow.com

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);

代码示例来源:origin: square/retrofit

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]);

代码示例来源:origin: bumptech/glide

@Provides ImgurService getImgurService(Retrofit retrofit) {
 return retrofit.create(ImgurService.class);
}

代码示例来源:origin: influxdata/influxdb-java

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);

代码示例来源:origin: square/retrofit

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);
 }
}

代码示例来源:origin: line/armeria

@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/");
  }
}

代码示例来源:origin: stackoverflow.com

Retrofit retrofit = new Retrofit.Builder()
  .baseUrl("https://api.github.com")
  .build();

GitHubService service = retrofit.create(GitHubService.class);

代码示例来源:origin: square/retrofit

@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);
 }
}

代码示例来源:origin: line/armeria

@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/");
}

代码示例来源:origin: stackoverflow.com

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);
 }
}

代码示例来源:origin: square/retrofit

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 + ")");
  }
 }
}

代码示例来源:origin: gradle.plugin.ru.rambler.jiratasksupdater/gradle-plugin

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);
}

代码示例来源:origin: square/retrofit

@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);
}

代码示例来源:origin: line/armeria

@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/");
}

代码示例来源:origin: BaronZ88/MinimalistWeather

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);
  }

代码示例来源:origin: commonsguy/cw-omnibus

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);
}

代码示例来源:origin: gradle.plugin.ru.rambler.jiratasksupdater/jiraTaskUpdater

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);
}

代码示例来源:origin: square/retrofit

@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);
 }
}

代码示例来源:origin: line/armeria

@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/");
}

代码示例来源:origin: stackoverflow.com

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);

相关文章