android 之OkHttp封装模板

1.activity_main.xml

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

    <TextView
        android:id="@+id/txt_show"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="hello world" />

    <EditText
        android:id="@+id/et_username"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        android:layout_marginTop="100dp"
        android:hint="请输入用户名" />

    <EditText
        android:id="@+id/et_password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入密码" />

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

</LinearLayout>

2.MainActivity.java

package com.bwie.loginmvpdemo;

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.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener,IView {
    private static final String TAG = "MainActivity";

    private EditText etUsername;
    private EditText etPassword;
    private Button btnLogin;
    private TextView txtShow;

    private  LoginPresenter presenter;

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

        etUsername = (EditText) findViewById(R.id.et_username);
        etPassword = (EditText) findViewById(R.id.et_password);
        btnLogin = (Button) findViewById(R.id.btn_login);
        txtShow = (TextView) findViewById(R.id.txt_show);

        btnLogin.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_login:
                String username = etUsername.getText().toString().trim();
                String password  =etPassword.getText().toString().trim();

                // 创建P的对象
                presenter = new LoginPresenter();
                presenter.attachView(this);

//                LoginPresenter presenter1 = new LoginPresenter(this);
                // 并且调用P层的方法
                presenter.login(username, password);
                break;
        }
    }

    @Override
    public void success(String data) {
        // 接收到P层传过来的数据之后做修改View的操作
        txtShow.setText("登录成功");
    }

    @Override
    public void failed(String message) {
        txtShow.setText("登录失败");
    }


    // V层销毁之后调用P层提供的解绑方法
    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (presenter != null) {
            presenter.detatch();
        }
    }
}

3.IResponse.java

package com.bwie.loginmvpdemo;

/**
 * Created by WuXirui
 * Create Time: 2017/11/3
 * Description:
 */

public interface IResponse {
    void onSuccess(String data);
    void onFailed(String message);
}
4.IView.java

package com.bwie.loginmvpdemo;

/**
 * Created by WuXirui
 * Create Time: 2017/11/3
 * Description:
 */

// 第一步,定义一个View层的回调接口
public interface IView {
    void success(String data);
    void failed(String message);
}

5.HttpUtils.java

package com.bwie.loginmvpdemo;

import android.util.Log;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.Map;

/**
 * Created by WuXirui
 * Create Time: 2017/11/3
 * Description:
 */

public class HttpUtils {
    private static final String TAG = "HttpUtils";
    private static volatile HttpUtils instance;

    private HttpUtils() {

    }

    public static HttpUtils getInstance() {
        if (null == instance) {
            synchronized (HttpUtils.class) {
                if (instance == null) {
                    instance = new HttpUtils();
                }
            }
        }
        return instance;
    }


    public void get(String url, Map<String, String> map, final IResponse response) {
        RequestParams params = new RequestParams(url);
        for (Map.Entry<String, String> entry : map.entrySet()) {
            params.addQueryStringParameter(entry.getKey(), entry.getValue());
        }

        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Log.i(TAG, "onSuccess: " + result);
                response.onSuccess(result);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.e(TAG, "onError: " + ex.getMessage());
                response.onFailed(ex.getMessage());
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }
}

6.LoginPresenter.java

package com.bwie.loginmvpdemo;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by WuXirui
 * Create Time: 2017/11/3
 * Description:
 */

// 创建P层的类
public class LoginPresenter {

    private IView iView;

    // 提供初始化IView对象的一个方法

    public LoginPresenter() {

    }

    public LoginPresenter(IView iView) {
        this.iView = iView;
    }


    public void attachView(IView iView) {
        this.iView = iView;
    }


    /**
     * 登录
     * @param username
     * @param password
     */

    // 调用Model层的请求网络或数据库的方法
    public void login(String username, String password) {
        Map<String, String> map = new HashMap<>();
        map.put("mobile", username);
        map.put("password", password);
        HttpUtils.getInstance().get("http://120.27.23.105/user/login", map,
                new IResponse() {
                    @Override
                    public void onSuccess(String data) {
                        // 把M层拿到的数据回调给V层
                        iView.success(data);
                    }

                    @Override
                    public void onFailed(String message) {
                        iView.failed(message);
                    }
                });
    }

    /**
     * 提供解绑的方法,避免内存泄漏
     */
    public void detatch(){
        if (iView != null) {
            iView = null;
        }
    }
}

7.BaseApplication.java

package com.bwie.loginmvpdemo;

import android.app.Application;

import org.xutils.x;

/**
 * Created by WuXirui
 * Create Time: 2017/11/3
 * Description:
 */

public class BaseApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        x.Ext.init(this);
        x.Ext.setDebug(BuildConfig.DEBUG);
    }
}










  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值