retrofit2.Retrofit.create()方法的使用及代码示例

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

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

Retrofit.create介绍

[英]Create an implementation of the API endpoints defined by the service interface.

The relative path for a given method is obtained from an annotation on the method describing the request type. The built-in methods are retrofit2.http.GET, retrofit2.http.PUT, retrofit2.http.POST, retrofit2.http.PATCH, retrofit2.http.HEAD, retrofit2.http.DELETE and retrofit2.http.OPTIONS. You can use a custom HTTP method with HTTP. For a dynamic URL, omit the path on the annotation and annotate the first parameter with Url.

Method parameters can be used to replace parts of the URL by annotating them with retrofit2.http.Path. Replacement sections are denoted by an identifier surrounded by curly braces (e.g., "{foo}"). To add items to the query string of a URL use retrofit2.http.Query.

The body of a request is denoted by the retrofit2.http.Body annotation. The object will be converted to request representation by one of the Converter.Factoryinstances. A RequestBody can also be used for a raw representation.

Alternative request body formats are supported by method annotations and corresponding parameter annotations:

  • retrofit2.http.FormUrlEncoded - Form-encoded data with key-value pairs specified by the retrofit2.http.Field parameter annotation.
  • retrofit2.http.Multipart - RFC 2388-compliant multipart data with parts specified by the retrofit2.http.Part parameter annotation.

Additional static headers can be added for an endpoint using the retrofit2.http.Headers method annotation. For per-request control over a header annotate a parameter with Header.

By default, methods return a Call which represents the HTTP request. The generic parameter of the call is the response body type and will be converted by one of the Converter.Factory instances. ResponseBody can also be used for a raw representation. Void can be used if you do not care about the body contents.

For example:

public interface CategoryService { 
@POST("category/{cat}/") 
Call<List<Item>> categoryList(@Path("cat") String a, @Query("page") int b); 
}

[中]创建由服务接口定义的API端点的实现。
给定方法的相对路径是从描述请求类型的方法上的注释中获得的。内置的方法是2。http。快,2号。http。放,2。http。发帖,2。http。补丁,2。http。头,2号。http。删除并修改2。http。选项。您可以将自定义HTTP方法用于HTTP。对于动态URL,请省略注释上的路径,并使用URL注释第一个参数。
方法参数可以用来替换URL的某些部分,方法参数可以用2来注释它们。http。路径替换部分由一个由大括号包围的标识符表示(例如“{foo}”)。要将项目添加到URL的查询字符串,请使用2。http。查询
请求主体由2表示。http。正文注释。该对象将由一个转换器转换为请求表示。工厂设备。RequestBody也可以用于原始表示。
方法注释和相应的参数注释支持替代请求正文格式:
*2。http。FormUrlEncoded-表单编码的数据,包含由2指定的键值对。http。字段参数注释。
*2。http。多部件-符合RFC 2388的多部件数据,带有改装2指定的部件。http。零件参数注释。
可以使用2为端点添加其他静态标头。http。标题方法注释。对于对标头的每请求控制,请使用标头注释参数。
默认情况下,方法返回代表HTTP请求的调用。调用的通用参数是响应主体类型,将由其中一个转换器转换。工厂实例。ResponseBody也可以用于原始表示。如果你不在乎身体的内容,可以使用Void。
例如:

public interface CategoryService { 
@POST("category/{cat}/") 
Call<List<Item>> categoryList(@Path("cat") String a, @Query("page") int b); 
}

代码示例

代码示例来源: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: stackoverflow.com

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

GitHubService service = retrofit.create(GitHubService.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: 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: smuyyh/BookReader

public BookApi(OkHttpClient okHttpClient) {
  Retrofit retrofit = new Retrofit.Builder()
      .baseUrl(Constant.API_BASE_URL)
      .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) // 添加Rx适配器
      .addConverterFactory(GsonConverterFactory.create()) // 添加Gson转换器
      .client(okHttpClient)
      .build();
  service = retrofit.create(BookApiService.class);
}

代码示例来源:origin: bingoogolapple/BGARefreshLayout-Android

@Override
public void onCreate() {
  super.onCreate();
  sInstance = this;
  mEngine = new Retrofit.Builder()
      .baseUrl("http://bgashare.bingoogolapple.cn/")
      .addConverterFactory(GsonConverterFactory.create())
      .build().create(Engine.class);
}

代码示例来源:origin: rengwuxian/RxJavaSamples

public static ZhuangbiApi getZhuangbiApi() {
  if (zhuangbiApi == null) {
    Retrofit retrofit = new Retrofit.Builder()
        .client(okHttpClient)
        .baseUrl("http://www.zhuangbi.info/")
        .addConverterFactory(gsonConverterFactory)
        .addCallAdapterFactory(rxJavaCallAdapterFactory)
        .build();
    zhuangbiApi = retrofit.create(ZhuangbiApi.class);
  }
  return zhuangbiApi;
}

代码示例来源:origin: rengwuxian/RxJavaSamples

public static GankApi getGankApi() {
  if (gankApi == null) {
    Retrofit retrofit = new Retrofit.Builder()
        .client(okHttpClient)
        .baseUrl("http://gank.io/api/")
        .addConverterFactory(gsonConverterFactory)
        .addCallAdapterFactory(rxJavaCallAdapterFactory)
        .build();
    gankApi = retrofit.create(GankApi.class);
  }
  return gankApi;
}

代码示例来源:origin: jgilfelt/chuck

static HttpbinApi getInstance(OkHttpClient client) {
  Retrofit retrofit = new Retrofit.Builder()
      .baseUrl("https://httpbin.org")
      .addConverterFactory(GsonConverterFactory.create())
      .client(client)
      .build();
  return retrofit.create(HttpbinApi.class);
}

代码示例来源:origin: HotBitmapGG/bilibili-android-client

/**
 * 根据传入的baseUrl,和api创建retrofit
 */
private static <T> T createApi(Class<T> clazz, String baseUrl) {
  Retrofit retrofit = new Retrofit.Builder()
      .baseUrl(baseUrl)
      .client(mOkHttpClient)
      .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
      .addConverterFactory(GsonConverterFactory.create())
      .build();
  return retrofit.create(clazz);
}

代码示例来源:origin: ivacf/archi

public static GithubService create() {
    Retrofit retrofit = new Retrofit.Builder()
        .baseUrl("https://api.github.com/")
        .addConverterFactory(GsonConverterFactory.create())
        .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
        .build();
    return retrofit.create(GithubService.class);
  }
}

代码示例来源:origin: ivacf/archi

public static GithubService create() {
    Retrofit retrofit = new Retrofit.Builder()
        .baseUrl("https://api.github.com/")
        .addConverterFactory(GsonConverterFactory.create())
        .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
        .build();
    return retrofit.create(GithubService.class);
  }
}

代码示例来源:origin: ivacf/archi

public static GithubService create() {
    Retrofit retrofit = new Retrofit.Builder()
        .baseUrl("https://api.github.com/")
        .addConverterFactory(GsonConverterFactory.create())
        .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
        .build();
    return retrofit.create(GithubService.class);
  }
}

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

@Override
public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 Retrofit retrofit=
  new Retrofit.Builder()
   .baseUrl("https://api.weather.gov")
   .addConverterFactory(GsonConverterFactory.create())
   .build();
 nws=retrofit.create(NWSInterface.class);
}

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

@Override
public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 Retrofit retrofit=
  new Retrofit.Builder()
   .baseUrl("https://api.weather.gov")
   .addConverterFactory(GsonConverterFactory.create())
   .build();
 nws=retrofit.create(NWSInterface.class);
}

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

public static void main(String... args) throws IOException {
  HostSelectionInterceptor hostSelectionInterceptor = new HostSelectionInterceptor();

  OkHttpClient okHttpClient = new OkHttpClient.Builder()
    .addInterceptor(hostSelectionInterceptor)
    .build();

  Retrofit retrofit = new Retrofit.Builder()
    .baseUrl("http://www.github.com/")
    .callFactory(okHttpClient)
    .build();

  Pop pop = retrofit.create(Pop.class);

  Response<ResponseBody> response1 = pop.robots().execute();
  System.out.println("Response from: " + response1.raw().request().url());
  System.out.println(response1.body().string());

  hostSelectionInterceptor.setHost("www.pepsi.com");

  Response<ResponseBody> response2 = pop.robots().execute();
  System.out.println("Response from: " + response2.raw().request().url());
  System.out.println(response2.body().string());
 }
}

代码示例来源:origin: bingoogolapple/BGABanner-Android

@Override
public void onCreate() {
  super.onCreate();
  sInstance = this;
  mEngine = new Retrofit.Builder()
      .baseUrl("http://bgashare.bingoogolapple.cn/banner/api/")
      .addConverterFactory(GsonConverterFactory.create())
      .build().create(Engine.class);
  Fresco.initialize(this);
}

代码示例来源:origin: apache/drill

public ServiceImpl(String connectionURL) {
 this.client = new Retrofit.Builder()
   .baseUrl(connectionURL)
   .addConverterFactory(JacksonConverterFactory.create())
   .build()
   .create(OpenTSDB.class);
}

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

public static void main(String... args) {
 Retrofit retrofit = new Retrofit.Builder()
   .baseUrl("http://httpbin.org")
   .addCallAdapterFactory(new ErrorHandlingCallAdapterFactory())
   .build();
 HttpBinService service = retrofit.create(HttpBinService.class);
 MyCall<Ip> ip = service.getIp();
 ip.enqueue(new MyCallback<Ip>() {

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

@Override
public void onViewCreated(@NonNull View view,
             @Nullable Bundle savedInstanceState) {
 super.onViewCreated(view, savedInstanceState);
 Retrofit retrofit=
  new Retrofit.Builder()
   .baseUrl("https://api.stackexchange.com")
   .addConverterFactory(GsonConverterFactory.create())
   .build();
 StackOverflowInterface so=
  retrofit.create(StackOverflowInterface.class);
 so.questions("android").enqueue(this);
}

相关文章