MVPRXJava与Recorfit运用

MVP接口运用就不做了 都是基本的传值
//API界面
import retrofit2.http.POST;
import retrofit2.http.Query;
import rx.Observable;
     /** * Created by Administrator on 2018/4/15. */
    public interface ApiService {
         // https://www.zhaoapi.cn/user/reg 
                  @POST("user/reg")
                  Observable<Bean> getPost(@Query("mobile") String mobole,@Query("password") String password);}

Model层
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
/** * Created by Administrator on 2018/4/15. */
class Moudel {
private IMou iMou;
    public Moudel(IMou iMou) {
this.iMou = iMou;
}
public void getDate(String url, String mobile, String password) {
       OkHttpClient.Builder client = new OkHttpClient.Builder();
       Retrofit retrofit = new Retrofit.Builder()
             .client(client.build())
             .baseUrl(url)
            .addConverterFactory(GsonConverterFactory.create())
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .build();

ApiService service = retrofit.create(ApiService.class);
service.getPost(mobile,password).subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<Bean>() {

@Override 
public void onCompleted() {
} @Override 
public void onError(Throwable e) { }
@Override
public void onNext(Bean bean) {
       iMou.getBean(bean);
} }); }}

P层
class Presenter implements IMou {

    private IPre iPre;
    private Moudel moudel;

    public Presenter(IPre iPre) {
        this.iPre = iPre;
        moudel = new Moudel(this);
    }

    public void getDate(String url, String mobile, String password) {
        moudel.getDate(url,mobile,password);
    }

    @Override
    public void getBean(Bean bean) {
        iPre.getBean(bean);
    }
}

view层
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements IPre{

    private Presenter presenter;
    public static String url = "https://www.zhaoapi.cn/";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        presenter = new Presenter(this);

        presenter.getDate(url,"15110198098","123");
    }

    @Override
    public void getBean(Bean bean) {
        String code = bean.getCode();
        if ("0".equals(code)){
            Toast.makeText(this,bean.getMsg(),Toast.LENGTH_SHORT).show();
        }else{
            Toast.makeText(this,bean.getMsg(),Toast.LENGTH_SHORT).show();
        }
    }
}



阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭