使用RxJava和Retrofit实现登录注册和头像上传

使用的权限和依赖
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.0'
implementation 'com.squareup.retrofit2:retrofit:2.4.0'
implementation 'com.squareup.retrofit2:converter-gson:2.4.0'
implementation 'io.reactivex.rxjava2:rxjava:2.1.13'
implementation 'io.reactivex.rxjava2:rxandroid:2.0.2'
implementation 'com.google.dagger:dagger:2.8'
annotationProcessor 'com.google.dagger:dagger-compiler:2.7'
implementation 'com.squareup.retrofit2:adapter-rxjava2:2.4.0'
implementation 'com.facebook.fresco:fresco:0.12.0'
// 支持 GIF 动图,需要添加
implementation 'com.facebook.fresco:animated-gif:0.12.0'
implementation 'com.android.support:support-v4:27.1.1'
implementation 'com.sunfusheng:marqueeview:1.3.3'
implementation 'com.jcodecraeer:xrecyclerview:1.3.2'
implementation 'com.youth.banner:banner:1.4.10'
implementation 'com.android.support:design:27.1.1'
implementation 'com.jakewharton:butterknife:8.8.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
implementation 'com.github.bumptech.glide:glide:4.7.1'
implementation 'org.greenrobot:eventbus:3.1.1'
implementation 'com.squareup.okhttp3:logging-interceptor:3.10.0'
//刷新
implementation 'com.scwang.smartrefresh:SmartRefreshLayout:1.0.4-7'

build.gradle

apply from: "config.gradle"
buildscript {
    
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.1.2'
    }
}

allprojects {
    repositories {
        google()
        jcenter()
        maven { url "https://jitpack.io" }
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

config.gradle

ext {
    android = [
            compileSdkVersion: 27,
            applicationId    : "com.bwie.test.myuploadyue",
            minSdkVersion    : 15,
            targetSdkVersion : 27,
            versionCode      : 1,
            versionName      : "1.0"
    ]

    dependencies = [
            "appcompat-v7"     : "com.android.support:appcompat-v7:27.1.1",
            "constraint-layout": "com.android.support.constraint:constraint-layout:1.1.0",
            "retrofit2"        : "com.squareup.retrofit2:retrofit:2.4.0",
            "converter-gson"   : "com.squareup.retrofit2:converter-gson:2.4.0",
            "adapter-rxjava"  :  "com.squareup.retrofit2:adapter-rxjava2:2.4.0",
            "rxjava2"          : "io.reactivex.rxjava2:rxjava:2.1.13",
            "rxjava2:rxandroid": "io.reactivex.rxjava2:rxandroid:2.0.2",
            "dagger"           : "com.google.dagger:dagger:2.8",
            "dagger-compiler" : "com.google.dagger:dagger-compiler:2.7"

    ]
}

activity_main

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    tools:context=".MainActivity">

    <FrameLayout
        android:id="@+id/flContent"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>

fragment_my

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <RelativeLayout
            android:id="@+id/login_back_pic"
            android:layout_width="match_parent"
            android:layout_height="183dp"
            android:background="@drawable/background">

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="120dp">

                <ImageView
                    android:id="@+id/iv5_touxiang"
                    android:layout_width="80dp"
                    android:layout_height="80dp"
                    android:layout_gravity="center"
                    android:layout_marginLeft="10dp"
                    android:src="@drawable/login" />

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:orientation="vertical">

                    <TextView
                        android:id="@+id/tv5_login"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="20dp"
                        android:text="登录/注册>"
                        android:textColor="@android:color/white"
                        android:textSize="15sp" />

                    <TextView
                        android:id="@+id/tv5_nicheng"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="20dp"
                        android:layout_marginTop="10dp"
                        android:text="昵称"
                        android:textColor="@android:color/white" />
                </LinearLayout>
            </LinearLayout>

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_marginTop="10dp">

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:onClick="tv5"
                    android:src="@drawable/shezhi" />

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="20dp"
                    android:src="@drawable/xiaoxi" />
            </LinearLayout>
        </RelativeLayout>

        <LinearLayout
            android:layout_marginTop="10dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <ImageView
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:src="@drawable/daifukuan"/>
            <ImageView
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:src="@drawable/daishouhuo"/>
            <ImageView
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:src="@drawable/daipingjia"/>
            <ImageView
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:src="@drawable/shouhou"/>
            <ImageView
                android:id="@+id/iv_dingdan"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:src="@drawable/wodedingdan"/>
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp">
            <TextView
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:textSize="10sp"
                android:text="代付款"/>
            <TextView
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:textSize="10sp"
                android:text="待收货"/>
            <TextView
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:textSize="10sp"
                android:text="待评价"/>
            <TextView
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:textSize="10sp"
                android:text="退换/售后"/>
            <TextView
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:textSize="10sp"
                android:text="我的订单"/>
        </LinearLayout>

        <View
            android:layout_marginTop="20dp"
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="#f0f2f5"/>

        <LinearLayout
            android:layout_marginTop="10dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <TextView
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:padding="10dp"
                android:textSize="10sp"
                android:text="0"/>
            <TextView
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:padding="10dp"
                android:textSize="10sp"
                android:text="0"/>
            <TextView
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:padding="10dp"
                android:textSize="10sp"
                android:text="0.00"/>
            <TextView
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:padding="10dp"
                android:textSize="10sp"
                android:text="0"/>
            <ImageView
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:src="@drawable/wodeqianbao"/>
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp">
            <TextView
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:textSize="10sp"
                android:text="京豆"/>
            <TextView
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:textSize="10sp"
                android:text="优惠券"/>
            <TextView
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:textSize="10sp"
                android:text="白条"/>
            <TextView
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:textSize="10sp"
                android:text="京东E卡"/>
            <TextView
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:textSize="10sp"
                android:text="我的钱包"/>
        </LinearLayout>
        <LinearLayout
            android:layout_marginTop="20dp"
            android:layout_width="match_parent"
            android:layout_height="10dp"
            android:background="#f0f2f5">

        </LinearLayout>
        <LinearLayout
            android:layout_marginTop="10dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <ImageView
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:src="@drawable/shangpinguanzhu"/>
            <ImageView
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:src="@drawable/dianpuguanzhu"/>
            <ImageView
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:src="@drawable/neirongguanzhu"/>
            <ImageView
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:src="@drawable/liulanjilu"/>

        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="6dp">
            <TextView
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:textSize="10sp"
                android:text="商品关注"/>
            <TextView
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:textSize="10sp"
                android:text="店铺关注"/>
            <TextView
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:textSize="10sp"
                android:text="内容关注"/>
            <TextView
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:textSize="10sp"
                android:text="浏览记录"/>

        </LinearLayout>
        <View
            android:layout_marginTop="20dp"
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="#f0f2f5"/>
        <LinearLayout
            android:layout_marginTop="10dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <ImageView
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:src="@drawable/wodehuodong"/>
            <ImageView
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:src="@drawable/shequ"/>
            <ImageView
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:src="@drawable/kehufuwu"/>

            <ImageView
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content" />
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="6dp">
            <TextView
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:textSize="10sp"
                android:text="我的活动"/>
            <TextView
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:textSize="10sp"
                android:text="社区"/>
            <TextView
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:textSize="10sp"
                android:text="客户服务"/>
            <TextView
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text=""/>

        </LinearLayout>
    </LinearLayout>
</ScrollView>

activity_login

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    tools:context=".LoginActivity">
    <RelativeLayout
        android:id="@+id/login_title_relative"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp">

        <ImageView
            android:id="@+id/cha"
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:src="@drawable/cha"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="京东登录"/>
    </RelativeLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/login_title_relative"
        android:layout_margin="10dp"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="账号"/>

            <EditText
                android:id="@+id/mobile"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:orientation="horizontal">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="密码"/>

            <EditText
                android:id="@+id/password"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
        </LinearLayout>

        <Button
            android:id="@+id/btLogin"
            android:text="登录"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/text_regist"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="手机快速注册"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="忘记密码"/>
        </LinearLayout>
    </LinearLayout>
</RelativeLayout>

activity_regist

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    tools:context=".RegistActivity">
    <RelativeLayout
        android:id="@+id/login_title_relative"
        android:padding="10dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ImageView
            android:id="@+id/cha_iamge"
            android:src="@drawable/cha"
            android:layout_width="20dp"
            android:layout_height="20dp" />

        <TextView
            android:text="注册"
            android:layout_centerInParent="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </RelativeLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/login_title_relative"
        android:layout_margin="10dp"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="账号"/>

            <EditText
                android:id="@+id/mobile"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:orientation="horizontal">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="密码"/>

            <EditText
                android:id="@+id/password"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
        </LinearLayout>

        <Button
            android:id="@+id/bt"
            android:layout_margin="10dp"
            android:text="注册"
            android:gravity="center"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>
</RelativeLayout>

activity_user_info

<?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:id="@+id/ueseInfo_line"
    android:orientation="vertical"
    tools:context=".UserInfoActivity">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/iv"
            android:layout_width="70dp"
            android:layout_height="70dp"/>

        <TextView
            android:id="@+id/tv"
            android:layout_gravity="center"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    </LinearLayout>

    <Button
        android:id="@+id/bt"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/shape_search"
        android:text="退出登录"/>
</LinearLayout>

pop_item

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/pop_layout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:gravity="center_horizontal"
    android:orientation="vertical">

    <Button
        android:id="@+id/btn_take_photo"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dip"
        android:layout_marginRight="20dip"
        android:layout_marginTop="20dip"
        android:background="@drawable/shape_search"
        android:text="拍照"
        android:textStyle="bold"/>

    <Button
        android:id="@+id/btn_pick_photo"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dip"
        android:layout_marginRight="20dip"
        android:layout_marginTop="5dip"
        android:background="@drawable/shape_search"
        android:text="从相册选择"
        android:textStyle="bold"/>

    <Button
        android:id="@+id/btn_cancel"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="15dip"
        android:layout_marginLeft="20dip"
        android:layout_marginRight="20dip"
        android:layout_marginTop="15dip"
        android:background="@drawable/shape_search"
        android:text="取消"
        android:textColor="#66000000"
        android:textStyle="bold"/>
</LinearLayout>

drawable文件夹下shape_search

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners android:radius="20dp"/>
    <solid android:color="#ffffff"/>
</shape>

MainActivity

import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.FrameLayout;

public class MainActivity extends AppCompatActivity {
    private FrameLayout mFl;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        FragmentManager fragmentManager = getSupportFragmentManager();
        MyFragment myFragment = new MyFragment();
        fragmentManager.beginTransaction()
                .replace(R.id.flContent, myFragment)
                .commit();
    }

    private void initView() {
        mFl = (FrameLayout) findViewById(R.id.flContent);
    }
}

MyFragment

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bwie.test.myuploadyue.base.BaseFragment;
import com.bwie.test.utils.SharedPreferencesUtils;

public class MyFragment extends BaseFragment implements View.OnClickListener{
    private View view;
    private ImageView mIv5Touxiang;
    /**
     * 登录/注册>
     */
    private TextView mTv5Login;
    /**
     * 昵称
     */
    private TextView mTv5Nicheng;
    private ImageView mIvDingdan;
    private RelativeLayout login_back_pic;

    @Override
    public int getContentLayout() {
        return R.layout.fragment_my;
    }

    @Override
    public void onResume() {
        super.onResume();
        String name = (String) SharedPreferencesUtils.getParam(getContext(), "name", "");
        String iconUrl = (String) SharedPreferencesUtils.getParam(getContext(), "iconUrl", "");
        String uid = (String) SharedPreferencesUtils.getParam(getContext(), "uid", "");
        if (!TextUtils.isEmpty(uid)) {
            //登录过
            login_back_pic.setBackgroundResource(R.drawable.background);
        } else {
            //未登录
            login_back_pic.setBackgroundResource(R.drawable.reg_bg);
        }
        if (!TextUtils.isEmpty(iconUrl)) {
            Glide.with(getContext()).load(iconUrl).into(mIv5Touxiang);
        }
        if (!TextUtils.isEmpty(name)) {
            mTv5Nicheng.setText(name);
        }
    }

    @Override
    public void inject() {

    }

    @Override
    public void initView(View view) {
        mIv5Touxiang = (ImageView) view.findViewById( R.id.iv5_touxiang );
        mTv5Login = (TextView) view.findViewById( R.id.tv5_login );
        mTv5Nicheng = (TextView) view.findViewById( R.id.tv5_nicheng );
        mIvDingdan = (ImageView) view.findViewById( R.id.iv_dingdan );
        login_back_pic = view.findViewById(R.id.login_back_pic);
        setListener();
    }

    private void setListener() {
        mTv5Login.setOnClickListener( this );
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv5_login:
                //判断是否登录过
                String uid = (String) SharedPreferencesUtils.getParam(getContext(), "uid", "");
                if (TextUtils.isEmpty(uid)) {
                    //未登录
                    Intent intent = new Intent(getContext(), LoginActivity.class);
                    startActivity(intent);
                } else {
                    //已登录
                    Intent intent = new Intent(getContext(), UserInfoActivity.class);
                    startActivity(intent);
                }
                break;
        }
    }
}

LoginActivity

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bwie.test.bean.UserBean;
import com.bwie.test.component.DaggerHttpComponent;
import com.bwie.test.module.HttpModule;
import com.bwie.test.myuploadyue.base.BaseActivity;
import com.bwie.test.myuploadyue.contact.LoginContract;
import com.bwie.test.myuploadyue.presenter.LoginPresenter;
import com.bwie.test.utils.SharedPreferencesUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class LoginActivity extends BaseActivity<LoginPresenter> implements View.OnClickListener,
                         LoginContract.View {
    @BindView(R.id.cha)
    ImageView mCha;
    @BindView(R.id.login_title_relative)
    RelativeLayout mLoginTitleRelative;
    @BindView(R.id.mobile)
    EditText mMobile;
    @BindView(R.id.password)
    EditText mPassword;
    @BindView(R.id.btLogin)
    Button mBtLogin;
    @BindView(R.id.text_regist)
    TextView mTextRegist;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        mMobile = (EditText) findViewById(R.id.mobile);
        mPassword = (EditText) findViewById(R.id.password);
        mBtLogin = (Button) findViewById(R.id.btLogin);
        mBtLogin.setOnClickListener(this);
    }

    @Override
    public int getContentLayout() {
        return R.layout.activity_login;
    }

    @Override
    public void inject() {
        DaggerHttpComponent.builder()
                .httpModule(new HttpModule())
                .build()
                .inject(this);
    }

    @Override
    public void loginSuccess(UserBean userBean) {
        Toast.makeText(LoginActivity.this, userBean.getMsg(), Toast.LENGTH_SHORT).show();
        Toast.makeText(LoginActivity.this, userBean.getMsg(), Toast.LENGTH_SHORT).show();
        SharedPreferencesUtils.setParam(LoginActivity.this,"uid",userBean.getData().getUid() + "");
        SharedPreferencesUtils.setParam(LoginActivity.this,"name",userBean.getData().getUsername() + "");
        SharedPreferencesUtils.setParam(LoginActivity.this,"iconUrl",userBean.getData().getIcon() + "");
        SharedPreferencesUtils.setParam(LoginActivity.this,"token",userBean.getData().getToken() + "");
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
        LoginActivity.this.finish();
    }

    @OnClick({R.id.cha,R.id.btLogin, R.id.text_regist})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.cha:
                finish();
                break;
            case R.id.btLogin:
                //需要调用P层,去完成接口调用
                String mobile = mMobile.getText().toString();
                String password = mPassword.getText().toString();
                mPresenter.login(mobile, password);
                break;
            case R.id.text_regist:
                Intent intent = new Intent(LoginActivity.this, RegistActivity.class);
                startActivity(intent);
                finish();
                break;
        }
    }
}

RegistActivity

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.bwie.test.bean.RegisterBean;
import com.bwie.test.component.DaggerHttpComponent;
import com.bwie.test.module.HttpModule;
import com.bwie.test.myuploadyue.base.BaseActivity;
import com.bwie.test.myuploadyue.contact.RegisterContract;
import com.bwie.test.myuploadyue.presenter.RegisterPresenter;

public class RegistActivity extends BaseActivity<RegisterPresenter> implements RegisterContract.View, 
                                     View.OnClickListener {
    private Button mBt;
    private ImageView mChaIamge;
    private RelativeLayout mLoginTitleRelative;
    private EditText mMobile;
    private EditText mPassword;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
    }

    @Override
    public int getContentLayout() {
        return R.layout.activity_regist;
    }

    @Override
    public void inject() {
        DaggerHttpComponent.builder()
                .httpModule(new HttpModule())
                .build()
                .inject(this);
    }

    private void initView() {
        mChaIamge = (ImageView) findViewById(R.id.cha_iamge);
        mChaIamge.setOnClickListener(this);
        mLoginTitleRelative = (RelativeLayout) findViewById(R.id.login_title_relative);
        mMobile = (EditText) findViewById(R.id.mobile);
        mPassword = (EditText) findViewById(R.id.password);
        mBt = (Button) findViewById(R.id.bt);
        mBt.setOnClickListener(this);
    }

    @Override
    public void registerSuccess(RegisterBean registerBean) {
        String msg = registerBean.getCode();
        if (msg.equals("0")){
            Intent intent = new Intent(RegistActivity.this, LoginActivity.class);
            startActivity(intent);
            //finish();
            Toast.makeText(RegistActivity.this, registerBean.getMsg(), Toast.LENGTH_SHORT).show();
        }else {
            Toast.makeText(RegistActivity.this, registerBean.getMsg(), Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.cha_iamge:
                finish();
                break;
            case R.id.bt:
                String mobile = mMobile.getText().toString();
                String password = mPassword.getText().toString();
                mPresenter.register(mobile, password);
                break;
        }
    }
}

UserInfoActivity

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bwie.test.component.DaggerHttpComponent;
import com.bwie.test.myuploadyue.base.BaseActivity;
import com.bwie.test.myuploadyue.contact.UpdateHeaderContract;
import com.bwie.test.myuploadyue.presenter.UpdatePresenter;
import com.bwie.test.utils.SharedPreferencesUtils;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class UserInfoActivity  extends BaseActivity<UpdatePresenter> implements View.OnClickListener,
                               UpdateHeaderContract.View {
    private ImageView mIv;
    private TextView mTv;
    /**
     * 退出登录
     */
    private Button mBt;
    private PopupWindow popupWindow;
    private LinearLayout ueseInfo_line;
    private String imgPath;
    private File imgFile;
    private static final int PHOTO_REQUEST_TAKEPHOTO = 1;// 拍照
    private static final int PHOTO_REQUEST_GALLERY = 2;// 从相册中选择
    private static final int PHOTO_REQUEST_CUT = 3;// 结果
    private Bitmap photo;
    //裁剪完成之后图片保存的路径
    private String crop_icon_path = Environment.getExternalStorageDirectory()+"/head_icon.jpg";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        imgPath = getExternalCacheDir() + File.separator + "header.jpg";
        imgFile = new File(imgPath);
        Log.e("UserInfoActivity", "imgPath:" + imgPath);
        initView();
        String name = (String) SharedPreferencesUtils.getParam(this, "name", "");
        String iconUrl = (String) SharedPreferencesUtils.getParam(this, "iconUrl", "");

        mTv.setText(name);
        Glide.with(this).load(iconUrl).into(mIv);
    }

    //找控件id
    private void initView() {
        mIv = (ImageView) findViewById(R.id.iv);
        mTv = (TextView) findViewById(R.id.tv);
        mBt = (Button) findViewById(R.id.bt);
        ueseInfo_line = findViewById(R.id.ueseInfo_line);
        mBt.setOnClickListener(this);
        mIv.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.bt:
                //清空SharedPreferences
                SharedPreferencesUtils.clear(UserInfoActivity.this);
                //回到登录页面
                Intent intent = new Intent(UserInfoActivity.this, LoginActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
                UserInfoActivity.this.finish();
                break;
            case R.id.iv:
                //在底部弹出PopupWindow
                initPopupWindow();
                popupWindow.showAtLocation(ueseInfo_line, Gravity.BOTTOM, 0, 0);
                break;
            case R.id.btn_cancel:
                //取消
                if (popupWindow != null && popupWindow.isShowing()) {
                    popupWindow.dismiss();
                }
                break;
            case R.id.btn_take_photo:
                //拍照
                takePhoto();
                if (popupWindow != null && popupWindow.isShowing()) {
                    popupWindow.dismiss();
                }
                break;
            case R.id.btn_pick_photo:
                //获取相册图片并进行剪切
                getLocalPic();
                if (popupWindow != null && popupWindow.isShowing()) {
                    popupWindow.dismiss();
                }
                break;
        }
    }

    //拍照
    private void takePhoto() {
        Intent intent = new Intent( MediaStore.ACTION_IMAGE_CAPTURE);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(imgFile));
        startActivityForResult(intent, PHOTO_REQUEST_TAKEPHOTO);
    }

    //剪切
    private void startPhotoZoom(Uri uri) {
        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(uri, "image/*");
        intent.putExtra("crop", "true");

        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);

        intent.putExtra("outputX", 300);
        intent.putExtra("outputY", 300);
        intent.putExtra("return-data", true);
        intent.putExtra("noFaceDetection", true);
        startActivityForResult(intent, 3);
    }

    /**
     * 获取相册的图片
     */
    private void getLocalPic() {
        Intent intent = new Intent();
        //指定选择/获取的动作...PICK获取,拿
        intent.setAction(Intent.ACTION_PICK);
        //指定获取的数据的类型
        intent.setType("image/*");
        startActivityForResult(intent, 2);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case PHOTO_REQUEST_TAKEPHOTO:
                if (resultCode == Activity.RESULT_OK) {
                    //截取图片
                    startPhotoZoom(Uri.fromFile(imgFile));
                }
                break;
            case PHOTO_REQUEST_CUT:
                //截图图片成功
                Bundle bundle = data.getExtras();
                if (bundle != null) {
                    photo = bundle.getParcelable("data");
                    //上传头像
                    mPresenter.updateHeader(getUid(), imgPath);
                }
                break;
            case  PHOTO_REQUEST_GALLERY:
                if (requestCode == 2 && resultCode == RESULT_OK) {
                    Uri uri = data.getData();
                    startPhotoZoom(uri);
                }
                if (requestCode == 3 && resultCode == RESULT_OK) {
                    Bitmap bitmap = data.getParcelableExtra("data");
                    File saveIconFile = new File(crop_icon_path);
                    if(saveIconFile.exists()){
                        saveIconFile.delete();
                    }
                    //创建出新的文件
                    try {
                        saveIconFile.createNewFile();
                        FileOutputStream fos = new FileOutputStream(saveIconFile);
                        //把bitmap通过流的形式压缩到文件中
                        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
                        fos.flush();
                        fos.close();
                        mPresenter.updateHeader(getUid(), crop_icon_path);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                break;
        }
    }

    //PopupWindow弹窗
    private void initPopupWindow() {
        View view = LayoutInflater.from(this).inflate(R.layout.pop_item, null);
        popupWindow = new PopupWindow(view, LinearLayout.LayoutParams.MATCH_PARENT, 
                                          LinearLayout.LayoutParams.WRAP_CONTENT);
        //点击PopupWindow外部可以取消
        popupWindow.setOutsideTouchable(true);
        popupWindow.setBackgroundDrawable(new ColorDrawable());

        Button btn_take_photo = view.findViewById(R.id.btn_take_photo);
        Button btn_pick_photo = view.findViewById(R.id.btn_pick_photo);
        Button btn_cancel = view.findViewById(R.id.btn_cancel);

        btn_cancel.setOnClickListener(this);
        btn_pick_photo.setOnClickListener(this);
        btn_take_photo.setOnClickListener(this);
    }

    @Override
    public int getContentLayout() {
        return R.layout.activity_user_info;
    }

    @Override
    public void inject() {
        DaggerHttpComponent.builder()
                .build()
                .inject(this);
    }

    @Override
    public void updateSuccess(String code) {
        if ("0".equals(code) && photo != null) {
            toast("上传成功");
            //去设置头像
            mIv.setImageBitmap(photo);
        }
    }
}

base文件夹下BaseActivity

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.Toast;

import com.bwie.test.inter.IBase;
import com.bwie.test.utils.SharedPreferencesUtils;
import javax.inject.Inject;

public abstract class BaseActivity<T extends BaseContract.BasePresenter> extends FragmentActivity 
                implements IBase,BaseContract.BaseView {
    @Inject
    protected T mPresenter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getContentLayout());
        inject();
        //绑定
        if (mPresenter != null) {
            mPresenter.attchView(this);
        }
    }

    @Override
    public void initView(View view) {
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //解绑
        if (mPresenter != null) {
            mPresenter.detachView();
        }
    }

    @Override
    public void showLoading() {
    }

    @Override
    public void dismissLoading() {
    }

    protected String getUid() {
        return (String) SharedPreferencesUtils.getParam(this, "uid", "");
    }

    protected String getToken() {
        return (String) SharedPreferencesUtils.getParam(this, "token", "");
    }

    protected void toast(String str){
        Toast.makeText(this,str,Toast.LENGTH_SHORT).show();
    }
}

BaseContract

public interface BaseContract {
    //抽取所有Presenter共性,比如绑定,解绑
    interface BasePresenter<T extends BaseView>{
        void attchView(T view);
        void detachView();
    }
    //抽取所有View的共性,比如显示进度条和关闭进度
    interface BaseView{
        void showLoading();
        void dismissLoading();
    }
}

BaseFragment

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bwie.test.inter.IBase;
import javax.inject.Inject;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public abstract class BaseFragment<T extends BaseContract.BasePresenter> extends Fragment 
                                implements IBase,BaseContract.BaseView {
    @Inject
    protected T mPresenter;
    private Unbinder bind;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        inject();
        if (mPresenter != null) {
            mPresenter.attchView(this);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.detachView();
        }
        if (bind != null) {
            bind.unbind();
        }
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, 
                            @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(getContentLayout(), null);
        bind = ButterKnife.bind(getActivity(), view);
        initView(view);
        return view;
    }

    @Override
    public void showLoading() {
    }

    @Override
    public void dismissLoading() {
    }
}

BasePresenter

package com.bwie.test.myuploadyue.base;

public class BasePresenter<T extends BaseContract.BaseView>implements BaseContract.BasePresenter<T> {
    protected T mView;

    @Override
    public void attchView(T view) {
        this.mView=view;
    }

    @Override
    public void detachView() {
        if (mView != null) {
            mView = null;
        }
    }
}

contact文件夹下LoginContract

import com.bwie.test.bean.UserBean;
import com.bwie.test.myuploadyue.base.BaseContract;

public interface LoginContract {
    interface View extends BaseContract.BaseView {
        void loginSuccess(UserBean userBean);
    }

    interface Presenter extends BaseContract.BasePresenter<View> {
        void login(String mobile, String password);
    }
}

RegisterContract

import com.bwie.test.bean.RegisterBean;
import com.bwie.test.myuploadyue.base.BaseContract;

public interface RegisterContract {
    interface View extends BaseContract.BaseView{
        void registerSuccess(RegisterBean registerBean);
    }

    interface Presenter extends BaseContract.BasePresenter<View>{
        void register(String mobile, String password);
    }
}

UpdateHeaderContract

import com.bwie.test.myuploadyue.base.BaseContract;

public interface UpdateHeaderContract {
    interface View extends BaseContract.BaseView{
        void updateSuccess(String code);
    }

    interface Presenter extends BaseContract.BasePresenter<View>{
        void updateHeader(String uid, String filePath);
    }
}

presenter文件夹下LoginPresenter

import com.bwie.test.bean.UserBean;
import com.bwie.test.myuploadyue.base.BasePresenter;
import com.bwie.test.myuploadyue.contact.LoginContract;
import com.bwie.test.net.LoginApi;

import javax.inject.Inject;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class LoginPresenter extends BasePresenter<LoginContract.View> implements LoginContract.Presenter {
    private LoginApi loginApi;

    @Inject
    public LoginPresenter(LoginApi loginApi) {
        this.loginApi = loginApi;
    }

    @Override
    public void login(String mobile, String password) {
        loginApi.login(mobile, password)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Observer<UserBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(UserBean userBean) {
                        mView.loginSuccess(userBean);
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}

RegisterPresenter

import com.bwie.test.bean.RegisterBean;
import com.bwie.test.myuploadyue.base.BasePresenter;
import com.bwie.test.myuploadyue.contact.RegisterContract;
import com.bwie.test.net.RegisterApi;

import javax.inject.Inject;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class RegisterPresenter extends BasePresenter<RegisterContract.View> implements 
                      RegisterContract.Presenter{
    RegisterApi registerApi;

    @Inject
    public RegisterPresenter(RegisterApi registerApi) {
        this.registerApi = registerApi;
    }

    @Override
    public void register(String mobile, String password) {
        registerApi.register(mobile, password)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Observer<RegisterBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(RegisterBean registerBean) {
                        mView.registerSuccess(registerBean);
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}

UpdatePresenter

import com.bwie.test.bean.BaseBean;
import com.bwie.test.myuploadyue.base.BasePresenter;
import com.bwie.test.myuploadyue.contact.UpdateHeaderContract;
import com.bwie.test.net.UpdateHeaderApi;

import java.io.File;
import javax.inject.Inject;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

public class UpdatePresenter extends BasePresenter<UpdateHeaderContract.View> implements
                       UpdateHeaderContract.Presenter{
    private UpdateHeaderApi updateHeaderApi;

    @Inject
    public UpdatePresenter(UpdateHeaderApi updateHeaderApi) {
        this.updateHeaderApi = updateHeaderApi;
    }

    @Override
    public void updateHeader(String uid, String filePath) {
        int i = filePath.lastIndexOf("/");
        String fileName = filePath.substring(i+1);
        RequestBody file = RequestBody.create( MediaType.parse("application/octet-stream"), 
                             new File(filePath));

        MediaType textType = MediaType.parse("text/plain");
        RequestBody u = RequestBody.create(textType, uid);
        MultipartBody.Part f = MultipartBody.Part.createFormData("file", fileName, file);
        updateHeaderApi.updateHeader(u, f)
                .subscribeOn( Schedulers.io())
                .observeOn( AndroidSchedulers.mainThread())
                .map(new Function<BaseBean, String>() {
                    @Override
                    public String apply(BaseBean baseBean) throws Exception {
                        return baseBean.getCode();
                    }
                }).subscribe(new Consumer<String>() {
            @Override
            public void accept(String s) throws Exception {
                if (mView!=null){
                    mView.updateSuccess(s);
                }
            }
        });
    }
}

component文件夹下HttpComponent

import com.bwie.test.module.HttpModule;
import com.bwie.test.myuploadyue.LoginActivity;
import com.bwie.test.myuploadyue.MyFragment;
import com.bwie.test.myuploadyue.RegistActivity;
import com.bwie.test.myuploadyue.UserInfoActivity;
import dagger.Component;

@Component(modules = HttpModule.class)
public interface HttpComponent {
    void inject(UserInfoActivity userInfoActivity);
    //void inject(MyFragment myFragment);
    void inject(LoginActivity loginActivity);
    void inject(RegistActivity registActivity);
}

inter文件夹下IBase

import android.view.View;

public interface IBase {
    int getContentLayout();
    void inject();
    void initView(View view);
}

OnItemClickListener

public interface OnItemClickListener {
    void onItemClick(int position);
    void onLongItemClick(int position);
}

module文件夹下HttpModule

import com.bwie.test.net.Api;
import com.bwie.test.net.LoginApi;
import com.bwie.test.net.LoginApiService;
import com.bwie.test.net.RegisterApi;
import com.bwie.test.net.RegisterApiService;
import com.bwie.test.net.UpdateHeaderApi;
import com.bwie.test.net.UpdateHeaderApiService;
import com.bwie.test.utils.MyInterceptor;

import java.util.concurrent.TimeUnit;
import dagger.Module;
import dagger.Provides;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

@Module
public class HttpModule {
    @Provides
    OkHttpClient.Builder provideOkHttpClientBuilder() {
        return new OkHttpClient.Builder()
                .writeTimeout(20, TimeUnit.SECONDS)
                .readTimeout(20, TimeUnit.SECONDS)
                .connectTimeout(10, TimeUnit.SECONDS);
    }

    @Provides
    Retrofit.Builder provideRetrofit(OkHttpClient.Builder builder){
        builder.addInterceptor(new MyInterceptor());
        return new Retrofit.Builder()
                .baseUrl(Api.BASEURL)
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .client(builder.build());
    }

    @Provides
    LoginApi provideLoginApi(Retrofit.Builder builder) {
        Retrofit retrofit = builder.build();
        LoginApiService loginApiService = retrofit.create(LoginApiService.class);
        return LoginApi.getLoginApi(loginApiService);
    }

    @Provides
    RegisterApi provideRegisterApi(Retrofit.Builder builder){
        Retrofit retrofit = builder.build();
        RegisterApiService registerApiService = retrofit.create( RegisterApiService.class );
        return RegisterApi.getRegisterApi(registerApiService);

    }

    @Provides
    UpdateHeaderApi provideUpdateHeaderApi(Retrofit.Builder builder) {
        Retrofit retrofit = builder.build();
        UpdateHeaderApiService updateHeaderApiService = retrofit.create(UpdateHeaderApiService.class);
        return UpdateHeaderApi.getUpdateHeaderApi(updateHeaderApiService);
    }
}

net文件夹下Api

public interface Api {
    String BASEURL = "https://www.zhaoapi.cn/";
}

LoginApi

import com.bwie.test.bean.UserBean;
import io.reactivex.Observable;

public class LoginApi {
    private static LoginApi loginApi;
    private LoginApiService loginApiService;

    private LoginApi(LoginApiService loginApiService) {
        this.loginApiService = loginApiService;
    }

    public static LoginApi getLoginApi(LoginApiService loginApiService) {
        if (loginApi == null) {
            loginApi = new LoginApi(loginApiService);
        }
        return loginApi;
    }

    public Observable<UserBean> login(String mobile, String password) {
        return loginApiService.login(mobile, password);
    }
}

LoginApiService

import com.bwie.test.bean.UserBean;
import io.reactivex.Observable;
import retrofit2.http.Field;
import retrofit2.http.FormUrlEncoded;
import retrofit2.http.POST;

public interface LoginApiService {
    @FormUrlEncoded
    @POST("user/login")
    Observable<UserBean> login(@Field("mobile") String mobile, @Field("password") String password);
}

RegisterApi

import com.bwie.test.bean.RegisterBean;
import io.reactivex.Observable;

public class RegisterApi {
    private static RegisterApi registerApi;
    private RegisterApiService registerApiService;

    public RegisterApi(RegisterApiService registerApiService) {
        this.registerApiService = registerApiService;
    }

    public static RegisterApi getRegisterApi(RegisterApiService registerApiService){
        if (registerApi==null){
            registerApi=new RegisterApi(registerApiService);
        }
        return registerApi;
    }
    public Observable<RegisterBean> register(String mobile, String password){
        return registerApiService.register(mobile,password);
    }
}

RegisterApiService

import com.bwie.test.bean.RegisterBean;
import io.reactivex.Observable;
import retrofit2.http.Field;
import retrofit2.http.FormUrlEncoded;
import retrofit2.http.POST;

public interface RegisterApiService {
    @FormUrlEncoded
    @POST("user/reg")
    Observable<RegisterBean> register(@Field("mobile") String mobile, @Field("password") String password);
}

UpdateHeaderApi

import com.bwie.test.bean.BaseBean;
import io.reactivex.Observable;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

public class UpdateHeaderApi {
    private static UpdateHeaderApi updateHeaderApi;
    private UpdateHeaderApiService updateHeaderApiService;

    private UpdateHeaderApi(UpdateHeaderApiService updateHeaderApiService) {
        this.updateHeaderApiService = updateHeaderApiService;
    }

    public static UpdateHeaderApi getUpdateHeaderApi(UpdateHeaderApiService updateHeaderApiService) {
        if (updateHeaderApi == null) {
            updateHeaderApi = new UpdateHeaderApi(updateHeaderApiService);
        }
        return updateHeaderApi;
    }

    public Observable<BaseBean> updateHeader(RequestBody uid, MultipartBody.Part file) {
        return updateHeaderApiService.updateHeader(uid, file);
    }
}

UpdateHeaderApiService

import com.bwie.test.bean.BaseBean;
import io.reactivex.Observable;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import retrofit2.http.Multipart;
import retrofit2.http.POST;
import retrofit2.http.Part;

public interface UpdateHeaderApiService {
    @Multipart
    @POST("file/upload")
    Observable<BaseBean> updateHeader(@Part("uid") RequestBody uid, @Part MultipartBody.Part file);
}

utils文件夹下GlideImageLoader

import android.content.Context;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
import com.youth.banner.loader.ImageLoader;

public class GlideImageLoader extends ImageLoader {
    @Override
    public void displayImage(Context context, Object path, ImageView imageView) {
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        //Glide 加载图片简单用法
        Glide.with(context).load(path).into(imageView);
    }
}

MyInterceptor

import java.io.IOException;
import okhttp3.FormBody;
import okhttp3.HttpUrl;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import retrofit2.http.FormUrlEncoded;

public class MyInterceptor implements Interceptor {
    private Response response;

    @Override
    public Response intercept(Chain chain) throws IOException {

        //区分是GET请求还是POST请求
        Request originRequest = chain.request();
        if ("GET".equals(originRequest.method())) {
            HttpUrl httpUrl = originRequest.url()
                    .newBuilder()
                    .addQueryParameter("source", "android")
                    .addQueryParameter("appVersion","101")
                    .build();
            Request request = new Request.Builder().url(httpUrl).build();
            //发送请求
            response = chain.proceed(request);
        } else if ("POST".equals(originRequest.method())) {
            //POST请求
            FormBody.Builder builder = new FormBody.Builder();
            //获取原始的请求体里的参数
            RequestBody body1 = originRequest.body();
            if(body1 instanceof FormBody){
            FormBody formBody = (FormBody) originRequest.body();
            //遍历原始的请求体里的参数
            for (int i = 0; i < formBody.size(); i++) {
                builder.add(formBody.name(i), formBody.value(i));
            }
            //添加新参数
            builder.add("source", "android");
            builder.add("appVersion","101");
            FormBody body = builder.build();
            //添加请求的参数
            Request request = originRequest.newBuilder()
                    .url(originRequest.url())
                    .post(body)
                    .build();
            response = chain.proceed(request);
            } else {
                response = chain.proceed(originRequest);
            }
        } else {
            response = chain.proceed(originRequest);
        }
        return response;
    }
}

SharedPreferencesUtils

import android.content.Context;
import android.content.SharedPreferences;

public class SharedPreferencesUtils {
   /**
    * 保存在手机里面的文件名
    */
   private static final String FILE_NAME = "share_date";

   public static void setParam(Context context , String key, Object object){
      String type = object.getClass().getSimpleName();
      SharedPreferences sp = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
      SharedPreferences.Editor editor = sp.edit();
      
      if("String".equals(type)){
         editor.putString(key, (String)object);
      }
      else if("Integer".equals(type)){
         editor.putInt(key, (Integer)object);
      }
      else if("Boolean".equals(type)){
         editor.putBoolean(key, (Boolean)object);
      }
      else if("Float".equals(type)){
         editor.putFloat(key, (Float)object);
      }
      else if("Long".equals(type)){
         editor.putLong(key, (Long)object);
      }
      editor.commit();
   }

   public static Object getParam(Context context , String key, Object defaultObject){
      String type = defaultObject.getClass().getSimpleName();
      SharedPreferences sp = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
      
      if("String".equals(type)){
         return sp.getString(key, (String)defaultObject);
      }
      else if("Integer".equals(type)){
         return sp.getInt(key, (Integer)defaultObject);
      }
      else if("Boolean".equals(type)){
         return sp.getBoolean(key, (Boolean)defaultObject);
      }
      else if("Float".equals(type)){
         return sp.getFloat(key, (Float)defaultObject);
      }
      else if("Long".equals(type)){
         return sp.getLong(key, (Long)defaultObject);
      }
      return null;
   }

   public static void clear(Context context){
      SharedPreferences sp = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
      sp.edit().clear().commit();
   }
}

bean文件夹下BaseBean

public class BaseBean {

    private String msg;
    private String code;

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }
}

UserBean

public class UserBean {
    private String msg;
    private String code;
    private DataBean data;

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public DataBean getData() {
        return data;
    }

    public void setData(DataBean data) {
        this.data = data;
    }

    public static class DataBean {

        private Object age;
        private String appkey;
        private String appsecret;
        private String createtime;
        private Object email;
        private Object fans;
        private Object follow;
        private Object gender;
        private Object icon;
        private Object latitude;
        private Object longitude;
        private String mobile;
        private Object money;
        private Object nickname;
        private String password;
        private Object praiseNum;
        private String token;
        private int uid;
        private Object userId;
        private String username;

        public Object getAge() {
            return age;
        }

        public void setAge(Object age) {
            this.age = age;
        }

        public String getAppkey() {
            return appkey;
        }

        public void setAppkey(String appkey) {
            this.appkey = appkey;
        }

        public String getAppsecret() {
            return appsecret;
        }

        public void setAppsecret(String appsecret) {
            this.appsecret = appsecret;
        }

        public String getCreatetime() {
            return createtime;
        }

        public void setCreatetime(String createtime) {
            this.createtime = createtime;
        }

        public Object getEmail() {
            return email;
        }

        public void setEmail(Object email) {
            this.email = email;
        }

        public Object getFans() {
            return fans;
        }

        public void setFans(Object fans) {
            this.fans = fans;
        }

        public Object getFollow() {
            return follow;
        }

        public void setFollow(Object follow) {
            this.follow = follow;
        }

        public Object getGender() {
            return gender;
        }

        public void setGender(Object gender) {
            this.gender = gender;
        }

        public Object getIcon() {
            return icon;
        }

        public void setIcon(Object icon) {
            this.icon = icon;
        }

        public Object getLatitude() {
            return latitude;
        }

        public void setLatitude(Object latitude) {
            this.latitude = latitude;
        }

        public Object getLongitude() {
            return longitude;
        }

        public void setLongitude(Object longitude) {
            this.longitude = longitude;
        }

        public String getMobile() {
            return mobile;
        }

        public void setMobile(String mobile) {
            this.mobile = mobile;
        }

        public Object getMoney() {
            return money;
        }

        public void setMoney(Object money) {
            this.money = money;
        }

        public Object getNickname() {
            return nickname;
        }

        public void setNickname(Object nickname) {
            this.nickname = nickname;
        }

        public String getPassword() {
            return password;
        }

        public void setPassword(String password) {
            this.password = password;
        }

        public Object getPraiseNum() {
            return praiseNum;
        }

        public void setPraiseNum(Object praiseNum) {
            this.praiseNum = praiseNum;
        }

        public String getToken() {
            return token;
        }

        public void setToken(String token) {
            this.token = token;
        }

        public int getUid() {
            return uid;
        }

        public void setUid(int uid) {
            this.uid = uid;
        }

        public Object getUserId() {
            return userId;
        }

        public void setUserId(Object userId) {
            this.userId = userId;
        }

        public String getUsername() {
            return username;
        }

        public void setUsername(String username) {
            this.username = username;
        }
    }
}

RegisterBean

public class RegisterBean {
    private String msg;
    private String code;
    private String data;

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getData() {
        return data;
    }

    public void setData(String data) {
        this.data = data;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值