zoukankan      html  css  js  c++  java
  • Android HttpClient post MultipartEntity

    转自[http://blog.csdn.net/hellohaifei/article/details/9707089]

    在Android 中使用HttpClient,MultipartEntity

    为了发送图片,文件等资源,现在采用开源的org.apache.http.entity.mime.MultipartEntity

    一.去官网http://hc.apache.org/downloads.cgi 下载

    可以只下载binary,如果可能需要修改源文件的话,可以直接下载source.

    二.导入jar包

    将下载下来的httpcomponents-client-4.2.5-bin.zip取其httpcomponents-client-4.2.5-bin.ziphttpcomponents-client-4.2.5libhttpmime-4.2.5.jar包

    将httpmime-4.2.5.jar包,放到android工程的lib目录下。

    三. 查看jar包,

    我这里用的是源文件,因为我需要修改些东西

    三.使用

    [java] view plaincopy
     
    1. class MyAsyncTask extends AsyncTask<String, Integer, String> {  
    2.   
    3.         String FORM_TABLE_NAME = "ask?action=Chatbottom-toSay-";// 自己需要配置的表单  
    4.         String filePath = "/mnt/sdcard/picture.jpg";// 测试写的文件路径,转换成自己的文件路径  
    5.         final String hostUrl = "http://www.myhost.com";// 写成自己要上传的地址  
    6.   
    7.         @Override  
    8.         protected String doInBackground(String... params) {  
    9.             HttpClient httpclient = null;  
    10.             httpclient = new DefaultHttpClient();  
    11.   
    12.             final HttpPost httppost = new HttpPost(hostUrl);  
    13.             final File imageFile = new File(filePath);  
    14.             final MultipartEntity multipartEntity = new MultipartEntity();  
    15.   
    16.             if (false) {  
    17.                 InputStream in = null;  
    18.                 try {  
    19.                     in = new FileInputStream(imageFile);  
    20.                 } catch (FileNotFoundException e) {  
    21.                     e.printStackTrace();  
    22.                 }  
    23.                 InputStreamBody inputStreamBody = new InputStreamBody(in,  
    24.                         "android_inputstream.jpg");  
    25.                 // FormBodyPart formBodyPart = new FormBodyPart(FORM_TABLE_NAME,  
    26.                 // contentBody);  
    27.                 multipartEntity.addPart(FORM_TABLE_NAME, inputStreamBody);  
    28.             }  
    29.             if (false) {  
    30.                 ContentBody contentBody = new FileBody(imageFile);  
    31.                 FormBodyPart formBodyPart = new FormBodyPart(FORM_TABLE_NAME,  
    32.                         contentBody);  
    33.                 multipartEntity.addPart(formBodyPart);  
    34.             }  
    35.             if (false) {  
    36.                 // FileBody fileBody = new FileBody(imageFile, "image/jpeg",  
    37.                 // "utf-8");  
    38.                 FileBody fileBody = new FileBody(imageFile);  
    39.                 multipartEntity.addPart(FORM_TABLE_NAME, fileBody);  
    40.             }  
    41.   
    42.             if (true) {  
    43.                 Bitmap photoBM = BitmapFactory.decodeFile(filePath);  
    44.                 if (photoBM == null) {  
    45.                     return null;  
    46.                 }  
    47.                 ByteArrayOutputStream photoBao = new ByteArrayOutputStream();  
    48.                 boolean successCompress = photoBM.compress(CompressFormat.JPEG,  
    49.                         80, photoBao);  
    50.                 if (!successCompress) {  
    51.                     return null;  
    52.                 }  
    53.                 ByteArrayBody byteArrayBody = new ByteArrayBody(  
    54.                         photoBao.toByteArray(), "android.jpg");  
    55.                 photoBM.recycle();  
    56.                 // InputStreamBody inbody = new InputStreamBody(new InputStream,  
    57.                 // filename);  
    58.                 multipartEntity.addPart(FORM_TABLE_NAME, byteArrayBody);  
    59.             }  
    60.   
    61.             httppost.setEntity(multipartEntity);  
    62.   
    63.             HttpResponse httpResponse;  
    64.             try {  
    65.                 httpResponse = httpclient.execute(httppost);  
    66.   
    67.                 final int statusCode = httpResponse.getStatusLine()  
    68.                         .getStatusCode();  
    69.   
    70.                 String response = EntityUtils.toString(  
    71.                         httpResponse.getEntity(), HTTP.UTF_8);  
    72.                 IWLog.d("got response: " + response);  
    73.   
    74.                 if (statusCode == HttpStatus.SC_OK) {  
    75.                     return "success";  
    76.                 }  
    77.   
    78.             } catch (ClientProtocolException e) {  
    79.                 e.printStackTrace();  
    80.             } catch (IOException e) {  
    81.                 e.printStackTrace();  
    82.             } finally {  
    83.                 if (httpclient != null) {  
    84.                     httpclient.getConnectionManager().shutdown();  
    85.                     httpclient = null;  
    86.                 }  
    87.             }  
    88.             return null;  
    89.   
    90.         }  
    91.   
    92.         @Override  
    93.         protected void onPostExecute(String result) {  
    94.             super.onPostExecute(result);  
    95.             if (result.equals("success")) {  
    96.   
    97.             }  
    98.         }  
    99.   
    100.     }  

    四.与HttpURLConnection比较

    网上好多人都用的是HttpURLConnection来上传图片,文件。由于我在解决实际问题时HttpURLConnection并不能达到预期,老是死在urlConnection.getInputStream()永远回不来。所以不得以改用的上面的库。最终感觉MultipartEntity用起来比较简单。

    附:

    在解决实际问题中,我也不是一帆风顺,也遇到了各种抽象的问题。推荐给大家个工具wireshark工具,用于抓取网络协议用的。很有帮助

    更多0
  • 相关阅读:
    【详记MySql问题大全集】四、设置MySql大小写敏感(踩坑血泪史)
    【详记MySql问题大全集】三、安装之后没有my.ini配置文件怎么办
    【详记MySql问题大全集】二、安装并破解Navicat
    【详记MySql问题大全集】一、安装MySql
    【从零开始搭建自己的.NET Core Api框架】(五)由浅入深详解CORS跨域机制并快速实现
    Redhat6.5编译安装MySQL5.6.38详解
    聚宽常用函数汇总
    小象机器学习(邹博老师)学习笔记
    Python画图色板
    kaggle注册、短信验证终极解决方案(亲测181205)
  • 原文地址:https://www.cnblogs.com/BenWong/p/3821709.html
Copyright © 2011-2022 走看看