1. 程式人生 > >Retrofit 和 rxjava2的簡單使用和封裝utils 和日誌攔截器

Retrofit 和 rxjava2的簡單使用和封裝utils 和日誌攔截器

依賴*********注意網路許可權

//rxjava依賴     implementation 'io.reactivex.rxjava2:rxandroid:2.0.2'     implementation 'io.reactivex.rxjava2:rxjava:2.0.2' //retrofit依賴     implementation 'com.squareup.retrofit2:retrofit:2.0.2'     implementation 'com.squareup.retrofit2:adapter-rxjava2:2.3.0'     implementation 'com.squareup.retrofit2:converter-gson:2.0.2' Bean包存放地址中的資料後建立Api介面

public interface Api {     @FormUrlEncoded     @POST("user/login")     Observable<User> login(@Field("mobile")String mobile,@Field("password")String password); } 建立Contance類和Api拼接

public class Contance {     public final static String URL_Post = "https://www.zhaoapi.cn"; } MainActivity中呼叫封裝工具類並且列印

public class MainActivity extends AppCompatActivity {     @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);         Observable<User> login = HttpUtils.getHttpUtilsInstance().apiClient.login("18611520300", "123123");         login.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<User>() {             @Override             public void onSubscribe(Disposable d) {             }             @Override             public void onNext(User user) {                 Log.i("aaa","+++++++"+user.getData().getMobile());             }             @Override             public void onError(Throwable e) {             }             @Override             public void onComplete() {             }         });     } } 封裝utils

public class HttpUtils {     public final Api apiClient;       private HttpUtils() {         Retrofit retrofit = new Retrofit.Builder().baseUrl(Contance.URL_Post)                 .addConverterFactory(GsonConverterFactory.create())                 .addCallAdapterFactory(RxJava2CallAdapterFactory.create())                 .build();         apiClient = retrofit.create(Api.class);     }       private static class GetsHttpUtilsInstance {         private static HttpUtils httpUtils = new HttpUtils();     }       public static HttpUtils getHttpUtilsInstance(){         return GetsHttpUtilsInstance.httpUtils;     }   } 新增攔截器,直接在封裝類中寫內部類,並且在HttpUtils方法中呼叫

 private HttpUtils() {         //攔截器         OkHttpClient okHttpClient = new OkHttpClient.Builder().addNetworkInterceptor(new LoggingInterceptor()).build();         Retrofit retrofit = new Retrofit.Builder().baseUrl(Contance.URL_Post)                 .addConverterFactory(GsonConverterFactory.create())                 .addCallAdapterFactory(RxJava2CallAdapterFactory.create())                 .client(okHttpClient)                 .build();         apiClient = retrofit.create(Api.class);     }       class LoggingInterceptor implements Interceptor{           @Override         public Response intercept(Chain chain) throws IOException {             Request request = chain.request();               long t1 = System.nanoTime();             //logger.info(String.format("Sending request %s on %s%n%s",             //       request.url(), chain.connection(), request.headers()));               Response response = chain.proceed(request);               long t2 = System.nanoTime(); //            logger.info(String.format("Received response for %s in %.1fms%n%s", //                    response.request().url(), (t2 - t1) / 1e6d, response.headers()));               return response;         }     }