android中的网络通信(三) Apache HTTP

  android HTTP中的两种形式HttpURLConnection和ApacheHTTP,上一篇介绍的是HttpURLConnection,通过url形式传递参数,ApacheHTTP使得HTTP编程更加简单高效。在上一遍的基础上把客户端的登陆请求用Apache HTTP来写。

public void ApacheHttp(String name, String password) {
		String urlStr = "http://192.168.1.101:8080/MyServer/servlet/LoginServlet";
		// 用HttpPost来提交参数
		HttpPost post = new HttpPost(urlStr);
		// 参数比较多的话,对参数进行封装
		List<NameValuePair> paras = new ArrayList<NameValuePair>();
		// 添加用户名和密码
		paras.add(new BasicNameValuePair("name", name));
		paras.add(new BasicNameValuePair("password", password));
		try {
                       //设置请求参数
			post.setEntity(new UrlEncodedFormEntity(paras,HTTP.UTF_8));
			//执行请求相应
			HttpResponse response = new DefaultHttpClient().execute(post);
			if(response.getStatusLine().getStatusCode()==200){
				String msg=EntityUtils.toString(response.getEntity());
				AlertDialog.Builder build = new AlertDialog.Builder(this);
				build.setMessage(msg).setPositiveButton("确定",
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								// TODO Auto-generated method stub

							}
						});
				AlertDialog alert = build.create();
				alert.show();
				
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值