一、效果
二、布局
<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"
tools:context=".MainActivity"
android:orientation="vertical">
<EditText
android:id="@+id/qq_number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="15dp"
android:hint="请输入qq号:"/>
<EditText
android:id="@+id/pwd_number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="15dp"
android:hint="请输入密码:"/>
<Button
android:id="@+id/login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="POST异步登录"/>
<Button
android:id="@+id/login_post"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="POST同步登录"/>
<Button
android:id="@+id/login_getasy"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Get异步登录"/>
<Button
android:id="@+id/login_getsy"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Get同步步登录"/>
<TextView
android:id="@+id/result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="log.."
android:textSize="25sp"
android:textColor="#000"/>
</LinearLayout>
三、MainActivity代码,需要注意的就是我的接口已经用不了了需要自己创或找接口
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private String strUrl = "http://169.254.53.96:8080/web/LoginServlet" ;
private EditText mQq;
private EditText mPwd;
private TextView mResultText;
private FormBody mFormBody;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化控件
initView();
}
private void initView() {
mQq = findViewById(R.id.qq_number);
mPwd = findViewById(R.id.pwd_number);
mResultText = findViewById(R.id.result);
Button login = findViewById(R.id.login);
login.setOnClickListener(this);
Button login_post = findViewById(R.id.login_post);
login_post.setOnClickListener(this);
Button login_getasy = findViewById(R.id.login_getasy);
login_getasy.setOnClickListener(this);
Button login_getsy = findViewById(R.id.login_getsy);
login_getsy.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.login:
//post异步请求
specific();
break;
case R.id.login_post:
//post同步请求
lpSpecific();
break;
case R.id.login_getasy:
//get异步
getAsy();
break;
case R.id.login_getsy:
//get同步
getSy();
break;
}
}
//get同步
private void getSy() {
//获取文本输入的信息进行非空判断
final String qq = mQq.getText().toString().trim();
final String pwd = mPwd.getText().toString().trim();
if(TextUtils.isEmpty(qq) || TextUtils.isEmpty(pwd)){
Toast.makeText(this, "用户名或密码不能为空", Toast.LENGTH_SHORT).show();
return;
}
new Thread(new Runnable() {
@Override
public void run() {
OkHttpClient client = new OkHttpClient.Builder()
.readTimeout(8, TimeUnit.SECONDS)
.connectTimeout(8, TimeUnit.SECONDS)
.build();
Request request = new Request.Builder()
.url("http://169.254.53.96:8080/web/LoginServlet?qq=" + qq + "&pwd=" + pwd)
.build();
Call call = client.newCall(request);
try {
Response response = call.execute();
//注意string和tostring的区别
final String string = response.body().string();
runOnUiThread(new Runnable() {
@Override
public void run() {
mResultText.setText(string);
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
//get异步
private void getAsy() {
//获取文本输入的信息进行非空判断
String qq = mQq.getText().toString().trim();
String pwd = mPwd.getText().toString().trim();
if(TextUtils.isEmpty(qq) || TextUtils.isEmpty(pwd)){
Toast.makeText(this, "用户名或密码不能为空", Toast.LENGTH_SHORT).show();
return;
}
//创建okHttpClient
OkHttpClient client = new OkHttpClient.Builder()
.connectTimeout(8, TimeUnit.SECONDS)
.readTimeout(8, TimeUnit.SECONDS)
.build();
//创建request
Request request = new Request.Builder().url("http://169.254.53.96:8080/web/LoginServlet?qq="+ qq + "&pwd=" +pwd).build();
//创建call对象
Call call = client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, "请求错误", Toast.LENGTH_SHORT).show();
}
});
}
@Override
public void onResponse(Call call, Response response) throws IOException {
final String string = response.body().string();
runOnUiThread(new Runnable() {
@Override
public void run() {
mResultText.setText(string);
}
});
}
});
}
//post同步
private void lpSpecific() {
//获取文本输入的信息进行非空判断
final String qq2 = mQq.getText().toString().trim();
final String pwd2 = mPwd.getText().toString().trim();
if(TextUtils.isEmpty(qq2) || TextUtils.isEmpty(pwd2)){
Toast.makeText(this, "用户名或密码不能为空", Toast.LENGTH_SHORT).show();
return;
}
new Thread(new Runnable() {
@Override
public void run() {
//创建okhttpclient
OkHttpClient client = new OkHttpClient.Builder()
.readTimeout(10,TimeUnit.SECONDS)
.connectTimeout(10,TimeUnit.SECONDS)
.build();
FormBody formBody = new FormBody.Builder()
.add("qq", qq2)
.add("pwd", pwd2)
.build();
Request request = new Request.Builder()
.post(formBody)
.url(strUrl)
.build();
try {
Response response = client.newCall(request).execute();
//注意string和tostring的区别
final String string = response.body().string();
runOnUiThread(new Runnable() {
@Override
public void run() {
mResultText.setText(string);
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
//post异步
private void specific() {
//获取文本输入的信息进行非空判断
String qq = mQq.getText().toString().trim();
String pwd = mPwd.getText().toString().trim();
if(TextUtils.isEmpty(qq) || TextUtils.isEmpty(pwd)){
Toast.makeText(this, "用户名或密码不能为空", Toast.LENGTH_SHORT).show();
return;
}
//创建okhttpClient
OkHttpClient client = new OkHttpClient.Builder()
//读取超时
.readTimeout(10,TimeUnit.SECONDS)
//连接超时
.connectTimeout(10,TimeUnit.SECONDS)
//写入超时
.writeTimeout(10,TimeUnit.SECONDS)
.build();
//创建表单
mFormBody = new FormBody.Builder()
.add("qq", qq)
.add("pwd", pwd)
.build();
//创建request
Request request = new Request.Builder()
//请求方法(也是比get多的一个方法)
.post(mFormBody)
.url(strUrl)
.build();
//创建call对象
Call call = client.newCall(request);
//异步的接口
call.enqueue(new Callback() {
@Override//失败
public void onFailure(Call call, IOException e) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, "请求错误", Toast.LENGTH_SHORT).show();
}
});
}
@Override//成功
public void onResponse(Call call, Response response) throws IOException {
final String string = response.body().string();
runOnUiThread(new Runnable() {
@Override
public void run() {
mResultText.setText(string);
}
});
}
});
}
}