//登录页面以post方式提交数据
new Thread(){public void run(){
try {
String path="http://ip地址:8080/Login/servlet/LoginServlet";
//以httpClient方式进行post提交
DefaultHttpClient client=new DefaultHttpClient();
//准备post请求
HttpPost post=new HttpPost(path);
//准备parameters 最好背诵下来 可以倒着写
List<NameValuePair> lists=new ArrayList<NameValuePair>();
//准备NameValuePair 实际上就是我们要提交的用户名和密码 key是服务器key :username
BasicNameValuePair nameValuePair=new BasicNameValuePair("username", username);
BasicNameValuePair pwdValuePair=new BasicNameValuePair("password", password);
//把nameValuePair和pwdValuePair加入到集合中去
lists.add(nameValuePair);
lists.add(pwdValuePair);
//准备entity
UrlEncodedFormEntity entity=new UrlEncodedFormEntity(lists);
//准备post方式提交的正文,以实体形式准备(键值对形式)
post.setEntity(entity);
HttpResponse response=client.execute(post);
//获取服务器返回的状态码
int code=response.getStatusLine().getStatusCode();
if(code==200)
{
//获取服务器返回的数据 以流的形式返回
InputStream inputStream=response.getEntity().getContent();
//把流转换成字符串
String content=StreamTools.readStream(inputStream);
//展示结果
ShowToast(content);
}
} catch (Exception e) {
e.printStackTrace();
}
};}.start();