Android_访问网络二(使用HttpClient访问)

PS:看了9年的小说,自己开始动手写了一本,请各位猿们动动手指,点击下,有起点账号的可以收藏下!!《武意长存》


上一篇介绍完了如何使用HttpURLConnection访问网络,接下来我们介绍如何使用HttpCilent进行网络的访问


public class LoginService {
	private static final String BASE_PATH = "http://110.89.138.70:8080/server/LoginServlet";

	public static String loginByClientGet(String username, String password){
		//定义一个客户端 
		HttpClient client = new DefaultHttpClient();
		
		String path = null;
		try {
			path = BASE_PATH + "?username="+URLEncoder.encode(username, "utf-8")+"&password="+URLEncoder.encode(password, "utf-8");
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		}

		// 定义一个get请求方法
		HttpGet httpGet = new HttpGet(path);
		
		HttpResponse httpResponse = null;
		try {
			httpResponse = client.execute(httpGet);
// 获得响应码
			int code = httpResponse.getStatusLine().getStatusCode();
			if(code == 200){
				InputStream is = httpResponse.getEntity().getContent();
				return StreamTools.readInputStream(is);
			}else {
				return null;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return null;
	}


public static String loginByClientPost(String username, String password){
		//定义一个客户端
		HttpClient client = new DefaultHttpClient();
		// 定义post方法
		HttpPost httpPost = new HttpPost(BASE_PATH);
		
		//指定要提交的数据实体
		List<NameValuePair> parameters = new ArrayList<NameValuePair>();
		parameters.add(new BasicNameValuePair("username", username));
		parameters.add(new BasicNameValuePair("password", password));
		
		
		HttpResponse httpResponse = null;
		try {
//以utf-8对参数进行编码
			httpPost.setEntity(new UrlEncodedFormEntity(parameters, "UTF-8"));
			
			httpResponse = client.execute(httpPost);
			int code = httpResponse.getStatusLine().getStatusCode();
			if(code == 200){
				InputStream is = httpResponse.getEntity().getContent();
				return StreamTools.readInputStream(is);
			}else {
				return null;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return null;
	}
}

StreamTools.java、 服务端的代码以及乱码问题可参见上一篇文章


点击loginByClientGet按钮调用以下方法

public void loginByClientGet(View view){
		final String username = etUsername.getText().toString().trim();
		final String password = etPassword.getText().toString().trim();
		
		if(TextUtils.isEmpty(username) || TextUtils.isEmpty(password)){
			Toast.makeText(this, "用户名或密码不能为空!", Toast.LENGTH_SHORT).show();
		}else {
			new Thread(){
				public void run() {
					final String result = LoginService.loginByClientGet(username, password);
					if(null != result){	//请求成功
						runOnUiThread(new Runnable() {
							@Override
							public void run() {
								Toast.makeText(MainActivity.this, result, Toast.LENGTH_SHORT).show();
							}
						});
					}else {
						runOnUiThread(new Runnable() {
							@Override
							public void run() {
								Toast.makeText(MainActivity.this, "请求失败", Toast.LENGTH_SHORT).show();
							}
						});
					}
				};
			}.start();
		}
	}

点击loginByClientPost按钮调用以下方法

public void loginByClientPost(View view){
		final String username = etUsername.getText().toString().trim();
		final String password = etPassword.getText().toString().trim();
		
		if(TextUtils.isEmpty(username) || TextUtils.isEmpty(password)){
			Toast.makeText(this, "用户名或密码不能为空!", Toast.LENGTH_SHORT).show();
		}else {
			new Thread(){
				public void run() {
					final String result = LoginService.loginByClientPost(username, password);
					if(null != result){	//请求成功
						runOnUiThread(new Runnable() {
							@Override
							public void run() {
								Toast.makeText(MainActivity.this, result, Toast.LENGTH_SHORT).show();
							}
						});
					}else {
						runOnUiThread(new Runnable() {
							@Override
							public void run() {
								Toast.makeText(MainActivity.this, "请求失败", Toast.LENGTH_SHORT).show();
							}
						});
					}
				};
			}.start();
		}

账号和密码正确时



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值