1.封裝OkHttpjava
public class HttpClient { private static HttpClient instance; private OkHttpClient.Builder builder; private HttpClient() { builder = new OkHttpClient.Builder() .addInterceptor(new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() { @Override public void log(String message) { if (BuildConfig.DEBUG) { Log.v("rretrofitLog==>>", message); } } }).setLevel(HttpLoggingInterceptor.Level.BODY)) .writeTimeout(30, TimeUnit.SECONDS) .connectTimeout(5, TimeUnit.SECONDS); } public static HttpClient getInstance() { if (instance == null) { synchronized (HttpClient.class) { if (instance == null) { instance = new HttpClient(); } } } return instance; } public OkHttpClient.Builder getBuilder() { return builder; } }
2.封裝Retrofitreact
public class RetrofitClient { private static RetrofitClient instance; private Retrofit.Builder mRetrofitBuilder; private OkHttpClient.Builder mOkhttpBuilder; private RetrofitClient() { mOkhttpBuilder = HttpClient.getInstance().getBuilder(); mRetrofitBuilder = new Retrofit.Builder() .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) .addConverterFactory(GsonConverterFactory.create()) .client(mOkhttpBuilder.build()); } public static RetrofitClient getInstance() { if (instance == null) { synchronized (RetrofitClient.class) { if (instance == null) { instance = new RetrofitClient(); } } } return instance; } public static String BaseURL = "接口的ip地址"; public Retrofit getRetrofit() { return mRetrofitBuilder .baseUrl(BaseURL) .client(mOkhttpBuilder.build()) .build(); } }
3.封裝BaseResponse(根據本身的需求)android
public class BaseResponse { private int code; private String msg; public int getCode() { return code; } public void setCode(int code) { this.code = code; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } }
4.封裝BaseObserver網絡
public abstract class BaseObserver<T extends BaseResponse> implements Observer<T>, ISubscriber<T> { private Toast mToast; @Override public void onSubscribe(Disposable d) { doOnSubscribe(d); } @Override public void onNext(T t) { doOnNext(t); } @Override public void onError(Throwable e) { if (e instanceof SocketTimeoutException) { setError("SocketTimeoutException"); } else if (e instanceof ConnectException) { setError("ConnectException"); } else if (e instanceof UnknownHostException) { setError("UnknownHostException"); } else { String error = e.getMessage(); doOnError(error); } } private void setError(String s) { showToast(s); doOnError(s); doOnNetError(); } private void doOnNetError() { //彈框代表網絡有問題 } @Override public void onComplete() { doOnComplete(); } private void showToast(String s) { if (mToast == null) { mToast = Toast.makeText(MyApp.getAppContext(), s, Toast.LENGTH_SHORT); } else { mToast.setText(s); } mToast.show(); } }
public interface ISubscriber<T extends BaseResponse> { void doOnSubscribe(Disposable d); void doOnError(String errorMsg); void doOnNext(T t); void doOnComplete(); }
5.封裝接口ide
public interface ApiService { ApiService instance = RetrofitClient.getInstance().getRetrofit().create(ApiService.class); @POST("/user/login") Observable<LoginBean> login(@Query("username") String name, @Query("password") String pwd); }
在此以前,添加所須要的依賴學習
implementation 'io.reactivex.rxjava2:rxjava:2.2.4' implementation 'io.reactivex.rxjava2:rxandroid:2.1.0' implementation 'com.squareup.retrofit2:retrofit:2.5.0' implementation 'com.squareup.retrofit2:converter-gson:2.1.0' implementation 'com.squareup.retrofit2:adapter-rxjava2:2.2.0' implementation 'com.squareup.okhttp3:logging-interceptor:3.4.1'//日誌攔截器
測試效果測試
ApiService.instance .login(name, pwd) .subscribeOn(Schedulers.newThread())//上游線程 .observeOn(AndroidSchedulers.mainThread())//下游線程 .subscribe(new BaseObserver<LoginBean>() { @Override public void doOnSubscribe(Disposable d) { } @Override public void doOnNext(LoginBean baseResponse) { int id = baseResponse.getData().getId(); Log.v("xd---", "" + id); } @Override public void doOnError(String errorMsg) { } @Override public void doOnComplete() { } });
本章是在學習 靈動_lee Rxjava2以後所總結,如若更多細節,可查閱 靈動_lee更多內容ui