现在Android开发中最潮流的网络的搭建过程,结构清晰易懂。在这里分享一下搭建的过程.
首先不用多说,肯定是先建立依赖:
//RxJava
compile 'io.reactivex:rxjava:1.1.3'
//RxAndroid
compile 'io.reactivex:rxandroid:1.1.0'
//retrofit
compile 'com.squareup.retrofit2:retrofit:2.0.0'
//retrofit依赖Gson
compile 'com.squareup.retrofit2:converter-gson:2.0.0'
//OkHttp
compile 'com.squareup.okhttp3:okhttp:3.2.0'
//retrofit依赖RxJava
compile 'com.squareup.retrofit2:adapter-rxjava:2.0.0'
Retrofit,OkHttp,RxJava的使用在这里就不多说了,如果不知道可以先去了解一下在来看这篇blog,在这里我使用MVP架构,是使用功能的不同来分包,下图是项目结构图:
然后的有网络访问的服务,先看看Retrofit服务类,设计了一个线程安全的单例模式,增加RxJava和Gson的支持(增加支持必须要有的converter-gson和adapter-rxjava库):
package com.dhz.presentation.common.api;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
/**
* 服务器Retrofit
* Created by Jelly on 2016/8/12.
*/
public class ApiService {
public static final String ServerPath = "服务器地址"; //服务器接口地址
private static class Instance{
private static Retrofit retrofit = createRetrofit();
}
public static Retrofit getInstance(){
return Instance.retrofit;
}
private static Retrofit createRetrofit(){
if(Instance.retrofit != null) return Instance.retrofit;
Retrofit retrofit = new Retrofit.Builder()
.client(new OkHttpClient()) //OKHttp支持
.baseUrl(ServerPath)
.addCallAdapterFactory(RxJavaCallAdapterFactory.create()) //RxJava支持
.addConverterFactory(GsonConverterFactory.create()) //Gson支持
.build();
return retrofit;
}
}
定义返回的Gson对象,在这里推荐使用Android Studio的插件GsonFormat去生成:
package com.dhz.presentation.cost.bean;
import java.util.List;
/**
*
* Created by Jelly on 2016/8/22.
*/
public class CostList {
/**
* interfaceName : orders_get
* returnStatus : 100
* isSuccess : Y
* data : [{"serial":"1","OrderNo":"O-20160812-00001","InsurancePos":"科技园","LicensePlate":"粤B12345","CreateDate":"2016-08-12 14:51:00","InsuranceDesc":"老司机翻车了","Icon":"http://g.hiphotos.baidu.com/baike/w%3D268/sign=79e453e07c1ed21b79c929e3956fddae/faedab64034f78f0094658e178310a55b3191c57.jpg","OrderStatus":"1"}]
*/
private String interfaceName;
private String returnStatus;
private String isSuccess;
/**
* serial : 1
* OrderNo : O-20160812-00001
* InsurancePos : 科技园
* LicensePlate : 粤B12345
* CreateDate : 2016-08-12 14:51:00
* InsuranceDesc : 老司机翻车了
* Icon : http://g.hiphotos.baidu.com/baike/w%3D268/sign=79e453e07c1ed21b79c929e3956fddae/faedab64034f78f0094658e178310a55b3191c57.jpg
* OrderStatus : 1
*/
private List<DataEntity> data;
public String getInterfaceName() {
return interfaceName;
}
public void setInterfaceName(String interfaceName) {
this.interfaceName = interfaceName;
}
public String getReturnStatus() {
return returnStatus;
}
public void setReturnStatus(String returnStatus) {
this.returnStatus = returnStatus;
}
public String getIsSuccess() {
return isSuccess;
}
public void setIsSuccess(String isSuccess) {
this.isSuccess = isSuccess;
}
public List<DataEntity> getData() {
return data;
}
public void setData(List<DataEntity> data) {
this.data = data;
}
public static class DataEntity {
private String serial;
private String OrderNo;
private String InsurancePos;
private String LicensePlate;
private String CreateDate;
private String InsuranceDesc;
private String Icon;
private String OrderStatus;
public String getSerial() {
return serial;
}
public void setSerial(String serial) {
this.serial = serial;
}
public String getOrderNo() {
return OrderNo;
}
public void setOrderNo(String OrderNo) {
this.OrderNo = OrderNo;
}
public String getInsurancePos() {
return InsurancePos;
}
public void setInsurancePos(String InsurancePos) {
this.InsurancePos = InsurancePos;
}
public String getLicensePlate() {
return LicensePlate;
}
public void setLicensePlate(String LicensePlate) {
this.LicensePlate = LicensePlate;
}
public String getCreateDate() {
return CreateDate;
}
public void setCreateDate(String CreateDate) {
this.CreateDate = CreateDate;
}
public String getInsuranceDesc() {
return InsuranceDesc;
}
public void setInsuranceDesc(String InsuranceDesc) {
this.InsuranceDesc = InsuranceDesc;
}
public String getIcon() {
return Icon;
}
public void setIcon(String Icon) {
this.Icon = Icon;
}
public String getOrderStatus() {
return OrderStatus;
}
public void setOrderStatus(String OrderStatus) {
this.OrderStatus = OrderStatus;
}
}
}
建立Api接口,这里采用Map作为参数,指定Json对象作为泛型参数:
package com.dhz.presentation.cost.api;
import com.dhz.presentation.cost.bean.CostList;
import com.dhz.presentation.cost.bean.CostOrderInfo;
import java.util.Map;
import retrofit2.http.POST;
import retrofit2.http.QueryMap;
import rx.Observable;
/**
* Created by Jelly on 2016/8/22.
*/
public interface CostApi {
@POST("iam_app_cnt_api.do")
Observable<CostOrderInfo> CostOrderInfo(@QueryMap Map<String,String> map);
}
调用方式,在调用的时候converter-gson会自动的把String转换成Json对象,通过RxJava的线程控制指定请求发生在子线程,回调发生在主线程。如果团队支持的话还可以加入lambda,这样代码看起来更简洁:
ApiService.getInstance().create(CostApi.class).CostOrderInfo(Map参数)
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Action1<CostOrderInfo>() {
@Override
public void call(CostOrderInfo costOrderInfo) {
//请求成功
}
}, new Action1<Throwable>() {
@Override
public void call(Throwable throwable) {
//在这里处理请求失败的情况
}
});
Retrofit不仅仅支持Json格式,还支持下面几种数据解析方式:
Gson: com.squareup.retrofit2:converter-gson
Jackson: com.squareup.retrofit2:converter-jackson
Moshi: com.squareup.retrofit2:converter-moshi
Protobuf: com.squareup.retrofit2:converter-protobuf
Wire: com.squareup.retrofit2:converter-wire
Simple XML: com.squareup.retrofit2:converter-simplexml
Scalars (primitives, boxed, and String): com.squareup.retrofit2:converter-scalars
由于Retrofit非常强大的解耦性和扩展性加上RxJava强大的功能以及OkHttp强大的网络请求功能,这套网络请求的框架是非常实用的,搭建过程也是十分简单。