Rxjava+Retrofit使用記錄

  1. 關於函數在哪一個線程執行: observeOn(AndroidSchedulers.mainThread())這個指定回調線程的 操做最好是放在最後的.subscribe(new Subscriber<String>以前。否則observeOn(AndroidSchedulers.mainThread())以後的map或者doOnNext()之類的函數都會在指定的mainThread中執行。java

  2. gradle配置 compile 'io.reactivex:rxandroid:1.2.1' compile 'io.reactivex:rxjava:1.1.6' compile 'com.squareup.retrofit2:retrofit:2.1.0' compile 'com.squareup.retrofit2:converter-gson:2.1.0' compile 'com.squareup.retrofit2:converter-scalars:2.1.0'//ConverterFactory的String依賴包 compile 'com.squareup.retrofit2:adapter-rxjava:2.1.0' public static final Retrofit retrofit=new Retrofit.Builder() .baseUrl(appUrlPay) .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) //添加對字符串的轉換,否則返回字符串,沒法解析成Gson時會出錯 .addConverterFactory(ScalarsConverterFactory.create()) .addConverterFactory(GsonConverterFactory.create(GsonUtil.getGsonInstance())) .client(okHttpClient) .build();react

  3. 關於onError: subscribe(new Action1<String>() 在請求網絡onError的時候,app會閃退 subscribe(new Subscriber<String>()由於實現了onError方法,因此會走該方法而不會閃退。android

    當onError的時候,doOnNext()函數不會執行網絡

相關文章
相關標籤/搜索