簡易登陸介面的實現
今天實現了簡易的登陸介面,通過與伺服器的簡單互動實現了登入的功能。
介面佈局使用相對佈局,不做過多講解,實現介面如下
居中的實現時將使用者名稱的標籤新增 android:layout_centerVertical="true"屬性
、
1、首先搭建伺服器
建立servlet檔案,方法的實現都放在doGet()中。
將客戶端輸入的使用者名稱和密碼傳入伺服器。
當用戶名和密碼都匹配時顯示登陸成功,否則失敗。
通過login.jsp測試
LoginServlet實現原始碼如下:
request.setCharacterEncoding("UTF-8");用來解決部分客戶端到伺服器傳輸亂碼的問題request.setCharacterEncoding("UTF-8"); String username = request.getParameter("userName"); String password = request.getParameter("userpass"); System.out.println(username+"***"+password); if(name.equals(username)&&"123".equals(password)){ response.getOutputStream().write("成功".getBytes()); }else { response.getOutputStream().write("失敗".getBytes()); }
2、根據不同的傳輸方式將使用者名稱和密碼傳入伺服器
首先監聽登入按鈕
確定輸入的 使用者名稱和密碼不為空。
如果是通過get方法傳送資料,則需要將傳送的路徑定義為String spec = "http://172.16.237.145:8080/video/login.do?userName="
+ userName + "&userpass=" + userPass;
建立客戶端和伺服器的連線
判斷是否正確連線,並將結果通過裝入Message,通過 handler方法在客戶端上顯示是否成功登陸。
具體getOper()方法如下
如果通過post方法傳輸,思路相同public void getOper(String userName, String userPass) { try { String spec = "http://172.16.237.145:8080/video/login.do?userName=" + userName + "&userpass=" + userPass; URL url = new URL(spec); HttpURLConnection urlConnection = (HttpURLConnection) url .openConnection(); urlConnection.setRequestMethod("GET"); urlConnection.setReadTimeout(5000); urlConnection .setRequestProperty( "User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.154 Safari/537.36"); if (urlConnection.getResponseCode() == 200) { InputStream is = urlConnection.getInputStream(); String result = StreamTools.streamToStr(is); System.out.println("返回結果"+result); Message msg = new Message(); msg.what = CHANGETEXTVIEW; msg.obj = result; handler.sendMessage(msg); System.out.println(userName+"==="+userPass); } else { System.out.println("連線失敗"); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }
不同有以下幾點:
spec路徑為String spec = "http://172.16.237.145:8080/video/login.do";
在傳輸使用者名稱和密碼時使用data,與路徑分開String data = "userName=" + userName + "&userpass="
+ userPass;
具體post方法如下:
public void postOper(String userName, String userPass){
try {
String spec = "http://172.16.237.145:8080/video/login.do";
URL url = new URL(spec);
HttpURLConnection urlConnection = (HttpURLConnection) url
.openConnection();
urlConnection.setRequestMethod("POST");
String data = "userName=" + userName + "&userpass="
+ userPass;
//System.out.println(data);
urlConnection.setReadTimeout(5000);
urlConnection.setConnectTimeout(5000);
urlConnection.setRequestProperty("Connection",
"keep-alive");
urlConnection.setRequestProperty("Content-Length",
String.valueOf(data.getBytes().length));
;
urlConnection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
urlConnection
.setRequestProperty(
"User-Agent",
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.154 Safari/537.36");
urlConnection.setDoInput(true);
urlConnection.setDoOutput(true);
OutputStream os = urlConnection.getOutputStream();
os.write(data.getBytes());
os.flush();
if (urlConnection.getResponseCode() == 200) {
InputStream is = urlConnection.getInputStream();
String result = StreamTools.streamToStr(is);
System.out.println("返回結果" + result);
/*
* te_result.setText(result);
*/
Message msg = new Message();
msg.what = CHANGETEXTVIEW;
msg.obj = result;
handler.sendMessage(msg);
} else {
System.out.println("連線失敗");
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
返回結果方法Handle使用內部類建立
具體程式碼如下:
private Handler handler = new Handler() {
public void handleMessage(Message msg) {
int what = msg.what;
switch (what) {
case CHANGETEXTVIEW:
String result = (String) msg.obj;
te_result.setText(result);
System.out.println("---------------");
break;
default:
break;
}
};
};
監聽事件程式碼如下:
public void login(View v) throws UnsupportedEncodingException {
// TODO Auto-generated method stub
int id = v.getId();
switch (id) {
case R.id.logInBtn:
final String userName = URLEncoder.encode(et_username.getText().toString(),"UTF-8");
final String userPass = et_pass.getText().toString();
if (TextUtils.isEmpty(userName) || TextUtils.isEmpty(userPass)) {
Toast.makeText(this, "使用者名稱或密碼不能為空", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, "send", Toast.LENGTH_LONG).show();
new Thread() {
public void run() {
getOper(userName, userPass);
//postOper(userName, userPass);
};
}.start();
}
break;
default:
break;
}
}
final String userName = URLEncoder.encode(et_username.getText().toString(),"UTF-8");中encode方法的呼叫是為了解決get方法在傳輸時亂碼的問題。
總體來說專案不是很難,但是需要很仔細,我在碼的時候在post方法傳入時總是傳入空值,檢查後得知為String data = "userName=" + userName + "&userpass="
+ userPass;重uerName和userpass少了兩個“=”,這搞的我那個叫鬱悶。