// MainActivity页面
package c.example.wang.myapplication413_4;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
/**
* 请输入手机号
*/
private EditText mEtName;
/**
* 请输入密码
*/
private EditText mEtPwd;
/**
* 注册
*/
private Button mBtLogin;
private static String SUBMIT_URL = "https://www.zhaoapi.cn/user/reg?mobile=%s&password=%s";
private static String URL = "https://www.zhaoapi.cn/user/reg";
private TextView mTv;
/**
* 新的注册方式
*/
private Button mBtLogin2;
/**
* 截取字符串
*/
private Button mBtLogin3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
// initView();
}
private void initView() {
mEtName = (EditText) findViewById(R.id.etName);
mEtPwd = (EditText) findViewById(R.id.etPwd);
mBtLogin = (Button) findViewById(R.id.btLogin);
mBtLogin.setOnClickListener(this);
mTv = (TextView) findViewById(R.id.tv);
mBtLogin2 = (Button) findViewById(R.id.btLogin2);
mBtLogin2.setOnClickListener(this);
mTv.setOnClickListener(this);
mBtLogin3 = (Button) findViewById(R.id.btLogin3);
mBtLogin3.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
default:
break;
case R.id.btLogin:
//获取用户输入的用户名和密码
String name = mEtName.getText().toString();
String pwd = mEtPwd.getText().toString();
//验证用户名或密码是否正确
if (checkPhone(name) && !checkPwd(pwd)) {
//使用Okhttp进行网络请求
// submit(name, pwd);
submit3(name, pwd);
} else {
Toast.makeText(MainActivity.this, "账号密码输入有误!!!", Toast.LENGTH_SHORT).show();
}
break;
case R.id.btLogin2:
// method1();
//获取用户输入的用户名和密码
String n = mEtName.getText().toString();
String p = mEtPwd.getText().toString();
//验证用户名或密码是否正确
if (checkPhone(n) && !checkPwd(p)) {
method2(n, p);
}
break;
case R.id.tv:
break;
case R.id.btLogin3:
String s = "?mobile=12354678954&password=123456&";
String substring = s.substring(0, s.length() - 1);
Log.e("MainActivity", "substring = " + substring);
break;
}
}
private void method2(String phone, String pwd) {
OkhttpUtils okhttpUtils = OkhttpUtils.getInstance();
Map<String, String> params = new HashMap<>();
params.put("mobile", phone);
params.put("password", pwd);
okhttpUtils.doGet(URL, params);
}
private void method1() {
OkhttpUtils okhttpUtils = OkhttpUtils.getInstance();
//获取用户输入的用户名和密码
String n = mEtName.getText().toString();
String p = mEtPwd.getText().toString();
//验证用户名或密码是否正确
if (checkPhone(n) && !checkPwd(p)) {
String url = String.format(SUBMIT_URL, n, p);
okhttpUtils.doGet(url);
} else {
Toast.makeText(MainActivity.this, "账号密码输入有误!!!", Toast.LENGTH_SHORT).show();
}
}
* post请求
*
* @param name
* @param pwd
*/
private void submit3(String name, String pwd) {
//创建OkHttpClient对象
OkHttpClient okHttpClient = new OkHttpClient();
//创建Request
FormBody.Builder builder = new FormBody.Builder();
builder.add("mobile", name);
builder.add("password", pwd);
FormBody formBody = builder.build();
Request request = new Request.Builder().url("https://www.zhaoapi.cn/user/reg").post(formBody).build();
//请求数据
okHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
final String string = response.body().string();
runOnUiThread(new Runnable() {
@Override
public void run() {
mTv.setText(string);
}
});
}
});
}
/**
* 同步请求
*
* @param name
* @param pwd
*/
private void submit2(String name, String pwd) {
final OkHttpClient okHttpClient = new OkHttpClient();
String url = String.format(SUBMIT_URL, name, pwd);
final Request request = new Request.Builder().url(url).build();
new Thread(new Runnable() {
@Override
public void run() {
try {
Response response = okHttpClient.newCall(request).execute();
String string = response.body().string();
Log.e("MainActivity", "string = " + string);
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
/**
* 注册
*
* @param name
* @param pwd
*/
private void submit(String name, String pwd) {
//创建OkhttpClient
OkHttpClient okHttpClient = new OkHttpClient();
//创建Requet对象
String url = String.format(SUBMIT_URL, name, pwd);
Log.e("MainActivity", "url = " + url);
Request request = new Request.Builder().url(url).build();
Log.e("MainActivity", "线程名 = " + Thread.currentThread().getName());
//进行请求
okHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.e("MainActivity", "onFailure");
}
@Override
public void onResponse(Call call, Response response) throws IOException {
final String string = response.body().string();
Log.e("MainActivity", "线程名 = " + Thread.currentThread().getName());
Log.e("MainActivity", "string = " + string);
runOnUiThread(new Runnable() {
@Override
public void run() {
mTv.setText(string);
}
});
}
});
}
/**
* 验证手机号是否正确
*
* @return
*/
private boolean checkPhone(String phone) {
Pattern p = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$");
Matcher m = p.matcher(phone);
return m.matches();
}
/**
* 验证密码是否为空
*
* @param pwd
* @return
*/
private boolean checkPwd(String pwd) {
return TextUtils.isEmpty(pwd);
}
}
//工具类
package c.example.wang.myapplication413_4;
import android.util.Log;
import java.io.IOException;
import java.util.Map;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class OkhttpUtils {
//private static OkhttpUtils okhttpUtils = new OkhttpUtils();//饿汉式
private static OkhttpUtils okhttpUtils;
private final OkHttpClient okHttpClient;
//构造方法要私有化
private OkhttpUtils() {
//创建OkhttpClient
okHttpClient = new OkHttpClient();
}
//懒汉式
public static OkhttpUtils getInstance() {
if (okhttpUtils == null) {
okhttpUtils = new OkhttpUtils();
}
return okhttpUtils;
}
/*饿汉式
public static OkhttpUtils getInstance(){
return okhttpUtils;
}*/
public void doGet(String url, Map<String, String> params) {
//判断params是否为null
if (params != null) {
StringBuilder sb = new StringBuilder();
sb.append("?");
for (Map.Entry<String, String> entry : params.entrySet()) {
sb.append(entry.getKey());
sb.append("=");
sb.append(entry.getValue());
sb.append("&");
}
//?mobile=12354678954&password=123456&
String s = sb.toString();
String strParam = s.substring(0, s.length() - 1);
url += strParam;
Log.e("sdfasdf", "url = " + url);
}
Request request = new Request.Builder().url(url).build();
okHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
final String string = response.body().string();
Log.e("MainActivity", "string = " + string);
}
});
}
/**
* GET请求
*/
public void doGet(String url) {
//创建Requet对象
Request request = new Request.Builder().url(url).build();
//进行请求
okHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.e("MainActivity", "onFailure");
}
@Override
public void onResponse(Call call, Response response) throws IOException {
final String string = response.body().string();
Log.e("MainActivity", "string = " + string);
}
});
}
}
//MainActivity的副本
<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=".MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#00ff00"
android:gravity="center"
android:text="注册"
android:textColor="#ffffff"
android:textSize="25sp"/>
<EditText
android:id="@+id/etName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入手机号"
/>
<EditText
android:id="@+id/etPwd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入密码"
/>
<Button
android:id="@+id/btLogin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ff0000"
android:text="注册"/>
<Button
android:id="@+id/btLogin2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="新的注册方式"/>
<Button
android:id="@+id/btLogin3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="截取字符串"/>
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
// 记得联网请求