Android 发送HTTP GET POST 请求以及通过 MultipartEntityBuilder 上传文件

简介:

折腾了好几天的 HTTP 终于搞定了,经测试正常,不过是初步用例测试用的,因为后面还要修改先把当前版本保存在博客里吧。

其中POST因为涉及多段上传需要导入两个包文件,我用的是最新的 httpmine4.3 发现网上很多 MultipartEntity 相关的文章都是早起版本的,以前的一些方法虽然还可用,但新版本中已经不建议使用了,所以全部使用新的方式 MultipartEntityBuilder 来处理了。

httpmime-4.3.2.jar  
httpcore-4.3.1.jar 


下载地址:http://hc.apache.org/downloads.cgi

有些镜像貌似打不开,页面上可以可以选择国内的 .cn 后缀的域名镜像服务器来下载


如果是 android studio 这里可能会遇到一个问题:Android Duplicate files copied in APK


经测试 POST 对中文处理也是正常的,没有发现乱码

下面是完整代码:

ZHttpRequest.java

package com.ai9475.util;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HTTP;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * Created by ZHOUZ on 14-2-3.
 */
public class ZHttpRequest
{
    protected String url = "";

    protected Map<String, String> headers = null;

    protected int connectionTimeout = 5000;

    protected int soTimeout = 10000;

    protected int statusCode = 200;

    protected String charset = HTTP.UTF_8;

    protected HttpGet httpGet;

    protected HttpPost httpPost;

    protected HttpParams httpParameters;

    protected HttpResponse httpResponse;

    protected HttpClient httpClient;

    protected String inputContent;

    /**
     * 设置当前请求的链接
     *
     * @param url
     * @return
     */
    public ZHttpRequest setUrl(String url)
    {
        this.url = url;
        return this;
    }

    /**
     * 设置请求的 header 信息
     *
     * @param headers
     * @return
     */
    public ZHttpRequest setHeaders(Map headers)
    {
        this.headers = headers;
        return this;
    }

    /**
     * 设置连接超时时间
     *
     * @param timeout 单位(毫秒),默认 5000
     * @return
     */
    public ZHttpRequest setConnectionTimeout(int timeout)
    {
        this.connectionTimeout = timeout;
        return this;
    }

    /**
     * 设置 socket 读取超时时间
     *
     * @param timeout 单位(毫秒),默认 10000
     * @return
     */
    public ZHttpRequest setSoTimeout(int timeout)
    {
        this.soTimeout = timeout;
        return this;
    }

    /**
     * 设置获取内容的编码格式
     *
     * @param charset 默认为 UTF-8
     * @return
     */
    public ZHttpRequest setCharset(String charset)
    {
        this.charset = charset;
        return this;
    }

    /**
     * 获取 HTTP 请求响应信息
     *
     * @return
     */
    public HttpResponse getHttpResponse()
    {
        return this.httpResponse;
    }

    /**
     * 获取 HTTP 客户端连接管理器
     *
     * @return
     */
    public HttpClient getHttpClient()
    {
        return this.httpClient;
    }

    /**
     * 获取请求的状态码
     *
     * @return
     */
    public int getStatusCode()
    {
        return this.statusCode;
    }

    /**
     * 通过 GET 方式请求数据
     *
     * @param url
     * @return
     * @throws IOException
     */
    public String get(String url) throws IOException
    {
        // 设置当前请求的链接
        this.setUrl(url);
        // 实例化 GET 连接
        this.httpGet = new HttpGet(this.url);
        // 自定义配置 header 信息
        this.addHeaders(this.httpGet);
        // 初始化客户端请求
        this.initHttpClient();
        // 发送 HTTP 请求
        this.httpResponse = this.httpClient.execute(this.httpGet);
        // 读取远程数据
        this.getInputStream();
        // 远程请求状态码是否正常
        if (this.statusCode != HttpStatus.SC_OK) {
            return null;
        }
        // 返回全部读取到的字符串
        return this.inputContent;
    }

    public String post(String url, Map<String, String> datas, Map<String, String> files) throws IOException
    {
        this.setUrl(url);
        // 实例化 GET 连接
        this.httpPost = new HttpPost(this.url);
        // 自定义配置 header 信息
        this.addHeaders(this.httpPost);
        // 初始化客户端请求
        this.initHttpClient();
        Iterator iterator = datas.entrySet().iterator();
        MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create();
        multipartEntityBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
        multipartEntityBuilder.setCharset(Charset.forName(this.charset));
        // 发送的数据
        while (iterator.hasNext()) {
            Map.Entry<String, String> entry = (Map.Entry<String, String>) iterator.next();
            multipartEntityBuilder.addTextBody(entry.getKey(), entry.getValue(), ContentType.create("text/plain", Charset.forName(this.charset)));
        }
        // 发送的文件
        if (files != null) {
            iterator = files.entrySet().iterator();
            while (iterator.hasNext()) {
                Map.Entry<String, String> entry = (Map.Entry<String, String>) iterator.next();
                String path = entry.getValue();
                if ("".equals(path) || path == null) continue;
                File file = new File(entry.getValue());
                multipartEntityBuilder.addBinaryBody(entry.getKey(), file);
            }
        }
        // 生成 HTTP 实体
        HttpEntity httpEntity = multipartEntityBuilder.build();
        // 设置 POST 请求的实体部分
        this.httpPost.setEntity(httpEntity);
        // 发送 HTTP 请求
        this.httpResponse = this.httpClient.execute(this.httpPost);
        // 读取远程数据
        this.getInputStream();
        // 远程请求状态码是否正常
        if (this.statusCode != HttpStatus.SC_OK) {
            return null;
        }
        // 返回全部读取到的字符串
        return this.inputContent.toString();
    }

    /**
     * 为 HTTP 请求添加 header 信息
     *
     * @param request
     */
    protected void addHeaders(HttpRequestBase request)
    {
        if (this.headers != null) {
            Set keys = this.headers.entrySet();
            Iterator iterator = keys.iterator();
            Map.Entry<String, String> entry;
            while (iterator.hasNext()) {
                entry = (Map.Entry<String, String>) iterator.next();
                request.addHeader(entry.getKey().toString(), entry.getValue().toString());
            }
        }
    }

    /**
     * 配置请求参数
     */
    protected void setParams()
    {
        this.httpParameters = new BasicHttpParams();
        this.httpParameters.setParameter("charset", this.charset);
        // 设置 连接请求超时时间
        HttpConnectionParams.setConnectionTimeout(this.httpParameters, this.connectionTimeout);
        // 设置 socket 读取超时时间
        HttpConnectionParams.setSoTimeout(this.httpParameters, this.soTimeout);
    }

    /**
     * 初始化配置客户端请求
     */
    protected void initHttpClient()
    {
        // 配置 HTTP 请求参数
        this.setParams();
        // 开启一个客户端 HTTP 请求
        this.httpClient = new DefaultHttpClient(this.httpParameters);
    }

    /**
     * 读取远程数据
     *
     * @throws IOException
     */
    protected void getInputStream() throws IOException
    {
        // 接收远程输入流
        InputStream inStream = this.httpResponse.getEntity().getContent();
        // 分段读取输入流数据
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] buf = new byte[1024];
        int len = -1;
        while ((len = inStream.read(buf)) != -1) {
            baos.write(buf, 0, len);
        }
        // 将数据转换为字符串保存
        this.inputContent = new String(baos.toByteArray());
        // 数据接收完毕退出
        inStream.close();
        // 获取请求返回的状态码
        this.statusCode = this.httpResponse.getStatusLine().getStatusCode();
    }

    /**
     * 关闭连接管理器释放资源
     */
    protected void shutdownHttpClient()
    {
        if (this.httpClient != null && this.httpClient.getConnectionManager() != null) {
            this.httpClient.getConnectionManager().shutdown();
        }
    }
}

MainActivity.java

这个我就只写事件部分了

    public void doClick(View view)
    {
        ZHttpRequest request = new ZHttpRequest();
        String url = "";
        TextView textView = (TextView) findViewById(R.id.showContent);
        String content = "空内容";
        try {
            if (view.getId() == R.id.doGet) {
                url = "http://www.baidu.com";
                content = "GET数据:" + request.get(url);
            } else {
                url = "http://192.168.1.6/test.php";
                HashMap<String, String> datas = new HashMap<String, String>();
                datas.put("p1", "abc");
                datas.put("p2", "中文");
                datas.put("p3", "abc中文cba");
                datas.put("pic", this.picPath);
                HashMap<String, String> files = new HashMap<String, String>();
                files.put("file", this.picPath);
                content = "POST数据:" + request.post(url, datas, files);
            }

        } catch (IOException e) {
            content = "IO异常:" + e.getMessage();
        } catch (Exception e) {
            content = "异常:" + e.getMessage();
        }
        textView.setText(content);
    }

其中的 this.picPath 就是指定的SD卡中的相片路径 String 类型


activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    >
    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical"
            >
            <Button
                android:id="@+id/doGet"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:padding="10dp"
                android:layout_marginBottom="10dp"
                android:text="GET请求"
                android:onClick="doClick"
                />
            <Button
                android:id="@+id/doPost"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:padding="10dp"
                android:layout_marginBottom="10dp"
                android:text="POST请求"
                android:onClick="doClick"
                />
            <Button
                android:id="@+id/doPhoto"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:padding="10dp"
                android:layout_marginBottom="10dp"
                android:text="拍照"
                android:onClick="doPhoto"
                />
            <ImageView
                android:id="@+id/showPhoto"
                android:layout_width="fill_parent"
                android:layout_height="250dp"
                android:scaleType="centerCrop"
                android:src="@drawable/add"
                android:layout_marginBottom="10dp"
                />
            <TextView
                android:id="@+id/showContent"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="10dp"
                />
        </LinearLayout>
    </ScrollView>
</LinearLayout>

由于我现在对Java 还不是很熟悉,只看过一些视频教程,实际开发经验不多,我按自己的理解写的注释,不过应该不会有什么太大问题吧,如有错误请指出,谢谢!




目录
相关文章
|
2月前
|
JavaScript
Node.js【GET/POST请求、http模块、路由、创建客户端、作为中间层、文件系统模块】(二)-全面详解(学习总结---从入门到深化)
Node.js【GET/POST请求、http模块、路由、创建客户端、作为中间层、文件系统模块】(二)-全面详解(学习总结---从入门到深化)
27 0
|
3天前
|
安全 网络安全 开发工具
对象存储oss使用问题之flutter使用http库进行post请求文件上传返回400如何解决
《对象存储OSS操作报错合集》精选了用户在使用阿里云对象存储服务(OSS)过程中出现的各种常见及疑难报错情况,包括但不限于权限问题、上传下载异常、Bucket配置错误、网络连接问题、跨域资源共享(CORS)设定错误、数据一致性问题以及API调用失败等场景。为用户降低故障排查时间,确保OSS服务的稳定运行与高效利用。
16 1
|
3天前
|
网络协议 安全 API
Android网络和数据交互: 什么是HTTP和HTTPS?在Android中如何进行网络请求?
HTTP和HTTPS是网络数据传输协议,HTTP基于TCP/IP,简单快速,HTTPS则是加密的HTTP,确保数据安全。在Android中,过去常用HttpURLConnection和HttpClient,但HttpClient自Android 6.0起被移除。现在推荐使用支持TLS、流式上传下载、超时配置等特性的HttpsURLConnection进行网络请求。
5 0
|
5天前
|
安全 Android开发 数据安全/隐私保护
Android中的动态权限请求与最佳实践
【4月更文挑战第14天】 在现代安卓应用开发中,用户隐私和安全被赋予了前所未有的重要性。随着Android 6.0(API级别23)引入的运行时权限模型,开发者必须更加细致地处理权限请求,以确保应用功能的完整性同时不侵犯用户的隐私。本文将深入探讨如何在Android应用中实现动态权限请求,分析常见问题,并提供一系列最佳实践,以帮助开发者优雅地处理这一挑战。
16 5
|
16天前
|
Go
go语言的http post推送
go语言的http post推送
10 1
|
29天前
|
数据采集 缓存 前端开发
http和https请求服务器的时候在请求头部分都带什么到服务器呢?
HTTP和HTTPS请求头基本结构相似,HTTPS多了一层SSL/TLS加密。常见请求头如Accept(指定内容类型)、Authorization(身份验证)、Cookie(会话跟踪)、User-Agent(标识用户代理)等。HTTPS特有的头包括Upgrade-Insecure-Requests(升级到HTTPS)、Strict-Transport-Security(强制使用HTTPS)、Sec-Fetch-*(安全策略)和X-Content-Type-Options、X-Frame-Options等(增强安全性)。实际应用中,请求头会根据需求和安全策略变化。
20 0
|
1月前
|
缓存
HTTP协议中,GET和POST有什么区别?分别适用什么场景?
HTTP协议中,GET和POST有什么区别?分别适用什么场景?
36 0
|
2月前
|
XML 自然语言处理 前端开发
NLP自学习平台提供了API接口调用服务,这些接口可以通过HTTP GET请求进行调用
【2月更文挑战第7天】NLP自学习平台提供了API接口调用服务,这些接口可以通过HTTP GET请求进行调用
17 2
|
2月前
|
数据采集 JSON Java
HttpClient:HTTP GET请求的服务器响应输出
HttpClient:HTTP GET请求的服务器响应输出
|
2月前
|
Kubernetes 容器
使用kubeadm部署k8s报错:The kubelet is not running或者level=error msg="Handler for POST /v1.43/images/create returned error: Head \"https://us-west2-dock
使用kubeadm部署k8s报错:The kubelet is not running或者level=error msg="Handler for POST /v1.43/images/create returned error: Head \"https://us-west2-dock