Android开发Retrofit2+Rxjava2+okHttp 网络框架封装

释放双眼,带上耳机,听听看~!

说明

RxJava的概念其实很模糊,我对它的理解就是一个给你方便处理异步问题的框架,到底有多方便,体会过才知道。。。

Retrofit就是对okhttp做了一层封装。把网络请求都交给给了Okhttp,我们只需要通过简单的配置就能使用retrofit来进行网络请求了,Retrofit 除了提供了传统的 Callback 形式的 API,还有 RxJava 版本的 Observable 形式 API。

自己整理资料当做笔记

一,需要添加的依赖包

 compile 'io.reactivex.rxjava2:rxjava:2.0.7'
compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.squareup.retrofit2:adapter-rxjava2:2.2.0'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
compile 'com.squareup.okhttp3:okhttp:3.5.0'
compile 'com.squareup.okhttp3:logging-interceptor:3.6.0'
compile 'com.jakewharton:butterknife:8.4.0'

二、网上找了一个url练习具体是http://gank.io/api/data/福利/10/1

1.根据返回参数,封装一个基类baseresult (根据实际情况而定)

package com.example.rxjavaretrofit.base;
/**
* Created by fg on 2017/7/25.
* 解析实体基类
*/
public class BaseResult<T> {
private static int SUCCESS_CODE=000000;//成功的code
private int code;
private String message;
private T results;
private boolean error;
public boolean isSuccess(){
return getCode()==SUCCESS_CODE;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public T getResults() {
return results;
}
public void setResults(T results) {
this.results = results;
}
public boolean isError() {
return error;
}
public void setError(boolean error) {
this.error = error;
}
@Override
public String toString() {
return super.toString();
}
}

2.把result内的返回数据  通过GsonFormet工具生成实体类 Bean

package com.example.rxjavaretrofit.bean;
/**
* Created by 付刚 on 2018/7/25.
*/
public class MeiZiBean {
/**
* _id : 5b50107f421aa917a31c0565
* createdAt : 2018-07-19T12:15:59.226Z
* desc : 2018-07-19
* publishedAt : 2018-07-19T00:00:00.0Z
* source : web
* type : 福利
* url : https://ww1.sinaimg.cn/large/0065oQSqly1ftf1snjrjuj30se10r1kx.jpg
* used : true
* who : lijinshanmx
*/
private String _id;
private String createdAt;
private String desc;
private String publishedAt;
private String source;
private String type;
private String url;
private boolean used;
private String who;
public String get_id() {
return _id;
}
public void set_id(String _id) {
this._id = _id;
}
public String getCreatedAt() {
return createdAt;
}
public void setCreatedAt(String createdAt) {
this.createdAt = createdAt;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public String getPublishedAt() {
return publishedAt;
}
public void setPublishedAt(String publishedAt) {
this.publishedAt = publishedAt;
}
public String getSource() {
return source;
}
public void setSource(String source) {
this.source = source;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public boolean isUsed() {
return used;
}
public void setUsed(boolean used) {
this.used = used;
}
public String getWho() {
return who;
}
public void setWho(String who) {
this.who = who;
}
}

如果需要实现序列化进行传值 可实现Parcelable或者Serializable接口

3.接下来我们就要开始接口了哦,回到我们上面的ApiService接口模拟一个API接口getMeiZi() 可以返回得到一个observable<Object>。

package com.example.rxjavaretrofit.network;
import com.example.rxjavaretrofit.base.BaseResult;
import com.example.rxjavaretrofit.bean.MeiZiBean;
import java.util.List;
import io.reactivex.Observable;
import retrofit2.http.GET;
/**
* Created by fg on 2018/7/25.
*/
public interface ApiServise {
//网络请求超时时间 单位毫秒
int DEFAULT_TIMEOUT=20000;
String HOST = "http://gank.io/";
String API_SERVER_URL = HOST + "api/data/";
@GET("福利/10/1")
Observable<BaseResult<List<MeiZiBean>>> getMeiZi();
}

4.就是对自己的Retrofit去进行封装设置

(1)retrofit是基于okhttp的加强版,所以第一步去自定义一个okhttpclient,里面设置两个拦截器,一个是日志拦截器,用于对日志的筛选,还一个就是网络拦截器,用于对网络请求头的总体设置。

package com.example.rxjavaretrofit.Utils;
import android.util.Log;
import java.io.IOException;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;
/**
* Created by fg on 2018/7/25.
* *@description 拦截器工具类
*/
public class InterceptorUtil {
public static String TAG="----";
//日志拦截器
public static HttpLoggingInterceptor LogInterceptor(){
return new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
@Override
public void log(String message) {
Log.w(TAG, "log: "+message );
}
}).setLevel(HttpLoggingInterceptor.Level.BODY);//设置打印数据的级别
}
public static Interceptor HeaderInterceptor(){
return   new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request mRequest=chain.request();
//在这里你可以做一些想做的事,比如token失效时,重新获取token
//或者添加header等等,PS我会在下一篇文章总写拦截token方法
return chain.proceed(mRequest);
}
};
}
}

(2)创建一个观察者基类,在其中进行对请求错误的封装和对进度条显示与消失的封装。

package com.example.rxjavaretrofit.base;
import android.accounts.NetworkErrorException;
import android.content.Context;
import com.example.rxjavaretrofit.widget.ProgressDialog;
import java.net.ConnectException;
import java.net.UnknownHostException;
import java.util.concurrent.TimeoutException;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
/**
* Created by fg on 2018/7/25.
* Observer的封装
*/
public abstract class BaseObserver<T> implements Observer<BaseResult<T>> {
protected Context mContext;
public BaseObserver(Context cxt) {
this.mContext = cxt;
}
public BaseObserver() {
}
@Override
public void onSubscribe(Disposable d) {
onRequestStart();
}
@Override
public void onNext(BaseResult<T> tBaseEntity) {
onRequestEnd();
if (tBaseEntity.isSuccess()) {
try {
onSuccees(tBaseEntity);
} catch (Exception e) {
e.printStackTrace();
}
} else {
try {
onCodeError(tBaseEntity);
} catch (Exception e) {
e.printStackTrace();
}
}
}
@Override
public void onError(Throwable e) {
//        Log.w(TAG, "onError: ", );这里可以打印错误信息
onRequestEnd();
try {
if (e instanceof ConnectException
|| e instanceof TimeoutException
|| e instanceof NetworkErrorException
|| e instanceof UnknownHostException) {
onFailure(e, true);
} else {
onFailure(e, false);
}
} catch (Exception e1) {
e1.printStackTrace();
}
}
@Override
public void onComplete() {
}
/**
* 返回成功
*
* @param t
* @throws Exception
*/
protected abstract void onSuccees(BaseResult<T> t) throws Exception;
/**
* 返回成功了,但是code错误
*
* @param t
* @throws Exception
*/
protected void onCodeError(BaseResult<T> t) throws Exception {
}
/**
* 返回失败
*
* @param e
* @param isNetWorkError 是否是网络错误
* @throws Exception
*/
protected abstract void onFailure(Throwable e, boolean isNetWorkError) throws Exception;
protected void onRequestStart() {
}
protected void onRequestEnd() {
closeProgressDialog();
}
public void showProgressDialog() {
ProgressDialog.show(mContext, false, "请稍后");
}
public void closeProgressDialog() {
ProgressDialog.cancle();
}
}

(3)然后要创建Retrofit实例,构建一个请求的基类

package com.example.rxjavaretrofit.base;
import com.example.rxjavaretrofit.Utils.InterceptorUtil;
import com.example.rxjavaretrofit.network.ApiServise;
import java.util.concurrent.TimeUnit;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
/**
* Created by fg on 2018/7/25.
*/
public class BaseRequest {
//retrofit底层用的okHttp,所以设置超时还需要okHttp
//然后设置5秒超时
//其中DEFAULT_TIMEOUT是我这边定义的一个常量
//TimeUnit为java.util.concurrent包下的时间单位
//TimeUnit.SECONDS这里为秒的单位
OkHttpClient client=new OkHttpClient.Builder()
.connectTimeout(20, TimeUnit.SECONDS)//设置连接超时时间
.readTimeout(20, TimeUnit.SECONDS)//设置读取超时时间
.writeTimeout(20, TimeUnit.SECONDS)//设置写入超时时间
.addInterceptor(InterceptorUtil.HeaderInterceptor())//添加其他拦截器
.addInterceptor(InterceptorUtil.LogInterceptor())//添加日志拦截器
.build();
Retrofit retrofit = new Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create()) //添加Gson转换器
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())//添加Rx转换器
.baseUrl(ApiServise.API_SERVER_URL) //baseurl
.client(client)
.build();
public ApiServise apiServise = retrofit.create(ApiServise.class);
private static BaseRequest instance;
public static synchronized BaseRequest getInstance(){
if(instance == null)
instance = new BaseRequest();
return instance;
}
public ApiServise getService() {
return apiServise;
}
}

OK这时候就可以跑通了让我们看看完整的请求 (observable(被观察者)和observer(观察者)相订阅)

public void getData() {
BaseRequest.getInstance().getService()
.getMeiZi()
.subscribeOn(Schedulers.newThread())
.subscribe(new BaseObserver<List<MeiZiBean>>(this) {
@Override
protected void onSuccees(BaseResult<List<MeiZiBean>> t) throws Exception {
List<MeiZiBean> results = t.getResults();
Log.e("fff",results.size()+"");
}
@Override
protected void onFailure(Throwable e, boolean isNetWorkError) throws Exception {
Log.e("fff",e.toString());
}
});
}

那么到这里,封装基本上大功告成了,但是上面可以成功解析。后续数据处理就不做叙述了,

项目下载链接:https://download.csdn.net/download/fugang1230/10563945

人已赞赏
Android文章

android 打印线程信息

2020-3-14 0:01:44

Android文章

Android开发项目中关于Retrofit2.0+RxJava+OkHttp的封装和使用

2020-3-14 17:06:08

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
有新消息 消息中心
搜索