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);}
//Vpublic interface IView { //请求成功方法 void onSuccess(String message); //请求失败方法 void onFailed(String error);}//Ppublic 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); } }); }}//Applicationpublic 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'
阅读全文
0 0
- oKhttp第三方登录
- Android通过微信实现第三方登录并使用OKHttp获得Token及源码下载
- 新浪第三方登录
- 第三方帐号登录
- 第三方登录
- QQ第三方登录
- Android 第三方登录
- QQ第三方登录
- 第三方登录流程图
- QQ第三方登录
- 豆瓣第三方登录
- 第三方登录
- php 第三方登录
- QQ第三方登录
- Android 第三方登录
- 第三方登录
- 第三方登录
- QQ第三方登录
- spring 多资源文件
- 小白学习Hibernate遇到的错误以及解决办法(GenerationTarget encountered exception accepting command : Error executing)
- Implementing a CNN for Human Activity Recognition in Tensorflow
- 改进排序算法:归并排序
- 用链表+函数指针+定时器中断实现的一个软件定时器(试用于所有单片机)
- oKhttp第三方登录
- 0基础lua学习(十六)lua的多态 base
- Vim 命令行快捷键
- 对list里的数据进行大小的排序
- Mac+windows下excel转化为复杂的json格式,将xlsx文件转成json(可内嵌对象数组)
- 慕课网《HTML+CSS基础课程》笔记总结(一)
- 选择恐惧症的福音!教你认清MVC,MVP和MVVM
- 共享时代的新宠儿自动售菜机
- 数据排序:冒泡排序