zoukankan      html  css  js  c++  java
  • android HttpClient 附带的参数

    Sending images can be done using the HttpComponents libraries. Download the latest HttpClient (currently4.0.1) binary with dependencies package and copy apache-mime4j-0.6.jar and httpmime-4.0.1.jar to your project and add them to your Java build path.

    You will need to add the following imports to your class.

    import org.apache.http.entity.mime.HttpMultipartMode;
    import org.apache.http.entity.mime.MultipartEntity;
    import org.apache.http.entity.mime.content.FileBody;
    import org.apache.http.entity.mime.content.StringBody;
    
    

    Now you can create a MultipartEntity to attach an image to your POST request. The following code shows an example of how to do this:

    public void post(String url, List nameValuePairs) {
        HttpClient httpClient = new DefaultHttpClient();
        HttpContext localContext = new BasicHttpContext();
        HttpPost httpPost = new HttpPost(url);

        try {
            MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);

            for(int index=0; index < nameValuePairs.size(); index++) {
                if(nameValuePairs.get(index).getName().equalsIgnoreCase("image")) {
                    // If the key equals to "image", we use FileBody to transfer the data
                    entity.addPart(nameValuePairs.get(index).getName(), new FileBody(new File (nameValuePairs.get(index).getValue())));
                } else {
                    // Normal string data
                    entity.addPart(nameValuePairs.get(index).getName(), new StringBody(nameValuePairs.get(index).getValue()));
                }
            }

            httpPost.setEntity(entity);

            HttpResponse response = httpClient.execute(httpPost, localContext);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    这个方法目测写得不错,先留着用

    史上最难学的计算机技术,没有之一
  • 相关阅读:
    [PHP] websocket协议的生成
    [Linux] 解决virtualbox共享文件夹没有访问权限的问题
    [Linux] VirtualBox的ubuntu系统与宿主机共享目录
    [PHP] 框架中.env文件的加载过程
    [PHP] PHP7已经删除了preg_replace的e修饰符
    [日常]解决Connection to `ssl://pecl.php.net:443' failed
    [日常]k8s的前世今生
    [Linux] 利用tcpdump和strace进行debug
    [Go] 使用读写锁对map资源进行安全处理
    [Linux] 使用awk比较两个文件的内容
  • 原文地址:https://www.cnblogs.com/sanra/p/3850747.html
Copyright © 2011-2022 走看看