HttpUrlConnection___登录(运用接口)

HttpUtils

package com.umeng.soexample.utils;

import android.os.Handler;
import android.os.Message;

import com.google.common.io.CharStreams;

import java.io.InputStreamReader;
import java.lang.reflect.Type;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class HttpHelpter {

    private static HttpHelpter instance = new HttpHelpter();

    private HttpHelpter() {

    }

    public static HttpHelpter getInstance() {
        return instance;
    }


    private void doHttp(final String urlPath) {
        new Thread() {
            @Override
            public void run() {
                super.run();
                try {
                    URL url = new URL(urlPath);
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    connection.setRequestMethod("GET");
                    connection.setConnectTimeout(5000);
                    connection.connect();
                    if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
                        String data = CharStreams.toString(new InputStreamReader(connection.getInputStream(), "UTF-8"));
                        Message message = Message.obtain();
                        message.obj = data;
                        message.what = 2000;
                        handler.sendMessage(message);

                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }.start();


    }

    private final int SUCCESS = 2000;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case SUCCESS:
                    String s = (String) msg.obj;
                    listener.success(s);
                    break;
            }

        }
    };

    public HttpHelpter doGet(String urlPath) {
        doHttp(urlPath);
        return this;
    }

    public interface HttpListener {
        void success(String data);
    }

    private HttpListener listener;

    public void result(HttpListener listener) {
        this.listener = listener;
    }


}

CallBack

package com.umeng.soexample.utils;


import com.umeng.soexample.bean.Product;

import java.util.List;

public interface CallBack {

    //结果接口
    void onSuccess(Object obj);

    void onFail(String e);

    void onData(List  list);


}

UrlHolder

package com.umeng.soexample.utils;

public class UrlHolder {

    private static final String BASE_URL = "http://www.zhaoapi.cn/";
    //登录的url
    public static final String LOGIN_URL = BASE_URL + "user/login";
    //注册的url
    public static final String REGISTER_URL = BASE_URL + "user/reg";
    //list  view 数据的展示
    public static final String LISTDATA_URL = "http://www.wanandroid.com/tools/mockapi/6523/restaurants_offset_0_limit_4";


}

view

package com.umeng.soexample.login;

public interface LoginView {

    void onSuccess(String result);

    void onFail(String msg);

}

model

package com.umeng.soexample.login;


import android.text.TextUtils;

import com.google.gson.Gson;
import com.umeng.soexample.bean.Bean;
import com.umeng.soexample.utils.CallBack;
import com.umeng.soexample.utils.HttpHelpter;
import com.umeng.soexample.utils.HttpUtils;
import com.umeng.soexample.utils.UrlHolder;

public class LoginModel {

    public void login(String name, String pwd, final CallBack callBack) {

        //处理账号和密码
        String par = "?mobile=" + name + "&password=" + pwd;

        HttpHelpter.getInstance().doGet(UrlHolder.LOGIN_URL + par).result(new HttpHelpter.HttpListener() {
            @Override
            public void success(String data) {
                Gson gson = new Gson();
                Bean bean = gson.fromJson(data, Bean.class);
                String code = bean.getCode();
                String msg = bean.getMsg();
                switch (code) {
                    case "0":
                        callBack.onSuccess(msg);
                        break;
                    case "1":
                        callBack.onFail(msg);
                        break;
                }
            }
        });


    }

}

presenter

package com.umeng.soexample.login;

import com.umeng.soexample.bean.Product;
import com.umeng.soexample.utils.CallBack;

import java.util.List;

public class LoginPresenter {

    //model
    private LoginModel loginModel;
    //view
    private LoginView loginView;

    public LoginPresenter(LoginView loginView) {
        this.loginView = loginView;
        loginModel = new LoginModel();

    }

    public void login(String name, String pwd) {
        loginModel.login(name, pwd, new CallBack() {
            @Override
            public void onSuccess(Object result) {
                loginView.onSuccess((String) result);
            }


            @Override
            public void onFail(String error) {
                loginView.onFail(error);
            }

            @Override
            public void onData(List list) {

            }
        });


    }
}

MainActivity
实例化p层,调用登录方法即可

你可能感兴趣的:(HttpUrlConnection___登录(运用接口))