oKhttp第三方登录

来源:互联网 发布:ubuntu win7双系统安装 编辑:程序博客网 时间:2024/05/21 17:09
//实现接口,实现接口方法public class MainActivity extends AppCompatActivity implements View.OnClickListener,IView {    private static final String TAG = "MainActivity";    private EditText etUserName;    private EditText etpassword;    private Button btnLogin;    //p层对象    private LoginPresenter presenter;    //内存泄漏    private RefWatcher refWatcher;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        etUserName = (EditText) findViewById(R.id.edit_username);        etpassword = (EditText) findViewById(R.id.edit_password);        btnLogin = (Button) findViewById(R.id.btn_login);        //初始化p        presenter = new LoginPresenter();        presenter.attachView(this);        btnLogin.setOnClickListener(this);        //内存泄漏        refWatcher= BaseApplication.getRefWatcher(this);        refWatcher.watch(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();                presenter.login(username,password);                 break;        }    }    //登录成功的方法    @Override    public void success(String message) {        Log.i(TAG,"success:"+message);    }    //登录失败的方法    @Override    public void failed(String message) {        Log.i(TAG,"failed:"+message);    }    //销毁内存泄漏    @Override    protected void onDestroy() {        super.onDestroy();    }}
//M层,网络请求
public class OkHttpUtils {    private OkHttpClient client;    private static volatile OkHttpUtils instance;    public OkHttpUtils(){        //创建OKHttpClient对象        client = new OkHttpClient();    }    //创建静态方法    //单例模式    public static OkHttpUtils getInstance(){        if(instance == null){            synchronized (OkHttpUtils.class){                if(null==instance){                    instance = new OkHttpUtils();                }            }        }        return instance;    }    // okhttp封装的get请求    public void get(String url, Map<String, String> map, final ResultCallBack resultCallBack){        StringBuffer buffer = new StringBuffer();        buffer.append(url).append("?");        for (Map.Entry<String, String> entry :map.entrySet()) {            buffer.append(entry.getKey())                    .append("=")                    .append(entry.getValue())                    .append("&");        }        buffer.deleteCharAt(buffer.lastIndexOf("&"));        Log.i(TAG, "url:" + buffer);        // 2.构建Request对象        final Request request = new Request.Builder()                .get()                .url(buffer.toString())                .build();        // 3.创建一个Call对象        Call call = client.newCall(request);        // 4. OKHttp提供的Callback回调接口        call.enqueue(new Callback() {            // 请求失败的回调方法            @Override            public void onFailure(Call call, IOException e) {                // 在子线程中返回                resultCallBack.onFailed(e.getMessage());            }            // 请求成功的回调方法            @Override            public void onResponse(Call call, Response response) throws IOException {                // 在子线程中返回                String result = response.body().string();                // 用我们自己的回调方法                resultCallBack.onSuccess(result);            }        });    }}
//获取网络数据成功失败的接口
public interface ResultCallBack {    //请求成功方法    void onSuccess(String message);    //请求失败方法    void onFailed(String error);}

//V
public interface IView {    //请求成功方法    void onSuccess(String message);    //请求失败方法    void onFailed(String error);}
//P
public class LoginPresenter {    private IView iv;    public void attachView(IView iv) {        this.iv = iv;    }    //创建登录方法    public void login(String name,String password){        //创建集合map存放数据        Map<String,String> map = new HashMap<>();        map.put("mobile",name);        map.put("password",password);        //创建请求数据的工具类(OkHttpUtils);        OkHttpUtils.getInstance().get("http://120.27.23.105/user/login", map, new ResultCallBack() {            //请求成功            @Override            public void onSuccess(String message) {                 iv.onSuccess(message);            }             //请求失败            @Override            public void onFailed(String error) {                iv.onFailed(error);            }        });    }}
//Application
public class BaseApplication extends Application{    private RefWatcher refWatcher;    public static RefWatcher getRefWatcher(Context context) {        BaseApplication application = (BaseApplication) context.getApplicationContext();        return application.refWatcher;    }    @Override    public void onCreate() {        super.onCreate();        refWatcher = LeakCanary.install(this);    }}
//XMl
<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="chendemin.bwei.com.okhttpdemo1.MainActivity">    <EditText        android:id="@+id/edit_name"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:hint="输入账号"/>    <EditText        android:id="@+id/edit_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>
//权限
<uses-permission android:name="android.permission.INTERNET" /><uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
android:name=".base.BaseApplication"
//依赖
compile 'com.squareup.okhttp3:okhttp:3.9.0'compile 'com.google.code.gson:gson:2.8.1'debugCompile 'com.squareup.leakcanary:leakcanary-android:1.3'releaseCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.3'

原创粉丝点击