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);
// [1]找到我們關(guān)心的控件
et_username = (EditText) findViewById(R.id.et_username);
et_password = (EditText) findViewById(R.id.et_password);
}
// 點(diǎn)擊按鈕 進(jìn)行g(shù)et方式提交數(shù)據(jù)
public void click1(View v) {
new Thread(){public void run() {
try {
String name = et_username.getText().toString().trim();
String pwd = et_password.getText().toString().trim();
//[2.1]定義get方式要提交的路徑 小細(xì)節(jié) 如果提交中文要對(duì)name 和 pwd 進(jìn)行一個(gè)urlencode 編碼
String path = "http://192.168.11.73:8080/login/LoginServlet?username="+URLEncoder.encode(name, "utf-8")+"&password="+URLEncoder.encode(pwd, "utf-8")+"";
//[3]獲取httpclient實(shí)例
DefaultHttpClient client = new DefaultHttpClient();
//[3.1]準(zhǔn)備get請(qǐng)求 定義 一個(gè)httpget實(shí)現(xiàn)
HttpGet get = new HttpGet(path);
//[3.2]執(zhí)行一個(gè)get請(qǐng)求
HttpResponse response = client.execute(get);
//[4]獲取服務(wù)器返回的狀態(tài)碼
int code = response.getStatusLine().getStatusCode();
if (code == 200) {
//[5]獲取服務(wù)器返回的數(shù)據(jù) 以流的形式返回
InputStream inputStream = response.getEntity().getContent();
//[6]把流轉(zhuǎn)換成字符串
String content = StreamTools.readStream(inputStream);
//[7]展示結(jié)果
showToast(content);
}
} catch (Exception e) {
e.printStackTrace();
}
};}.start();
}
// [1]點(diǎn)擊按鈕 進(jìn)行post方式提交數(shù)據(jù)
public void click2(View v) {
new Thread(){public void run() {
try {
//[2]獲取用戶名和密碼
String name = et_username.getText().toString().trim();
String pwd = et_password.getText().toString().trim();
String path = "http://192.168.11.73:8080/login/LoginServlet";
//[3]以httpClient 方式進(jìn)行post 提交
DefaultHttpClient client = new DefaultHttpClient();
//[3.1]準(zhǔn)備post 請(qǐng)求
HttpPost post = new HttpPost(path);
//[3.1.0]準(zhǔn)備parameters
List<NameValuePair> lists = new ArrayList<NameValuePair>();
//[3.1.1]準(zhǔn)備 NameValuePair 實(shí)際上就是我們要提交的用戶名 和密碼 key是服務(wù)器key :username
BasicNameValuePair nameValuePair = new BasicNameValuePair("username",name);
BasicNameValuePair pwdValuePair = new BasicNameValuePair("password",pwd);
//[3.1.3] 把nameValuePair 和 pwdValuePair 加入到集合中
lists.add(nameValuePair);
lists.add(pwdValuePair);
//[3.1.3]準(zhǔn)備entity
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(lists);
//[3.2]準(zhǔn)備post方式提交的正文 以實(shí)體形式準(zhǔn)備 (鍵值對(duì)形式 )
post.setEntity(entity);
HttpResponse response = client.execute(post);
//[4]獲取服務(wù)器返回的狀態(tài)碼
int code = response.getStatusLine().getStatusCode();
if (code == 200) {
//[5]獲取服務(wù)器返回的數(shù)據(jù) 以流的形式返回
InputStream inputStream = response.getEntity().getContent();
//[6]把流轉(zhuǎn)換成字符串
String content = StreamTools.readStream(inputStream);
//[7]展示結(jié)果
showToast(content);
}
} catch (Exception e) {
e.printStackTrace();
}
};}.start();
}
//封裝toast方法 該toast方法執(zhí)行在主線程
public void showToast(final String content){
runOnUiThread(new Runnable() {
@Override
public void run() {
//該方法一定是執(zhí)行主線程
Toast.makeText(getApplicationContext(), content, 1).show();
}
});
}
}
把一個(gè)inputstream轉(zhuǎn)化成一個(gè)string
public class StreamTools {
//把一個(gè)inputStream 轉(zhuǎn)換成一個(gè)String
public static String readStream(InputStream in) throws Exception{
//定義一個(gè)內(nèi)存輸出流
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int len = -1;
byte[] buffer = new byte[1024]; //1kb
while((len=in.read(buffer))!=-1){
baos.write(buffer, 0, len);
}
in.close();
String content = new String(baos.toByteArray(),"gbk");
return content;
}
}
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者