設計思路:
1獲取用戶輸入的用戶名和密碼,並判斷是否爲空
2將獲取的用戶名和密碼,發送到服務器端,並進行驗證
3獲取返回的響應信息,並輸出到用戶界面
具體代碼:
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.text.TextUtils;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
private EditText mEtUserName;
private EditText mEtUserPassword;
private Button mBtnLogin;
private WebView mWvShow;
private StringBuffer mStrBuffer;
private MessageHandler mHandler = new MessageHandler();
private static final int INPUT_IS_EMPTY = 0;
private static final int REQUST_SUCCESS = 1;
private static final int REQUST_FAIL = 2;
private static final String DEFAULT_PARAMS_ENCODING = "utf-8";
private static final String MIME_TYPE = "text/html";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
mBtnLogin.setOnClickListener(this);
}
private void initView() {
mEtUserName = (EditText) findViewById(R.id.mainAc_et_userName);
mEtUserPassword = (EditText) findViewById(R.id.mainAc_et_userPassword);
mBtnLogin = (Button) findViewById(R.id.mainAc_btn_login);
mWvShow = (WebView) findViewById(R.id.mainAc_wv_show);
}
@Override
public void onClick(View v) {
// 獲取用戶輸入的用戶名和密碼
String userName = mEtUserName.getText().toString().trim();
String userPassword = mEtUserPassword.getText().toString().trim();
// 使用TextUtil 工具類,判斷用戶名和密碼是否爲空
if (TextUtils.isEmpty(userName) || TextUtils.isEmpty(userPassword)) {
mHandler.sendEmptyMessage(INPUT_IS_EMPTY);
return;
}
connectIntenet(userName, userPassword);
}
private void connectIntenet(final String userName, final String userPassword) {
new Thread() {
public void run() {
OutputStream os = null;
InputStream is = null;
try {
URL url = new URL(
"http://192.168.15.200:8088/UserManager/longin");
HttpURLConnection httpURLConnection = (HttpURLConnection) url
.openConnection();
// 設置httpURLConnection連接的屬性
httpURLConnection.setRequestMethod("POST");// 設置請求方式爲POST
httpURLConnection.setReadTimeout(8000);// 設置讀取超時時間
httpURLConnection.setConnectTimeout(8000);// 設置連接超時時間
// 設置
httpURLConnection.setDefaultUseCaches(false);// 設置是否使用緩衝區
httpURLConnection.setDoInput(true);
httpURLConnection.setDoOutput(true);
// 打開連接
httpURLConnection.connect();
// 向服務器發送數據
String requestStr = "username=" + userName + "&password="
+ userPassword;
os = httpURLConnection.getOutputStream();
os.write(requestStr.getBytes());
os.flush();
int code = httpURLConnection.getResponseCode();
System.out.println(code);
if (code == HttpURLConnection.HTTP_OK) {
// 從服務器得到數據
is = httpURLConnection.getInputStream();
mStrBuffer = new StringBuffer();
int length = 0;
byte[] bytes = new byte[1024 * 1024];
while ((length = is.read(bytes)) != -1) {
mStrBuffer.append(new String(bytes, 0, length));
}
mHandler.sendEmptyMessage(REQUST_SUCCESS);
} else {// 若連接誒不成功
mHandler.sendEmptyMessage(REQUST_FAIL);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (os != null && is != null) {
try {
os.close();
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
};
}.start();
}
class MessageHandler extends Handler {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case INPUT_IS_EMPTY:
Toast.makeText(MainActivity.this, "用戶名或密碼不能爲空",
Toast.LENGTH_SHORT).show();
break;
case REQUST_SUCCESS:
mWvShow.loadDataWithBaseURL(null, mStrBuffer.toString(),
MIME_TYPE, DEFAULT_PARAMS_ENCODING, null);
break;
case REQUST_FAIL:
Toast.makeText(MainActivity.this, "請求連接失敗", Toast.LENGTH_SHORT)
.show();
break;
}
佈局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
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="用戶名:" />
<EditText
android:id="@+id/mainAc_et_userName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true" />
</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="密 碼:" />
<EditText
android:id="@+id/mainAc_et_userPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:singleLine="true" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/mainAc_btn_login"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="登錄" />
</LinearLayout>
<WebView
android:id="@+id/mainAc_wv_show"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
}
}
}
運行結果: