Android 采用HttpClient提交数据到服务器

在前几篇文章中《Android 采用get方式提交数据到服务器》《Android 采用post方式提交数据到服务器》介绍了android的两种提交数据到服务器的方法

本文继续介绍采用HttpClient提交数据到服务器

HTTP 协议可能是现在 Internet 上使用得最多、最重要的协议了,越来越多的 Java 应用程序需要直接通过 HTTP 协议来访问网络资源。虽然在 JDK 的 java net包中已经提供了访问 HTTP 协议的基本功能,但是对于大部分应用程序来说,JDK 库本身提供的功能还不够丰富和灵活。HttpClient 是 Apache Jakarta Common 下的子项目,用来提供高效的、最新的、功能丰富的支持 HTTP 协议的客户端编程工具包,并且它支持 HTTP 协议最新的版本和建议。HttpClient 已经应用在很多的项目中,比如 Apache Jakarta 上很著名的另外两个开源项目 Cactus 和 HTMLUnit 都使用了 HttpClient。

修改代码如下:

    public void LoginHttpClientGet(View view) {
        String name = et_name.getText().toString().trim();
        String pwd = et_pwd.getText().toString().trim();

        if (TextUtils.isEmpty(name) || TextUtils.isEmpty(pwd)) {
            Toast.makeText(this, "用户名密码不能为空", 0).show();
        } else {
            // 1、打开一个浏览器
            HttpClient client = new DefaultHttpClient();
            // 2、输入地址
            String path = "http://169.254.168.71:8080/web/LoginServlet?username="
                    + name + "&password=" + pwd;
            try {
                HttpGet httpGet = new HttpGet(path);
                // 3、敲回车
                HttpResponse response = client.execute(httpGet);
                int code = response.getStatusLine().getStatusCode();

                if (code == 200) {
                    InputStream is = response.getEntity().getContent();
                    // 把is的内容转换为字符串
                    ByteArrayOutputStream bos = new ByteArrayOutputStream();
                    byte[] buffer = new byte[1024];
                    int len = -1;
                    while ((len = is.read(buffer)) != -1) {
                        bos.write(buffer, 0, len);
                    }
                    String result = new String(bos.toByteArray());
                    is.close();
                    Toast.makeText(this, result, 0).show();

                } else {
                    Toast.makeText(this, "请求失败,失败原因: " + code, 0).show();
                }

            } catch (Exception e) {
                e.printStackTrace();
                Toast.makeText(this, "请求失败,请检查logcat日志控制台", 0).show();
            }

        }

    }
    
    public void LoginHttpClientPost(View view) {
        String name = et_name.getText().toString().trim();
        String pwd = et_pwd.getText().toString().trim();

        if (TextUtils.isEmpty(name) || TextUtils.isEmpty(pwd)) {
            Toast.makeText(this, "用户名密码不能为空", 0).show();
        } else {
            // 1、打开一个浏览器
            HttpClient client = new DefaultHttpClient();
            // 2、输入地址
            String path = "http://169.254.168.71:8080/web/LoginServlet?username="
                    + name + "&password=" + pwd;
            try {
                HttpPost httpPost = new HttpPost(path);
                //指定要去提交的数据实体
                List<NameValuePair> parameters = new ArrayList<NameValuePair>();
                parameters.add(new BasicNameValuePair("username", name));
                parameters.add(new BasicNameValuePair("password", pwd));
                httpPost.setEntity(new UrlEncodedFormEntity(parameters, "utf-8"));
                //3、敲回车
                HttpResponse response = client.execute(httpPost);
                int code = response.getStatusLine().getStatusCode();

                if (code == 200) {
                    InputStream is = response.getEntity().getContent();
                    // 把is的内容转换为字符串
                    ByteArrayOutputStream bos = new ByteArrayOutputStream();
                    byte[] buffer = new byte[1024];
                    int len = -1;
                    while ((len = is.read(buffer)) != -1) {
                        bos.write(buffer, 0, len);
                    }
                    String result = new String(bos.toByteArray());
                    is.close();
                    Toast.makeText(this, result, 0).show();

                } else {
                    Toast.makeText(this, "请求失败,失败原因: " + code, 0).show();
                }

            } catch (Exception e) {
                e.printStackTrace();
                Toast.makeText(this, "请求失败,请检查logcat日志控制台", 0).show();
            }

        }

    }

获取更多C语言与算法的相关知识,关注公众号:“csuanfa”

posted @ 2016-06-29 15:38  wuyudong  阅读(635)  评论(0编辑  收藏  举报
Top_arrow