Mvp + retrofit + rxjava, invalid fitrxjava
Reference
"Scaling fit": "com. squareup. using fit2: Using Fit: 2.0.1 "," using fit-adapter ":" com. squareup. using fit2: adapter-rxjava: 2.0.1 "," using fit-converter ":" com. squareup. retrofit2: converter-gson: 2.0.1 "," gson ":" com. google. code. gson: 2.6.2 "," rxjava ":" io. reactivex: rxjava: 1.1.2 "," rxandroid ":" io. reactivex: rxandroid: 1.1.0 "," okhttp ":" com. squareup. okhttp3: okhttp: 3.2.0 "," okhttp-urlconnection ":" com. squareup. okhttp3: okhttp-urlconnection: 3.2.0 "," okhttp-logging ":" com. squareup. okhttp3: logging-interceptor: 3.2.0 "," okhttp-cookie ":" com. github. franmontiel: PersistentCookieJar: v0.9.3 ",View Code
Retrofit
Import com. google. gson. gson; import com. google. gson. gsonBuilder; import java. util. concurrent. timeUnit; import okhttp3.OkHttpClient; import okhttp3.logging. httpLoggingInterceptor; import using fit2.20.fit; import using fit2.adapter. rxjava. rxJavaCallAdapterFactory; import into fit2.converter. gson. gsonConverterFactory;/** used to assist in initializing retrofit **/public class RxService {private static final long DEFAULT_TIMEOUT = 2 0L; final static Gson gson = new GsonBuilder (). setDateFormat ("yyyy-MM-dd't'hh: mm: ss. SSS 'Z '"). serializeNulls () // call the serializeNulls method to change the default behavior of the gson object. The null value is output. create (); // addInterceptor: Set the application interceptor, which can be used to set public parameters, header information, log interception, etc. // addNetworkInterceptor: Network interceptor, which can be used to retry or rewrite the application interceptor, corresponds to setRequestInterceptor in 1.9. // SetLevel NONE (not recorded) BASIC (Request/response line) HEADER (Request/response line + HEADER) BODY (Request/response line + HEADER + BODY) // cookieJar: in the same session // TimeUnit. SECONDS do unit private static OkHttpClient okHttpClient = new OkHttpClient. builder (). addNetworkInterceptor (new HttpLoggingInterceptor (). setLevel (HttpLoggingInterceptor. level. HEADERS )). addInterceptor (new HttpLoggingInterceptor (). setLevel (HttpLoggingInterceptor. level. BODY )). cookieJar (App. getInstance (). getCookieJar ()). connectTimeout (DEFAULT_TIMEOUT, TimeUnit. SECONDS ). readTimeout (DEFAULT_TIMEOUT, TimeUnit. SECONDS ). build (); private static dynamic fit = null; private RxService () {}// private final static RxService rxService = new RxService (); public static <T> T createApi (Class <T> clazz, String url) {Variable fit = new variable fit. builder (). baseUrl (url ). client (okHttpClient ). addCallAdapterFactory (RxJavaCallAdapterFactory. create ()). addConverterFactory (GsonConverterFactory. create (gson )). build (); return response fit. create (clazz);} public static <T> T createApi (Class <T> clazz) {Dynamic Fit = new Dynamic Fit. builder (). baseUrl (Config. BASE_DEFAULT ). client (okHttpClient ). addCallAdapterFactory (RxJavaCallAdapterFactory. create ()). addConverterFactory (GsonConverterFactory. create (gson )). build (); return response fit. create (clazz );}}View Codeimport using fit2.http. field; import into fit2.http. formUrlEncoded; import into fit2.http. POST; import rx. observable;/*** Created by JDD on 2016/4/8. */public interface ILoginService {@ FormUrlEncoded @ POST (LoginSingleService. URL) Observable <UsersEntity> login (@ Field ("token") String token, @ Field ("userName") String userName, @ Field ("userPassword") String userPassword );}View Code/** create and configure your fit service **/public class LoginSingleService {public static final String URL = "login. do "; protected static final Object monitor = new Object (); static ILoginService sJokeSingleton = null; // public static final int meizhiSize = 10; // public static final int gankSize = 5; // Test service public static ILoginService getLoginSingleton () {synchronized (monitor) {if (sJokeSingleton = null) {sJokeSingleton = RxService. createApi (ILoginService. class) ;}return sJokeSingleton ;}}}View Code
Presenter
Import android. util. log; import rx. observer; import rx. subtasks; import rx. android. schedulers. androidSchedulers; import rx. schedulers. schedulers; public class LoginPresenter implements ILoginPresenter {public subscribe login (final String username, final String pwd, final CallBack callBack) {if (username = null | pwd = null | username. equals ("") | pwd. equals ("") {callBack. error (STATUS. NULL _ ERROR); return null;} // observer subscribe s = LoginSingleService. getLoginSingleton () // event source, Observable by the observer. login ("21341234", username, pwd ). subscribeOn (Schedulers. io () // specifies the thread that the observer runs. map (entity-> entity. getData ()). observeOn (AndroidSchedulers. mainThread () // specifies the thread that the subscriber runs. subscribe (new Observer <UserEntity> () {// subscriber @ Override public void onCompleted () {Log. e ("onNext", "onCompleted "); CallBack. end () ;}@ Override public void onError (Throwable e) {Log. e ("onNext", "onError" + e. getMessage (); callBack. error (STATUS. ERROR);} @ Override public void onNext (UserEntity entity) {STATUS status; if (entity! = Null) {status = STATUS. SUCCESS; Log. e ("onNext", "hello" + entity. getName (); App. getInstance (). setUserEntity (entity); App. getInstance (). setPerson (entity, pwd);} else status = STATUS. ERROR; callBack. success (status) ;}}); return s ;}}View Code
Main
Import android. content. context; import android. OS. bundle; import android. support. annotation. nullable; import android. support. v7.app. appCompatActivity; import rx. subtasks; import rx. subscriptions. compositeSubscription;/*** Created by LiuZhen on. */public abstract class BaseActivity extends AppCompatActivity {private final static String TAG = "BaseActivity"; private compositesubw.m Compositesubject; protected Context context; @ Override protected void onCreate (@ Nullable Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (getLayoutId (); this. context = BaseActivity. this;} public compositesubw.getcompositesub.pdf () {if (this. mcompositesub.pdf = null) {this. mcompositesubtasks = new compositesubtasks ();} return this. MCompositeSubscription;}/** enable CompositeSubscription to hold the Subscription **/public void addSubscription (Subscription s) {if (this. mcompositesub.pdf = null) {this. mcompositesubtasks = new compositesubtasks ();} this. mcompositesub.pdf. add (s) ;}@ Override protected void onDestroy () {super. onDestroy (); if (this. mcompositesub.pdf! = Null) {this. mcompositesub.pdf. unsubscribe (); // cancel all subscriptions} protected abstract int getLayoutId ();}View Codeimport android. OS. bundle; import android. widget. textView; import org. greenrobot. eventbus. eventBus; import org. greenrobot. eventbus. subscribe; import org. greenrobot. eventbus. threadMode; public class MainActivity extends BaseActivity {private TextView username; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); EventBus. getDefault (). register (this); username = (TextView) findViewById (R. id. username); username. setText (App. getInstance (). getPerson (). getName () + "hello") ;}@ Override protected int getLayoutId () {return R. layout. activity_main;} @ Subscribe (threadMode = ThreadMode. MAIN) public void onEvent () {Toasts. showShort ("Refresh UI", context) ;}@ Override protected void onDestroy () {super. onDestroy (); EventBus. getDefault (). unregister (this );}}View Code