前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >HttpClient详解

HttpClient详解

作者头像
xiangzhihong
发布2018-01-26 11:00:47
9380
发布2018-01-26 11:00:47
举报
文章被收录于专栏:向治洪向治洪

HttpClient执行Get和Post请求

  1. Get
代码语言:js
复制
/**
    * 采用httpclient的方式 用get提交数据到服务器
    */
   public void loginByClientGet(View view) {
       String password = et_password.getText().toString().trim();
       String name = et_username.getText().toString().trim();
       if (TextUtils.isEmpty(name) || TextUtils.isEmpty(password)) {
           Toast.makeText(this, "用户名密码不能为空", 1).show();
           return;
       }
       // 1.打开浏览器
       HttpClient client = new DefaultHttpClient();
       // 2.输入浏览器的地址
       String uri = "http://192.168.1.100:8080/web/LoginServlet?" + "name="
               + URLEncoder.encode(name) + "&password="
               + URLEncoder.encode(password);
       HttpGet httpGet = new HttpGet(uri);
       // 3.敲回车.
       try {
           HttpResponse response = client.execute(httpGet);
           int code = response.getStatusLine().getStatusCode();
           if (code == 200) {
               InputStream is = response.getEntity().getContent();
               String result = StreamTools.readFromStream(is);
               Toast.makeText(this, result, 1).show();
           } else {
               Toast.makeText(this, "服务器异常", 1).show();
           }
       } catch (Exception e) {
           e.printStackTrace();
           Toast.makeText(this, "访问网络异常", 1).show();
       }
   }

2. Post

代码语言:js
复制
/**
 * 采用httpclient post数据到服务器
 */
public void loginByClientPost(View view) {
    String password = et_password.getText().toString().trim();
    String name = et_username.getText().toString().trim();
    if (TextUtils.isEmpty(name) || TextUtils.isEmpty(password)) {
        Toast.makeText(this, "用户名密码不能为空", 1).show();
        return;
    }
    try {
        // 1.创建一个浏览器
        HttpClient client = new DefaultHttpClient();
        // 2.准备一个连接
        HttpPost post = new HttpPost(
                "http://192.168.1.100:8080/web/LoginServlet");
        // 要向服务器提交的数据实体
        List<NameValuePair> parameters = new ArrayList<NameValuePair>();
        parameters.add(new BasicNameValuePair("name", name));
        parameters.add(new BasicNameValuePair("password", password));
        UrlEncodedFormEntity entity = new UrlEncodedFormEntity(parameters,
                "utf-8");
        post.setEntity(entity);
        // 3.敲回车
        HttpResponse response = client.execute(post);
        int code = response.getStatusLine().getStatusCode();
        if (code == 200) {
            InputStream is = response.getEntity().getContent();
            String result = StreamTools.readFromStream(is);
            Toast.makeText(this, result, 1).show();
        } else {
            Toast.makeText(this, "服务器异常", 1).show();
        }
    } catch (Exception e) {
        e.printStackTrace();
        Toast.makeText(this, "client post 失败", 1).show();
    }
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016/02/19,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • HttpClient执行Get和Post请求
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档