/** * 作者:create by YW * 日期:2018.01.04 15:14 * 描述: */ public class RetrofitFactory { private static final String TAG = "YW"; public static final String BASE_URL = "http://api.ihuxin.net/"; private static final long TIMEOUT = 30; private static RetrofitFactory ourInstance; private RetrofitService mRetrofitService; public static RetrofitFactory newInstance() { if (ourInstance == null) { synchronized (RetrofitFactory.class) { if (ourInstance == null) { ourInstance = new RetrofitFactory(); } } } return ourInstance; } private RetrofitFactory() { OkHttpClient httpClient = new OkHttpClient.Builder() .addInterceptor(HEADER) //header .addInterceptor(new LoggingInterceptor()) //logging .connectTimeout(TIMEOUT, TimeUnit.SECONDS) .readTimeout(TIMEOUT, TimeUnit.SECONDS) .build(); mRetrofitService = new Retrofit.Builder() .baseUrl(BASE_URL) .addConverterFactory(GsonConverterFactory.create(gson)) //Gson转换器 .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) //添加Retrofit到RxJava的转换器 .client(httpClient) .build() .create(RetrofitService.class); } public RetrofitService getRetrofitService() { return mRetrofitService; } }/** * 作者:create by YW * 日期:2018.01.03 15:06 * 描述:V : view * P : presenter * M : model */ public abstract class BaseActivity<V, P extends BasePresenter<V>> extends AppCompatActivity { protected P mPresenter; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 创建绑定的Presenter mPresenter = createPresenter(); if (mPresenter != null) { mPresenter.attachView((V) this); } // 绑定子类布局 setContentView(provideContentViewId()); initView(); initData(); initListener(); } @Override protected void onResume() { super.onResume(); if (mPresenter != null) { mPresenter.onResume(); } } @Override protected void onDestroy() { super.onDestroy(); // 解绑View与Presenter if (mPresenter != null) { mPresenter.detachView(); } } protected void initView() { } protected void initData() { } protected void initListener() { } /** * @return xml布局Id */ protected abstract int provideContentViewId(); /** * @return P : Activity绑定Presenter */ public abstract P createPresenter(); }/** * 作者:create by YW * 日期:2018.01.03 15:11 * 描述:P */ public abstract class BasePresenter<V> { private BaseActivity mContext; public BasePresenter(BaseActivity context) { mContext = context; } protected Reference<V> mViewRef; /** * @param view 绑定View(Activity) */ public void attachView(V view) { mViewRef = new WeakReference<V>(view); } /** * 在Base的onResume调 并回调到Presenter处理 */ public abstract void onResume(); /** * 解绑View */ public void detachView() { if (mViewRef != null) { mViewRef.clear(); mViewRef = null; } } /** * @return 获取绑定的View */ public V getView() { return mViewRef != null ? mViewRef.get() : null; } } ----------------------------------------------------------/** * 作者:create by YW * 日期:2018.01.03 16:12 * 描述: */ public class MainActivity extends BaseActivity<IMainAtView, MainAtPresenter> implements IMainAtView { TextView tv; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @Override protected int provideContentViewId() { return R.layout.activity_main; } @Override public MainAtPresenter createPresenter() { return new MainAtPresenter(this); } @Override public TextView getText() { return tv; } @Override protected void initView() { tv = (TextView) findViewById(R.id.tv); tv.setOnClickListener(v -> mPresenter.userInfo("13250013344")); } }/** * 作者:create by YW * 日期:2018.01.03 15:43 * 描述:P -> 在BasePresenter绑定View */ public class MainAtPresenter extends BasePresenter<IMainAtView> { public MainAtPresenter(BaseActivity context) { super(context); } @Override public void onResume() { } public void userInfo(String phone) { Map<String, String> map = new HashMap(); map.put("v", "5"); RetrofitFactory.newInstance().getRetrofitService() .userInfo(phone, map) .subscribeOn(Schedulers.io()) //订阅在io .observeOn(AndroidSchedulers.mainThread()) .subscribe(userInfo1 -> { Log.e("YW", userInfo1.toString()); getView().getText().setText(userInfo1.toString()); }, this::loginError); } private void loginError(Throwable throwable) { Log.e("YW", throwable.getLocalizedMessage()); } }public class UserInfo { protected String s; protected String m; private DBean d; public String getS() { return s; } public void setS(String s) { this.s = s; } public String getM() { return m; } public void setM(String m) { this.m = m; } public DBean getD() { return d; } public void setD(DBean d) { this.d = d; } public static class DBean { private String sex; private String nname; private String msisdn; private String iconUrl; private String showType; private String type; private String version; public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getNname() { return nname; } public void setNname(String nname) { this.nname = nname; } public String getMsisdn() { return msisdn; } public void setMsisdn(String msisdn) { this.msisdn = msisdn; } public String getIconUrl() { return iconUrl; } public void setIconUrl(String iconUrl) { this.iconUrl = iconUrl; } public String getShowType() { return showType; } public void setShowType(String showType) { this.showType = showType; } public String getType() { return type; } public void setType(String type) { this.type = type; } public String getVersion() { return version; } public void setVersion(String version) { this.version = version; } @Override public String toString() { return "DBean{" + "sex='" + sex + '\'' + ", nname='" + nname + '\'' + ", msisdn='" + msisdn + '\'' + ", iconUrl='" + iconUrl + '\'' + ", showType='" + showType + '\'' + ", type='" + type + '\'' + ", version='" + version + '\'' + '}'; } } @Override public String toString() { return "UserInfo{" + "s='" + s + '\'' + ", m='" + m + '\'' + ", d=" + d + '}'; } }
MVP + Retrofit + RaJava
最新推荐文章于 2024-02-09 16:06:56 发布