Android_访问网络二(使用HttpClient访问)
2014-09-01 17:37
218 查看
PS:看了9年的小说,自己开始动手写了一本,请各位猿们动动手指,点击下,有起点账号的可以收藏下!!《武意长存》
上一篇介绍完了如何使用HttpURLConnection访问网络,接下来我们介绍如何使用HttpCilent进行网络的访问
StreamTools.java、服务端的代码以及乱码问题可参见上一篇文章
![](http://img.blog.csdn.net/20140901174036998?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMDY2Mzc1OA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
点击loginByClientGet按钮调用以下方法
点击loginByClientPost按钮调用以下方法
账号和密码正确时
上一篇介绍完了如何使用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(); }
账号和密码正确时
相关文章推荐
- Android使用DefaultHttpClient访问要登录的网络
- android使用httpClient访问网络
- android开发 使用HttpClient访问网络,post、get方式。注册、登陆、上传文件。
- Android中使用HttpURLConnection和HttpClient实现GET和POST请求访问网络
- Android使用HttpClient多线程访问网络错误解决方法之一
- 【Android开发】网络编程及Internet应用-使用HttpClient访问网络
- Android访问网络,使用HttpURLConnection还是HttpClient?
- 使用Http访问网络(使用Apache HttpClient)
- Android:使用HttpClient接口实现网络通信
- Android应用中使用AsyncHttpClient来异步网络数据
- android 请求网络 和 httpclient的使用上传下载
- [Android]正确地访问网络资源----Proxy的使用
- [Android实例] Android 使用HttpClient访问https
- Android访问网络,使用HttpURLConnection还是HttpClient?
- Android访问网络,使用HttpURLConnection还是HttpClient?
- android 请求网络 和 httpclient的使用上传下载
- Android学习指南之三十:使用URLConnection和HttpClient访问网络的方法
- Android 使用 HttpClient 进行网络通信,包括Get方式和Post方式