//实现接口,实现接口方法 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'
oKhttp第三方登录
最新推荐文章于 2024-06-17 11:23:46 发布
4845

被折叠的 条评论
为什么被折叠?



