
本文共 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}")ObservableshopBook(@Path("id") String id);
���������������GET������
@GET(DOMAIN2 + "/common/book/type")Observable
> getBookType();
POST������
@POST(DOMAIN1 + "/yoshin/user/register")Observableregister(@Body Map body);
DELETE������
@DELETE(DOMAIN2 + "/service/clear/location")Observableclear();
������������
Retrofit������������������������������������������������
Streaming
������@Streaming������������������������ohnline������������������������������������������
@Streaming@GETObservabledownload(@Url String url);
Multipart������������
������������������������������������������������
@Multipart@POSTObservabletestFileUpload1(@PartMap Map params);
������������������
������@FormUrlEncoded������������@Field���������������������������
@FormUrlEncoded@POSTObservabletestFormUrlEncoded1(@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������������������������������������������������������������
发表评论
最新留言
关于作者
