Android 实现rxjava+retrofit封装网络层封装

我建议直接起飞,不用往下看了

一、引入相关的包

    api 'com.squareup.retrofit2:retrofit:2.5.0'
    api 'com.squareup.retrofit2:converter-scalars:2.5.0'
    api 'com.squareup.retrofit2:adapter-rxjava2:2.5.0'
    api 'com.squareup.retrofit2:converter-gson:2.5.0'
    api 'com.squareup.okhttp3:okhttp:3.12.8'
    api 'com.squareup.okhttp3:logging-interceptor:3.12.8'
    api 'io.reactivex.rxjava2:rxandroid:2.0.2'

二、创建api(service)

这是与服务器的对应的接口声明。IApi

public interface IApi {
    /**
     * 登录
     *
     * @param loginBody 请求体
     * @return Observable
     */
    @POST ( "/api/login" )
    Observable< BaseBean< LoginResult > > login ( @Body LoginBody loginBody );
}

三、接收数据

BaseBean是服务器约定严格按照某种格式返回的实体类,具体如下,可根据实际项目做出修整。

public class BaseBean< T > {
    private int code;
    private String message;
    private T data;
    //忽略get()和set()
}

T是泛型,可以接收任意类型的返回值。如

1.操作型,只返回code,message。

{
    "code": 200,
    "message": "提交成功"
}

那么IApi可以这么写

Observable< BaseBean< Void > > submit( @Body UserBody body );

2.当服务器直接返回列表的查询结果。如

{
    "code": 200,
    "message": "",
    "data": [
        {
            "name": "a",
            "age": 18
        },
        {
            "name": "b",
            "age": 19
        }
    ]
}

那么IApi可以这么写

Observable< BaseBean< List < User > > > query( @Body QueryBody body );

3.如果返回的data是分页数据,那么直接新建一个bean去接收就好了,这个比较简单就不说了。

四、开始封装

RetrofitManager.class

public class RetrofitManager {
    private IApi iApi;
    private static RetrofitManager instance;

    public static RetrofitManager getInstance ( ) {
        if ( instance == null ) {
            synchronized ( RetrofitManager.class ) {
                if ( instance == null ) {
                    instance = new RetrofitManager ( );
                }
            }
        }
        return instance;
    }

    private RetrofitManager ( ) {
        OkHttpClient.Builder okBuilder = new OkHttpClient.Builder ( );
        okBuilder.addInterceptor ( new MyLogInterceptor ( ) );
        Retrofit retrofit = new Retrofit.Builder ( )
                .client ( okBuilder.build ( ) )
                .baseUrl ( HttpApp.baseUrl )
                .addConverterFactory ( GsonConverterFactory.create ( ) )
                .addCallAdapterFactory ( RxJava2CallAdapterFactory.create ( ) )
                .build ( );
        iApi = retrofit.create ( IApi.class );
    }

    public IApi service ( ) {
        return iApi;
    }
}

接下来就可以整合起来了
HttpClient.class

ublic class HttpClient implements IApi {

    private IApi api;

    private HttpClient ( ) {
        api = RetrofitManager.getInstance ( ).service ( );
    }

    private static HttpClient instance;

    public static HttpClient getInstance ( ) {
        if ( instance == null ) {
            synchronized ( HttpClient.class ) {
                if ( instance == null ) {
                    instance = new HttpClient ( );
                }
            }
        }
        return instance;
    }

    @Override
    public Observable< BaseBean< LoginResult > > login ( LoginBody loginBody ) {
        return api.login ( loginBody ).subscribeOn ( Schedulers.io ( ) )
                .observeOn ( AndroidSchedulers.mainThread ( ) );
    }
}

五、开始使用

在你需要的地方,这样用

HttpModel.getInstance ( ).login ( loginBody ) .subscribe ( observer );

附:

1.定义一个BaseObserver为了方便给回调做简单的处理

public abstract class BaseObserver< T > extends ResourceObserver< T > {

    protected BaseObserver ( ) {
    }

    @Override
    public void onNext ( @NonNull T value ) {
        onAccept ( value, "" );
    }

    @Override
    public void onError ( Throwable e ) {
        if ( e instanceof HttpException ) {
            HttpException httpException = ( HttpException ) e;
            switch ( httpException.code ( ) ) {
                case UNAUTHORIZED:
                    onAccept ( null, "登录验证已过期" );
                    break;
                case INTERNAL_SERVER_ERROR:
                    onAccept ( null, "服务器错误" );
                    break;
                case FORBIDDEN:
                case NOT_FOUND:
                    onAccept ( null, "无效的请求" );
                    break;
                case REQUEST_TIMEOUT:
                case GATEWAY_TIMEOUT:
                case BAD_GATEWAY:
                case SERVICE_UNAVAILABLE:
                default:
                    onAccept ( null, httpException.getMessage ( ) );
                    break;
            }
        } else if ( e instanceof ConnectException ) {
            onAccept ( null, "网络连接异常,请检查您的网络状态" );
        } else if ( e instanceof SocketTimeoutException ) {
            onAccept ( null, "网络连接超时,请检查您的网络状态,稍后重试" );
        } else if ( e instanceof UnknownHostException ) {
            onAccept ( null, "网络异常,请检查您的网络状态" );
        } else if ( e instanceof JsonParseException
                || e instanceof JSONException
                || e instanceof ParseException ) {
            onAccept ( null, "数据解析错误" );
        } else if ( e instanceof SSLHandshakeException ) {
            onAccept ( null, "证书验证失败" );
        } else if ( e instanceof RuntimeException ) {
            onAccept ( null, "运行时异常" );
        } else {
            onAccept ( null, e.toString ( ) );
        }
    }

    @Override
    public void onComplete ( ) {
    }

    public abstract void onAccept ( T t, String error );

    public static final int UNAUTHORIZED = 401;
    private static final int FORBIDDEN = 403;
    private static final int NOT_FOUND = 404;
    private static final int REQUEST_TIMEOUT = 408;
    private static final int INTERNAL_SERVER_ERROR = 500;
    private static final int BAD_GATEWAY = 502;
    private static final int SERVICE_UNAVAILABLE = 503;
    private static final int GATEWAY_TIMEOUT = 504;
}

2.MyLogInterceptor 是一个监听类,可以不要的。但为了方便开发,还是建议加上。
MyLogInterceptor.class

public class MyLogInterceptor implements Interceptor {
    private static final Charset UTF8 = StandardCharsets.UTF_8;

    @Override
    public Response intercept ( Chain chain ) throws IOException {
        Request request = chain.request ( );
        RequestBody requestBody = request.body ( );
        String body = null;
        if ( requestBody != null ) {
            Buffer buffer = new Buffer ( );
            requestBody.writeTo ( buffer );
            Charset charset = UTF8;
            MediaType contentType = requestBody.contentType ( );
            if ( contentType != null ) {
                charset = contentType.charset ( UTF8 );
            }
            body = buffer.readString ( charset );
        }
        Response response = chain.proceed ( request );
        ResponseBody responseBody = response.body ( );
        String rBody;
        BufferedSource source = responseBody.source ( );
        source.request ( Long.MAX_VALUE );
        Buffer buffer = source.buffer ( );
        Charset charset = UTF8;
        MediaType contentType = responseBody.contentType ( );
        if ( contentType != null ) {
            try {
                charset = contentType.charset ( UTF8 );
            } catch ( UnsupportedCharsetException e ) {
                e.printStackTrace ( );
            }
        }
        rBody = buffer.clone ( ).readString ( charset );
        Logger.d ( "├─────────────────────────────────────────────────────────────────" );
        Logger.d ( "│【请求响应码】" + response.code ( ) );
        Logger.d ( "│【请求头】:" + request.headers ( ) );
        Logger.d ( "│【请求方法】:" + request.method ( ) );
        Logger.d ( "│【请求参数】:" + body );
        Logger.d ( "│【请求路径】:" + response.request ( ).url ( ) );
        Logger.d ( "│【请求回调】:" + rBody );
        Logger.d ( "├─────────────────────────────────────────────────────────────────" );
        return response;
    }
}

注:关于转换失败的问题,可参考
https://www.jianshu.com/p/d882ffae1853

你可能感兴趣的:(Android 实现rxjava+retrofit封装网络层封装)