一、倒依赖,经常用的一次导到位
implementation 'com.squareup.okhttp3:okhttp:3.8.1'
implementation 'com.squareup.okhttp3:logging-interceptor:3.4.1'
implementation 'io.reactivex:rxjava:1.1.6'
// Retrofit的rx解析库
implementation 'com.squareup.retrofit2:retrofit:2.1.0'
implementation 'com.squareup.retrofit2:adapter-rxjava:2.1.0'
// Retrofit的gson库字符串库
implementation 'com.squareup.retrofit2:converter-gson:2.1.0'
implementation 'com.squareup.retrofit2:converter-scalars:2.1.0'
implementation 'com.orhanobut:logger:1.8'
//解决Android3.0以后不能butterknife问题
implementation 'com.jakewharton:butterknife:8.4.0'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.4.0'
compile 'io.reactivex:rxandroid:1.2.1'
implementation 'com.android.support:recyclerview-v7:27.+'
implementation 'com.android.support:design:27.+'
implementation 'com.github.bumptech.glide:glide:3.8.0'
二、base包
(1)BaseActivity类
public abstract class BaseActivity<P extends BasePresenter, M extends BaseModel> extends AppCompatActivity {
public P mPersenter;
public M mModel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
inits();
initView();
}
protected abstract void initView();
private void inits() {
setContentView(getLayoutId());
mPersenter = TUtils.getT(this, 0);
mModel = TUtils.getT(this, 1);
if(this instanceof BaseView){
mPersenter.setMV(mModel,this);
}
}
protected abstract int getLayoutId();
}
(2)baseFragment类
public abstract class BaseFragment<P extends BasePresenter,M extends BaseModel> extends Fragment {
public P mPersenter ;
public M mModel ;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View inflate = LayoutInflater.from(getContext()).inflate(getLayoutId(), null);
inits();
initView(inflate);
return inflate;
}
protected abstract int getLayoutId();
protected void inits() {
mPersenter = TUtils.getT(this, 0);
mModel = TUtils.getT(this, 1);
if (this instanceof BaseView) mPersenter.setMV(mModel,this);
}
protected abstract void initView(View inflate);
}
(3)BasePresenter类
public class BasePresenter<M, V> {
public M mModel;
public V mView;
public void setMV(M m,V v){
this.mModel=m;
this.mView=v;
}
}
三、login包
(1)LoginContract类,不想解释什么原因这么写了,自己悟把!
public class LoginContract {
interface View extends BaseView {
void loginSucc(Bean string);
}
interface Model extends BaseModel {
void login(String url , CallBacks callBacks);
}
abstract static class Presenter extends BasePresenter<Model, View> {
public abstract void loginPre(String url);
}
}
(2)LoginModel类
public class LoginModel implements LoginContract.Model{
@Override
public void login(String url, final CallBacks callBacks) {
Retrofit retrofit = new Retrofit.Builder().baseUrl("https://api.dyhoa.com/dapi/v4/exam/")
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.build();
retrofit.create(ApiService.class).loginByPre()
.retry()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Action1<Bean>() {
@Override
public void call(Bean bean) {
callBacks.LoginSucc(bean);
}
}, new Action1<Throwable>() {
@Override
public void call(Throwable throwable) {
Log.e("TAG", throwable.getMessage().toString());
}
});
}
}
(3)LoginPresenter类
public class LoginPresenter extends LoginContract.Presenter{
@Override
public void loginPre(String url) {
mModel.login(url, new CallBacks() {
@Override
public void LoginSucc(Bean str) {
mView.loginSucc(str);
}
});
}
}
四、工具包
(1)CallBacks类
public interface CallBacks {
void LoginSucc(Bean str);
}
(2)ApiService类
public interface ApiService {
@GET("videoNew")
rx.Observable<Bean> loginByPre() ;
}
(3)TUtils类
public class TUtils {
public static <T> T getT(Object o, int i) {
try {
return ((Class<T>) ((ParameterizedType) (o.getClass()
.getGenericSuperclass())).getActualTypeArguments()[i])
.newInstance();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassCastException e) {
e.printStackTrace();
}
return null;
}
public static Class<?> forName(String className) {
try {
return Class.forName(className);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return null;
}
}
记得用的时候先 用工具包,免得报错太多。。。