Tutils tmvp工具类
public class TUtil {
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;
}
}
RetrofitManager 创建retrofit对象
public class RetrofitManager {
//地址
private String BaseURL = Contents.BASEURL;
private static OkHttpClient mOkHttpClient;
private Retrofit mRetrofit;
/**
* 构建Retrofit
*
* @return
*/
public Retrofit build() {
initOkHttpClient();
initRetrofit();
return mRetrofit;
}
public RetrofitManager() {
}
protected void initRetrofit() {
mRetrofit = new Retrofit.Builder()
.baseUrl(BaseURL)
.client(mOkHttpClient)
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create(new Gson()))
.build();
}
private void initOkHttpClient() {
//拦截器,用于输出网络请求和结果的 Log
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
//初始化OkHttpClient
mOkHttpClient = new OkHttpClient.Builder()
.addInterceptor(interceptor)
.retryOnConnectionFailure(true)
.connectTimeout(15, TimeUnit.SECONDS)
.build();
}
}
LoginContract(契约类),有了契约类后自动生成相关的base类
public class LoginContract {
public interface View extends BaseView {
void loginSucc(String str);
void loginFail(String result);
}
public interface Model extends BaseModel {
rx.Observable<Bean> loginByPsw(String phone, String code);
}
public abstract static class Presenter extends BasePresenter<Model, View> {
abstract void phoneLogin(String phone,String psw);
}
}
BaseModel初始化retrofit对象
public interface BaseModel {
public RetrofitManager mRetrofitManager=new RetrofitManager();
}
BasePresenter 初始化presenter对象
public class BasePresenter<M, V> {
public M mModel;
public V mView;
public void setMV(M m,V v){
this.mModel=m;
this.mView=v;
}
}
BaseActivity
public abstract class BaseActivity <M extends BaseModel,P extends BasePresenter> extends AppCompatActivity {
public M model;
public P presenter;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
model= TUtils.getT(this,0);
presenter=TUtils.getT(this,1);
if(this instanceof BaseView){
presenter.setMV(model,this);
}
setContentView(getLayoutId());
initView();
}
public abstract int getLayoutId();
public abstract void initView();
}
BaseFragment
public abstract class BaseFragment <M extends BaseModel,P extends BasePresenter> extends Fragment {
public M model;
public P presenter;
public View view;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view= inflater.inflate(getLayoutId(),container,false);
model=TUtil.getT(this,0);
presenter=TUtil.getT(this,1);
if (this instanceof BaseView){
presenter.setM(model,this);
}
return view;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
initView(view);
initData();
}
protected abstract void initData();
protected abstract void initView(View view);
protected abstract int getLayoutId();
}