<uses-permission android:name="android.permission.INTERNET"/>
<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" >
<EditText
android:id="@+id/et_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="請輸入用戶名" />
<EditText
android:id="@+id/et_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="請輸入密碼"
android:inputType="textPassword" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="click"
android:text="Button" />
</LinearLayout>
LoginServicepackage org.gentry.login.service;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import org.gentry.login.utils.StreamTools;
public class LoginService {
/**
*
* @param username
* @param password
* @return 返回null 登錄異常
*/
public static String loginByGet(String username, String password) {
// 提交數據到服務器
try {
String path = "http://192.168.1.100:8080/web/LoginServlet?username"
+ username + "&password" + password; // 拼裝路徑
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5000);
conn.setRequestMethod("GET");
int code = conn.getResponseCode();
if (code == 200) {
// 請求成功
InputStream is = conn.getInputStream();
String text = StreamTools.readInputStream(is);
return text;
} else {
return null;
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
}
StreamToolspackage org.gentry.login.utils;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
public class StreamTools {
/**
* 把輸入流的內容轉化成字符串
*
* @param is
* @return
*/
public static String readInputStream(InputStream is) {
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int len = 0;
byte[] buffer = new byte[1024];
while ((len = is.read(buffer)) != -1) {
baos.write(buffer, 0, len);
}
is.close();
byte[] result = baos.toByteArray();
// 試着解析result裏面的字符串
String temp = new String(result);
return temp;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return "獲取失敗";
}
}
}
MainActivity
package org.gentry.login;
import org.gentry.login.service.LoginService;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
private EditText et_username;
private EditText et_password;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_username = (EditText) findViewById(R.id.et_username);
et_password = (EditText) findViewById(R.id.et_password);
}
public void click(View view) {
final String username = et_username.getText().toString().trim();
final String password = et_password.getText().toString().trim();
new Thread() {
@Override
public void run() {
final String result = LoginService.loginByGet(username,
password);
if (result != null) {
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(MainActivity.this, result,
Toast.LENGTH_SHORT).show();
}
});
} else {
// 請求失敗
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(MainActivity.this, "請求失敗",
Toast.LENGTH_SHORT).show();
}
});
}
};
}.start();
}
}