登录实现+retrofit+rxjava

依赖导入

/retrofit
    compile 'io.reactivex.rxjava2:rxjava:2.0.7'
    compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
    compile 'com.squareup.retrofit2:retrofit:2.1.0'
    compile 'com.squareup.retrofit2:adapter-rxjava2:2.2.0'
    compile 'com.squareup.retrofit2:converter-gson:2.1.0'
布局文件

xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.loginregister.MainActivity">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="50dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="京东登录"
            android:textColor="#000"
            android:textSize="20sp"
            android:layout_centerInParent="true"
            />
    RelativeLayout>
    <LinearLayout
        android:layout_marginTop="60dp"
        android:layout_gravity="center_horizontal"
        android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <TextView
            android:textSize="20sp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="账号"
            />
        <EditText
            android:id="@+id/admin"
            android:layout_marginRight="20dp"
            android:layout_width="250dp"
            android:layout_height="wrap_content"
            android:hint="请输入你的账号"
            />
    LinearLayout>

    <LinearLayout
        android:layout_gravity="center_horizontal"

        android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <TextView
            android:textSize="20sp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="密码"
            />
        <EditText
            android:id="@+id/pwd"
            android:layout_marginRight="20dp"
            android:layout_width="250dp"
            android:layout_height="wrap_content"
            android:hint="请输入你的密码"
            />
    LinearLayout>
    <Button
        android:id="@+id/loginFive"
        android:textColor="#888888"
        android:layout_gravity="center_horizontal"
        android:background="@drawable/login_button"
        android:layout_marginTop="30dp"
        android:layout_width="350dp"
        android:layout_height="50sp"
        android:textSize="20sp"
        android:text="登录"
        />
    <RelativeLayout
        android:layout_marginTop="10dp"
        android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/register"
            android:padding="10dp"
            android:text="注册账号"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <TextView
            android:id="@+id/forget"
            android:padding="10dp"
            android:layout_alignParentRight="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="忘记密码"
            />

    RelativeLayout>

LinearLayout>

创建一个接口

public interface MyService {

    @GET("user/login")
    Observable<LoginBean> getLogin(@QueryMap Map<String,String> map);

}
封装retrofit+rxjava+okhttp

public class RetrofitUtils {
       private static RetrofitUtils retrofitUtils;
    private final Retrofit retrofit;
     //单例模式
    private RetrofitUtils(String baseUrl){
        retrofit = new Retrofit.Builder()
                .baseUrl(baseUrl)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();
       }

        //封装
       public static RetrofitUtils getInstance(String baseUrl){
           if(retrofitUtils == null){
               retrofitUtils = new RetrofitUtils(baseUrl);
               OkHttpClient.Builder builder = new OkHttpClient.Builder();
               builder.readTimeout(20, TimeUnit.SECONDS);
               builder.writeTimeout(20,TimeUnit.SECONDS);
               builder.connectTimeout(20,TimeUnit.SECONDS);
           }
           return  retrofitUtils;
       }
       //返回一个retrofit对象
      public Retrofit getRetrofit(){
           return retrofit;
      }
}
Model层

public class MyMdule {

    public void getData(String admin, String pwd, final ModuleListener moduleListener){
        //调用工具类调用方法
        Retrofit retrofit = RetrofitUtils.getInstance(API.loginUrl).getRetrofit();
        Map<String,String> map = new HashMap<>();
        map.put("mobile",admin);
        map.put("password",pwd);
        Observable<LoginBean> login = retrofit.create(MyService.class).getLogin(map);
           login.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<LoginBean>() {
                    @Override
                    public void accept(LoginBean loginBean) throws Exception {
                        if(moduleListener!=null){
                            //接口回调数据
                             moduleListener.success(loginBean);
                        }
                    }});
    }
    //定义的接口
    public interface ModuleListener{
        void success(LoginBean loginBean);
    }
}
presenter层

public class MyPresenter {
     PresenterListener presenterListener;
    private final MyMdule myMdule;

    public MyPresenter(PresenterListener presenterListener) {
        this.presenterListener = presenterListener;
        myMdule = new MyMdule();
    }

     //判断不能为空
    public void getData(String admin,String pwd){
        if(TextUtils.isEmpty(admin) || TextUtils.isEmpty(pwd)){
            if(presenterListener!=null){
                presenterListener.empty("不能为空!");
                return;
            }
        }
      //调用方法
        myMdule.getData(admin, pwd, new MyMdule.ModuleListener() {
            @Override
            public void success(LoginBean loginBean) {
                String msg = loginBean.getMsg();
                if(presenterListener!=null){
                    presenterListener.success(msg);
                }
            }
        });
    }

    public interface PresenterListener{
        void success(String msg);
         void empty(String s);
    }
}


Mainactivity里面

public class MainActivity extends AppCompatActivity implements MyPresenter.PresenterListener {

    @BindView(R.id.admin)
    EditText mAdmin;
    @BindView(R.id.pwd)
    EditText mPwd;
    @BindView(R.id.loginFive)
    Button mLoginFive;
    @BindView(R.id.register)
    TextView mRegister;
    @BindView(R.id.forget)
    TextView mForget;
    private MyPresenter myPresenter;

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


        ButterKnife.bind(this);
        myPresenter = new MyPresenter(this);
    }

    //成功返回的数据
    @Override
    public void success(String msg) {
        Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
    }

    //判断不能为空
    @Override
    public void empty(String s) {
        Toast.makeText(MainActivity.this, s, Toast.LENGTH_SHORT).show();

    }

    @OnClick({R.id.loginFive, R.id.register})
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.loginFive:
                myPresenter.getData(mAdmin.getText().toString().trim(), mPwd.getText().toString().trim());
                break;
            case R.id.register:
                startActivity(new Intent(MainActivity.this,SecondActivity.class));
                break;
        }
    }
}



你可能感兴趣的:(登录实现+retrofit+rxjava)