activity_main
<?xml version="1.0" encoding="utf-8"?>
<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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="用户名"
android:textSize="17sp" />
<EditText
android:id="@+id/ed_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密 码"
android:textSize="17sp" />
<EditText
android:id="@+id/ed_pwd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10" />
</LinearLayout>
<Button
android:id="@+id/login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登录"
android:textSize="17sp"
android:layout_gravity="center"
android:onClick="login"/>
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
MainActivity
package wanghuiqi.bawie.com.post_okhttp_login;
import android.annotation.SuppressLint;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
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.concurrent.TimeUnit;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class MainActivity extends AppCompatActivity {
private EditText edName;
private EditText edPwd;
private String path = "http://169.254.53.96:8080/web/LoginServlet";
private TextView text;
private static final int SUCCESS = 665;
private static final int FALL = 894;
@SuppressLint("HandlerLeak")
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case SUCCESS:
String success_text = (String) msg.obj;
text.setText(success_text);
break;
case FALL:
Toast.makeText(MainActivity.this, "没有网啊!亲", Toast.LENGTH_SHORT).show();
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化控件
initView();
}
private void initView() {
edName = findViewById(R.id.ed_name);
edPwd = findViewById(R.id.ed_pwd);
text = findViewById(R.id.text);
}
public void login(View view) {
//获取输入框的内容
String qq = edName.getText().toString().trim();
String pwd = edPwd.getText().toString().trim();
if (TextUtils.isEmpty(qq) || TextUtils.isEmpty(pwd)) {
Toast.makeText(this, "输入不能为空", Toast.LENGTH_SHORT).show();
return;
}
//创建OKHttpClient
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.connectTimeout(10, TimeUnit.SECONDS)
.readTimeout(10, TimeUnit.SECONDS)
.writeTimeout(10, TimeUnit.SECONDS)
.build();
FormBody formBody = new FormBody.Builder()
.add("qq", qq).add("pwd", pwd)
.build();
Request request = new Request.Builder()
.post(formBody)
.url(path)
.build();
//创建call
Call call = okHttpClient.newCall(request);
//异步
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
//失败时
handler.sendEmptyMessage(FALL);
}
@Override
public void onResponse(Call call, Response response) throws IOException {
//成功时
String string = response.body().string();
Message msg = Message.obtain();
msg.obj = string;
msg.what = SUCCESS;
handler.sendMessage(msg);
}
});
}
}