每天记录学习的新知识 :Retrofit2过一遍
发布日期:2021-05-10 05:21:24 浏览次数:18 分类:精选文章

本文共 3263 字,大约阅读时间需要 10 分钟。

Square���������������Retrofit������������������������������������������������������������������������������������������������������������������������������������

������������

Retrofit���������������������������������������������������������������������������������������������������Json������������������Gson���������������������RxJava������������������

������������

������������������Retrofit������������������������������

  • com.squareup.retrofit2:retrofit:2.2.0
  • com.squareup.retrofit2:adapter-rxjava2:2.2.0
  • com.squareup.retrofit2:converter-gson:2.2.0

������������������OkHttp3������������������ if -toggler.

������������

���������������������������������������������������������������������������������������������������������������������

������������GET������

@GET(DOMAIN2 + "/shop/user2/book/{id}")
Observable
shopBook(@Path("id") String id);

���������������GET������

@GET(DOMAIN2 + "/common/book/type")
Observable
> getBookType();

POST������

@POST(DOMAIN1 + "/yoshin/user/register")
Observable
register(@Body Map
body);

DELETE������

@DELETE(DOMAIN2 + "/service/clear/location")
Observable
clear();

������������

Retrofit������������������������������������������������

Streaming

������@Streaming������������������������ohnline������������������������������������������

@Streaming
@GET
Observable
download(@Url String url);

Multipart������������

������������������������������������������������

@Multipart
@POST
Observable
testFileUpload1(@PartMap Map
params);

������������������

������@FormUrlEncoded������������@Field���������������������������

@FormUrlEncoded
@POST
Observable
testFormUrlEncoded1(@Field("username") String name, @Field("age") int age);

���������������

������������������������������������Retrofit���������

Gson������

���������������������Gson������������

private Gson createGson() {
GsonBuilder builder = new GsonBuilder();
builder.serializeNulls();
builder.registerTypeAdapterFactory(new ResponseTypeAdapterFactory());
return builder.create();
}

OKHttp���������

���������OKHttp���������������������������������������

private OkHttpClient createOkHttp() {
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.addInterceptor(interceptor);
if (BuildConfig.isDebug) {
httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
builder.addInterceptor(httpLoggingInterceptor);
}
builder.retryOnConnectionFailure(true);
builder.connectTimeout(30, TimeUnit.SECONDS);
return builder.build();
}

������Retrofit������

���������������������������Retrofit������������

private void createRetrofit(String baseUrl) {
Retrofit.Builder builder = new Retrofit.Builder();
builder.addConverterFactory(GsonConverterFactory.create(createGson()));
builder.addCallAdapterFactory(RxJava2CallAdapterFactory.create());
builder.client(createOkHttp());
builder.baseUrl(baseUrl);
api = builder.build().create(Api.class);
}

���������������������������������������������������Retrofit���������������������������������������������������������������

���������������������������������������Retrofit������������������������������������������������������������

上一篇:SmartRefreshLayout 上拉加载下拉刷新
下一篇:每天记录学习的新知识 :OKHTTP 下载进度计算

发表评论

最新留言

初次前来,多多关照!
[***.217.46.12]2025年04月06日 16时59分20秒