httpmime-4.1.3 简单使用

来源:互联网 发布:北方网络视频 编辑:程序博客网 时间:2024/06/06 07:37

HttpClient 是 Apache Jakarta Common 下的子项目,可以用来提供高效的、最新的、功能丰富的支持 HTTP 协议的客户端编程工具包,并且它支持 HTTP 协议最新的版本和建议(CV大法来自于HttpClient百度百科,-  -),httpclient的jar包可去apache官网或者csdn或者iask下载(个人感觉iask还是不错的,至少好多资源免积分,而且里面还有很多意想不到的资源哦, -  -)。

言归正传,介绍项目中使用的httpmime-4.1.3.jar包使用,理解jar包里面的类定义及属性方法可参照其javadoc,下面直接上代码。

public class HttpworkTask extends Thread {public final static String TAG = "HttpworkTask"; //log tagprivate NetworkListener listener;private static HttpClient httpClient;private final static int CONNECTIONTIMEOUT = 10000;//http链接超时 private final static int REQUESTTIMEOUT = 20000;//http数据请求超时private String url = null;private Map<String, Object> paras = null;//post的StringBodyprivate Map<String, File> fileParas = null;//post的FileBodypublic HttpworkTask(String url, Map<String, Object> paras, Map<String, File> fileParas){this.url = url;this.paras = paras;this.fileParas = fileParas;}@Overridepublic void run() {BufferedReader br = null;StringBuilder sBuilder = new StringBuilder();HttpParams httpParams = new BasicHttpParams();HttpConnectionParams.setConnectionTimeout(httpParams, CONNECTIONTIMEOUT);HttpConnectionParams.setSoTimeout(httpParams, REQUESTTIMEOUT);SchemeRegistry registry = new SchemeRegistry();  registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));  registry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
//由于是使用线程操作http,所以设置Thread safe属性,不然当start多个httpworktask线程时必然报错,这点需要注意  httpClient = new DefaultHttpClient(new ThreadSafeClientConnManager(httpParams, registry), httpParams); HttpPost post = new HttpPost(url);//添加 post的String 和File数据MultipartEntity entity = new MultipartEntity();try {// 添加参数if (paras != null && !paras.isEmpty()) {for (Map.Entry<String, Object> item : paras.entrySet()) {entity.addPart(item.getKey(), new StringBody(item.getValue().toString(), Charset.forName("UTF-8")));}}// 添加文件if (fileParas != null && !fileParas.isEmpty()) {for (Map.Entry<String, File> item : fileParas.entrySet()) {if (item.getValue().exists()) {Log.i(TAG, "upload File is exists and filepath is-->" + item.getKey() + "  " + item.getValue().getPath());entity.addPart(item.getKey(), new FileBody(item.getValue()));}else{Log.e(TAG, "upload File is NOT exists!");}}}post.setEntity(entity);HttpResponse response = httpClient.execute(post);int statecode = response.getStatusLine().getStatusCode();Log.i(TAG, "http response code-->" + statecode);if (statecode == HttpStatus.SC_OK) {HttpEntity responseEntity = response.getEntity();if (responseEntity != null) {InputStream is = responseEntity.getContent();br = new BufferedReader(new InputStreamReader(is));String tempStr;while ((tempStr = br.readLine()) != null) {sBuilder.append(tempStr);}br.close();}}} catch (Exception e) {listener.onConnectionError(NetworkListener.NET_ERROR, "http connect is error,pls check your phone network");} finally {if (br != null) {try {br.close();} catch (IOException e) {e.printStackTrace();}}}post.abort();//http返回的数据String resData = sBuilder.toString();Log.i(TAG, "http server response -->" + resData);if (resData != null) {listener.onConnectionRecieveData(resData.getBytes(), resData.length());}}public static void shutdownHttp() {if (httpClient != null) {httpClient.getConnectionManager().shutdown();}}}
说明几点:1.使用MultipartEntity,Stringbody和FileBody可同时post。
         2.可同时上传n个File文件。
            一般在定义http业务的json协议时,如json上传照片文件,imgUrls:图片文件(同时上传多张图片),entity.addPart的第一个参数为fileParas map的key值应为imgUrls,这样多次addpart后会不会顶掉前面add的filebody呢(key值相同啊)?其实不会!具体可看下addpart函数的实现源码,它是用addpart的两个参数生成一个对象放到一个list中去了。
原创粉丝点击