现在Android网络请求大部分已经换成了Retrofit2.0 +RxJava + 0kHttp3.0,Retrofit 使用接口的方式,负责请求的数据和请求的结果,OkHttp 负责请求的过程,RxJava 负责异步,各种线程之间的切换。毫无疑问这三剑客 已成为当前Android 网络请求最流行的方式。
简介
Retrofit: Retrofit是Square 公司开发的一款基于restful风格接口的Android 网络请求的框架,对Okhttp做了一层封装。网络请求还是基于Okhttp,我们只需要通过简单的配置就能使用Retrofit来进行网络请求了。Retrofit官网
RxJava:RxJava 在 GitHub 主页上介绍 "a library for composing asynchronous and event-based programs using observable sequences for the Java VM"(一个在 Java VM 上使用可观测的序列来组成异步的、基于事件的程序的库)。总之,rxjava让异步操作变得非常简单。RxJava
OkHttp: Square 公司开源的网络请求库。 OkHttp
基本使用
HTTP协议中共定义了八种方法或者叫“动作”来表明对Request-URI指定的资源的不同操作方式,分别是OPTIONS、HEAD、GET、POST、PUT、DELETE、TRACE、CONNECT,介绍就省略了。
虽然有这么多种请求方式,但App的接口请求无非就是数据的增删改查,增删改查对应到Restful风格接口上也就是post,delete,put,get四种请求。在Retrofit框架提供的注解中,其中HTTP注解有五个,用来修饰请求方法,除了post,delete,put,get还有一个head。除了这五种网络请求方式外,App无非还会用到retrofit的单文件或者多文件的上传下载,同步请求或者异步请求的处理,还有okhttp数据缓存的问题。
注解 | 请求方式 | 用途 |
---|---|---|
@Path | GET | 用于替换Url路径中的变量字符 |
@Query | GET | 用于拼接在Url路径后的查询参数,但相对于直接在Url后拼接,@Query则是添加一个变量 |
@QueryMap | GET | 效果等同于多个@Query 参数为Map类型 |
@FormUrlEncoded/@Field | POST | @FormUrlEncoded修饰表单域,每个表单域子件key-value采用@Field修饰 |
@Body | POST | 可以指定一个对象作为HTTP请求体@Body |
@FormUrlEncoded/@Field | PUT | @FormUrlEncoded修饰表单域,每个表单域子件key-value采用@Field修饰 |
@HTTP/@Body | DELETE | @HTTP修饰表单域,以指定一个对象作为HTTP请求体@Body,此时表单域一定要写上hasBody = true |
@Multipart/@Part或者@PartMap | POST | 文件上传使用,@Multipart修饰表单域,参数@Part修饰或者参数使用@PartMap |
举例说明
引入依赖
compile 'com.squareup.retrofit2:retrofit:2.2.0'
compile 'com.squareup.retrofit2:adapter-rxjava:2.2.0'
compile 'com.squareup.retrofit2:converter-gson:2.0.0-beta3'
compile 'com.squareup.okhttp3:logging-interceptor:3.4.1'
compile 'com.squareup.okhttp3:okhttp:3.4.1'
compile 'io.reactivex:rxjava:1.1.9'
compile 'io.reactivex:rxandroid:1.2.1'
创建Retrofit对象
public class RetrofitManager {
private static RetrofitManager mRetrofitManager;
private Retrofit mRetrofit;
private RetrofitManager(){
initRetrofit();
}
public static synchronized RetrofitManager getInstance(){
if (mRetrofitManager == null){
mRetrofitManager = new RetrofitManager();
}
return mRetrofitManager;
}
private void initRetrofit() {
HttpLoggingInterceptor LoginInterceptor = new HttpLoggingInterceptor();
LoginInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.addInterceptor(new RspCheckInterceptor());
if (AppConfig.DEBUG){
builder.addInterceptor(LoginInterceptor);
}
builder.connectTimeout(15, TimeUnit.SECONDS);
builder.readTimeout(20, TimeUnit.SECONDS);
builder.writeTimeout(20, TimeUnit.SECONDS);
builder.retryOnConnectionFailure(true);
OkHttpClient client = builder.build();
Gson gson = new GsonBuilder()
.setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
.setExclusionStrategies(new ExclusionStrategy() {
@Override
public boolean shouldSkipField(FieldAttributes f) {
return false;
}
@Override
public boolean shouldSkipClass(Class> clazz) {
return false;
}
})
.create();
mRetrofit = new Retrofit.Builder()
.baseUrl(AppConfig.BASE_URL)
.addConverterFactory(GsonConverterFactory.create(gson))
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.client(client)
.build();
}
public T createReq(Class reqServer){
return mRetrofit.create(reqServer);
}
}
创建访问API的请求
/**
* 添加标签
*
* @param supplier_id
* @param staff_id
* @param name
* @param type
* @return
*/
@FormUrlEncoded
@POST("memberservice/addMemberTag/suppliers/{supplier_id}/operator/{staff_id}")
Observable addMemberTag(@Path("supplier_id") String supplier_id,
@Path("staff_id") String staff_id,
@Field("name") String name, @Field("type") String type, @Field("timestamp") String timestamp);
/**
* 删除标签(注意一定要写hasBody = true,否则delete不能使用body参数请求)
*
* @param supplier_id
* @param operator_id
* @return
*/
@HTTP(method = "DELETE", path = "memberservice/delMemberTag/suppliers/{supplier_id}/operator/{operator_id}", hasBody = true)
Observable deletMemberTag(@Path("supplier_id") String supplier_id,
@Path("operator_id") String operator_id,
@Body RequestBody content);
/**
* 修改标签
*
* @param supplier_id
* @param operator_id
* @param map
* @return
*/
@FormUrlEncoded
@PUT("memberservice/updateMemberTag/suppliers/{supplier_id}/operator/{operator_id}")
Observable updateMemberTag(@Path("supplier_id") String supplier_id,
@Path("operator_id") String operator_id,
@FieldMap Map map);
/**
* 查询标签
*
* @param supplier_id
* @param staff_id
* @return
*/
@GET("memberservice/queryStaffTag/suppliers/{supplier_id}/operator/{staff_id}")
Observable> queryMemberTag(@Path("supplier_id") String supplier_id,
@Path("staff_id") String staff_id,
@Query("timestamp") String timestamp);
}
发送请求以及处理结果
RetrofitManager.getInstance().createReq(Api.class).addMemberTag(supply_id, staff_id, data, "2", getTime()).subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.io())
.subscribe(new Subscriber() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
//处理请求失败的操作
}
@Override
public void onNext(BaseResponse baseResponse) {
//处理成功后的结果
}
});
多图以及单图上传
方法一:
@Multipart
@POST("suppliers/{supplier_id}/staff/{staff_id}/meeting/{meeting_id}/upload")
Observable uploadePics(
@Path("supplier_id")String supplier_id,@Path("staff_id")String staff_id,
@Path("meeting_id")String meeting_id,@Part List partList);
MultipartBody.Builder builder = new MultipartBody.Builder()
.setType(MultipartBody.FORM)//表单类型
.addFormDataPart("summary_position", formatted_address); //地理位置
//单张或者多张
for (int i = 0; i < listString.size(); i++) {
File file = new File(listString.get(i));
RequestBody imageBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
builder.addFormDataPart("summary_pics[]", file.getName(), imageBody);
}
List parts = builder.build().parts();
RetrofitManager2.getInstance().createReq(PhpApi.class).uploadePics(
supplier_id,staff_id,meeting_id,parts)
.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.io())
.subscribe(new Subscriber() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
}
@Override
public void onNext(BaseResponse baseResponse) {
}
});
方法二:
Observable uploadePics2(
@Path("supplier_id")String supplier_id,@Path("staff_id")String staff_id,
@Path("meeting_id")String meeting_id,
@PartMap Map params);
//单张或多张图片
Listfiles = new ArrayList();
for (int i = 0; i < listString.size(); i++) {
File file = new File(listString.get(i));
files.add(file);
}
//组装partMap对象
Map partMap = new HashMap<>();
for(File file : files ){
RequestBody fileBody = RequestBody.create(MediaType.parse("image/*"), file);
partMap.put("summary_pics[]\"; filename=\""+file.getName()+"\"", fileBody);
}
RequestBody formatted_addressString = RequestBody.create(MediaType.parse("text/plain"), formatted_address);
partMap.put("summary_position" , formatted_addressString);
RetrofitManager2.getInstance().createReq(PhpApi.class).uploadePics2(
supplier_id,staff_id,meeting_id,partMap)
.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.io())
.subscribe(new Subscriber() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
}
@Override
public void onNext(BaseResponse baseResponse) {
}
});