httpmime-4.1.3 简单使用

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 tag
	private 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的StringBody
	private Map<String, File> fileParas = null;//post的FileBody

	public HttpworkTask(String url, Map<String, Object> paras, Map<String, File> fileParas){
		this.url = url;
		this.paras = paras;
		this.fileParas = fileParas;
	}
	
	@Override
	public 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中去了。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值