首先要封装好的Retrofit工具类 —— RetrofitManager
package com.example.mvpretrofitrxjava.utils;
import java.util.concurrent.TimeUnit;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
/**
* Created by DELL on 2018/7/11.
*/
public class RetrofitManager {
//设置公共的请求url
private static String BASE_URL = "https://www.zhaoapi.cn/";
private final Retrofit retrofit;
private static class SingleHolder {
private static final RetrofitManager _INSTANCE = new RetrofitManager(BASE_URL);
}
public static RetrofitManager getDefault() {
return SingleHolder._INSTANCE;
}
private RetrofitManager(String baseUrl) {
retrofit = new Retrofit.Builder()
.baseUrl(baseUrl)
.client(buildOkhttpClient())
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build();
}
private OkHttpClient buildOkhttpClient() {
OkHttpClient client = new OkHttpClient.Builder()
.readTimeout(5000, TimeUnit.MILLISECONDS)
.writeTimeout(5000, TimeUnit.MILLISECONDS)
.connectTimeout(5000, TimeUnit.MILLISECONDS)
.build();
return client;
}
public T create(Class Clazz) {
return retrofit.create(Clazz);
}
}
登录的Api —— ILoginApi:
package com.example.mvpretrofitrxjava.mvp.view.iview;
import com.example.mvpretrofitrxjava.mvp.model.bean.LoginBean;
import io.reactivex.Observable;
import retrofit2.http.GET;
import retrofit2.http.Query;
/**
* Created by DELL on 2018/7/11.
*/
public interface ILoginApi {
@GET("user/login")
Observable login(@Query("mobile") String mobile, @Query("password") String password);
}
登录回调的接口 —— ILoginView:
package com.example.mvpretrofitrxjava.mvp.view.iview;
import com.example.mvpretrofitrxjava.mvp.model.bean.LoginBean;
/**
* Created by DELL on 2018/7/11.
*/
public interface ILoginView {
void onLoginSuccess(LoginBean loginBean);
void onLoginFailed(String error);
}
随后就是Model层 —— LoginModel:
package com.example.mvpretrofitrxjava.mvp.model;
import com.example.mvpretrofitrxjava.mvp.model.bean.LoginBean;
import com.example.mvpretrofitrxjava.mvp.view.iview.ILoginApi;
import com.example.mvpretrofitrxjava.utils.RetrofitManager;
import io.reactivex.Observable;
/**
* Created by DELL on 2018/7/11.
*/
public class LoginModel {
public Observable login(String mobile, String password) {
return RetrofitManager.getDefault().create(ILoginApi.class).login(mobile, password);
}
}
Presenter层 —— LoginPresenter:
package com.example.mvpretrofitrxjava.mvp.presenter;
import com.example.mvpretrofitrxjava.mvp.model.LoginModel;
import com.example.mvpretrofitrxjava.mvp.model.bean.LoginBean;
import com.example.mvpretrofitrxjava.mvp.view.iview.ILoginView;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
/**
* Created by DELL on 2018/7/11.
*/
public class LoginPresenter {
private LoginModel loginModel;
ILoginView mILoginView;
public LoginPresenter(ILoginView loginView) {
mILoginView = loginView;
loginModel = new LoginModel();
}
public void login(String mobile, String password) {
loginModel.login(mobile, password)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(LoginBean loginBean) {
if (mILoginView != null) {
mILoginView.onLoginSuccess(loginBean);
}
}
@Override
public void onError(Throwable e) {
if (mILoginView != null) {
mILoginView.onLoginFailed(e.toString());
}
}
@Override
public void onComplete() {
}
});
}
}
最后是MainActivity中的代码:
package com.example.mvpretrofitrxjava.mvp.view.activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import com.example.mvpretrofitrxjava.R;
import com.example.mvpretrofitrxjava.mvp.model.bean.LoginBean;
import com.example.mvpretrofitrxjava.mvp.presenter.LoginPresenter;
import com.example.mvpretrofitrxjava.mvp.view.iview.ILoginView;
public class MainActivity extends AppCompatActivity implements ILoginView {
private LoginPresenter presenter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
presenter = new LoginPresenter(this);
}
public void login(View view) {
presenter.login("15201161615", "123456");
}
@Override
public void onLoginSuccess(final LoginBean loginBean) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, "登录成功" + loginBean.getData().getMobile(), Toast.LENGTH_SHORT).show();
}
});
}
@Override
public void onLoginFailed(final String error) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, "登录失败" + error, Toast.LENGTH_SHORT).show();
}
});
}
}
写在最后:只是简单的利用MVP+Retrofit+RxJava框架实现了登录功能,不喜勿喷!!!