**BaseActivity
public abstract class BaseActivity<T extends BasePre> extends AppCompatActivity implements Iview {
private Mpre basePre;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initData();
initView();
}
public BasePre getBasePre(Iview iview) {
synchronized (BasePre.class) {
basePre = new Mpre(iview);
}
return basePre;
}
abstract void initView();
abstract void initData();
}
BaseView
public interface BaseView {
void Scuess(Object o, int RequestCode);
void Error(Throwable t, int RequestCode);
}
MyModel
public class Mmodel {
Ipre ipre;
private final OkhttpUtils instance;
public Mmodel(Mpre mpre) {
this.ipre = mpre;
instance = OkhttpUtils.Instance();
}
public void UpLoad( String uid, final int requestCode) {
File f1 = new File("/storage/emulated/0/Pictures/c.png");
RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), f1);
MultipartBody.Part body = MultipartBody.Part.createFormData("image", f1.getName(), requestFile);
RequestBody uidBody = RequestBody.create(MediaType.parse("text/plain"), uid);
instance.InstanceApi()
.upLoad(uidBody, body)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new DisposableSubscriber<Bean>() {
@Override
public void onNext(Bean bean) {
ipre.Scuess(bean, requestCode);
}
@Override
public void onError(Throwable t) {
ipre.Error(t, requestCode);
}
@Override
public void onComplete() {
}
});
}
Iview
public interface Iview extends BaseView {
}
MainActivity
public class MainActivity extends BaseActivity implements Iview {
BasePre basePre;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
basePre = getBasePre(this);
basePre.accthView(this);
basePre.UpLoad("10039", 2);
}
@Override
void initView() {
Log.e("o", "initView");
}
@Override
void initData() {
Log.e("o", "initData");
}
@Override
protected void onDestroy() {
super.onDestroy();
basePre.dechView();
}
@Override
public void Scuess(Object o, int RequestCode) {
if (RequestCode == 0) {
Log.e("o1", (String) o);
} else if (RequestCode == 1) {
Log.e("o2", (String) o);
} else if (RequestCode == 2) {
Bean bean = (Bean) o;
Log.e("o3", bean.getMsg());
}
}
@Override
public void Error(Throwable t, int RequestCode) {
if (RequestCode == 0) {
Log.e("err1", t.getMessage());
} else if (RequestCode == 1) {
Log.e("err2", t.getMessage());
} else if (RequestCode == 2) {
Log.e("err3", t.getMessage());
}
}
}
Mpre
public class Mpre extends BasePre implements Ipre {
Iview iview;
private final Mmodel mmodel;
public Mpre(Iview iview) {
this.iview = iview;
mmodel = new Mmodel(this);
}
@Override
public void Scuess(Object o,int RequestCode) {
iview.Scuess(o, RequestCode);
}
@Override
public void Error(Throwable t,int RequestCode) {
iview.Error(t, RequestCode);
}
@Override
public void Loginpre(String name, String pass,int RequestCode) {
mmodel.LoginM(name, pass,RequestCode);
}
@Override
public void Regpre(String name, String pass,int RequestCode) {
mmodel.RegM(name, pass,RequestCode);
}
@Override
public void UpLoad( String uid, int RequestCode) {
mmodel.UpLoad(uid,RequestCode);
}
}
Ipre
public interface Ipre {
void Scuess(Object o,int RequestCode);
void Error(Throwable t,int RequestCode);
}
Mpre
public class Mpre extends BasePre implements Ipre {
Iview iview;
private final Mmodel mmodel;
public Mpre(Iview iview) {
this.iview = iview;
mmodel = new Mmodel(this);
}
@Override
public void Scuess(Object o,int RequestCode) {
iview.Scuess(o, RequestCode);
}
@Override
public void Error(Throwable t,int RequestCode) {
iview.Error(t, RequestCode);
}
@Override
public void Loginpre(String name, String pass,int RequestCode) {
mmodel.LoginM(name, pass,RequestCode);
}
@Override
public void Regpre(String name, String pass,int RequestCode) {
mmodel.RegM(name, pass,RequestCode);
}
@Override
public void UpLoad( String uid, int RequestCode) {
mmodel.UpLoad(uid,RequestCode);
}
}
Mmodel
public class Mmodel {
Ipre ipre;
private final OkhttpUtils instance;
public Mmodel(Mpre mpre) {
this.ipre = mpre;
instance = OkhttpUtils.Instance();
}
public void UpLoad( String uid, final int requestCode) {
File f1 = new File("/storage/emulated/0/Pictures/c.png");
RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), f1);
MultipartBody.Part body = MultipartBody.Part.createFormData("image", f1.getName(), requestFile);
RequestBody uidBody = RequestBody.create(MediaType.parse("text/plain"), uid);
instance.InstanceApi()
.upLoad(uidBody, body)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new DisposableSubscriber<Bean>() {
@Override
public void onNext(Bean bean) {
ipre.Scuess(bean, requestCode);
}
@Override
public void onError(Throwable t) {
ipre.Error(t, requestCode);
}
@Override
public void onComplete() {
}
});
}
public void LoginM(String name, String pass, int RequestCode) {
ipre.Scuess("name=" + name + "/..../" + "pass=" + pass, RequestCode);
}
public void RegM(String name, String pass, int RequestCode) {
ipre.Scuess("name=" + name + "/..../" + "pass=" + pass, RequestCode);
}
}**