Back to project page Common-Library.
The source code is released under:
Apache License
If you think the Android project Common-Library listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.morgan.library.utils; //w w w . j a v a 2 s. c o m import java.io.File; import java.io.FileNotFoundException; import java.util.Map; import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.httpclient.cookie.CookiePolicy; import org.apache.commons.httpclient.methods.GetMethod; import org.apache.commons.httpclient.methods.PostMethod; import org.apache.commons.httpclient.methods.StringRequestEntity; import org.apache.commons.httpclient.methods.multipart.FilePart; import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity; import org.apache.commons.httpclient.methods.multipart.Part; import org.apache.commons.httpclient.methods.multipart.StringPart; import org.apache.commons.httpclient.params.HttpMethodParams; import android.util.Log; /** * ??????????????????????????(commons-httpclient-3.1.jar) * * @author Morgan.Ji * */ public class HttpClientUtils { private static final String TAG = HttpClientUtils.class.getName(); private static final int CONNECT_ERROR_SLEEP_TIME = 1000; private static final int TIMEOUT_CONNECTION = 1000; private static final int TIMEOUT_SOCKET = 1000; private static final String HOST_URL = ""; private static final String UTF_8 = "UTF-8"; public final static String SERVER_CONNECT_ERROR = "{code:0,message:\"????????\"}"; private final static int RETRY_TIME = 3; private static String appUserAgent; private static HttpClient getHttpClient() { HttpClient httpClient = new HttpClient(); // ?? HttpClient ?? Cookie,??????????? httpClient.getParams().setCookiePolicy( CookiePolicy.BROWSER_COMPATIBILITY); // ?? ?????????????? httpClient.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler()); // ?? ?????? httpClient.getHttpConnectionManager().getParams() .setConnectionTimeout(TIMEOUT_CONNECTION); // ?? ????????? httpClient.getHttpConnectionManager().getParams() .setSoTimeout(TIMEOUT_SOCKET); // ?? ??? httpClient.getParams().setContentCharset(UTF_8); return httpClient; } private static GetMethod getHttpGet(String url, String userAgent) { GetMethod httpGet = new GetMethod(url); httpGet.getParams().setSoTimeout(TIMEOUT_SOCKET); httpGet.setRequestHeader("Host", HOST_URL); httpGet.setRequestHeader("Connection", "Keep-Alive"); httpGet.setRequestHeader("User-Agent", userAgent); return httpGet; } private static PostMethod getHttpPost(String url, String userAgent) { PostMethod httpPost = new PostMethod(url); httpPost.getParams().setSoTimeout(TIMEOUT_SOCKET); httpPost.setRequestHeader("Host", HOST_URL); httpPost.setRequestHeader("Connection", "Keep-Alive"); httpPost.setRequestHeader("User-Agent", userAgent); return httpPost; } private static String getUserAgent() { if (appUserAgent == null || appUserAgent == "") { StringBuilder ua = new StringBuilder("Morgan"); ua.append('/' + AppUtils.getPackageInfo().versionName + '_' + AppUtils.getPackageInfo().versionCode);// App?? ua.append("/Android");// ???????? ua.append("/" + android.os.Build.VERSION.RELEASE);// ?????? ua.append("/" + android.os.Build.MODEL); // ?????? appUserAgent = ua.toString(); } return appUserAgent; } /** * get??URL * * @param url * @throws AppException */ public static String get(String url) { Logger.d(TAG, "get request: " + url); String userAgent = getUserAgent(); HttpClient httpClient = null; GetMethod httpGet = null; String responseBody = ""; int time = 0; do { try { httpClient = getHttpClient(); httpGet = getHttpGet(url, userAgent); int statusCode = httpClient.executeMethod(httpGet); if (statusCode != HttpStatus.SC_OK) { responseBody = SERVER_CONNECT_ERROR; break; } else { responseBody = httpGet.getResponseBodyAsString(); break; } } catch (Exception e) { time++; if (time < RETRY_TIME) { try { Thread.sleep(CONNECT_ERROR_SLEEP_TIME); } catch (InterruptedException e1) { } continue; } responseBody = SERVER_CONNECT_ERROR; } finally { // ???? if (null != httpGet) { httpGet.releaseConnection(); } httpClient = null; } } while (time < RETRY_TIME); Logger.d(TAG, "get response: " + url + "/r/n" + responseBody); return responseBody; } public static String post(String url, Map<String, Object> params) { return post(url, params, null); } /** * ??post?? * * @param url * @param params * @param files * @throws AppException */ public static String post(String url, Map<String, Object> params, Map<String, File> files) { Logger.d(TAG, "post request: " + url + " params: " + params.toString()); String userAgent = getUserAgent(); HttpClient httpClient = null; PostMethod httpPost = null; // post???????????? int length = (params == null ? 0 : params.size()) + (files == null ? 0 : files.size()); Part[] parts = new Part[length]; int i = 0; if (params != null) { for (String name : params.keySet()) { parts[i++] = new StringPart(name, String.valueOf(params .get(name)), UTF_8); } } if (files != null) { for (String file : files.keySet()) { try { parts[i++] = new FilePart(file, files.get(file)); } catch (FileNotFoundException e) { e.printStackTrace(); } } } String responseBody = ""; int time = 0; do { try { httpClient = getHttpClient(); httpPost = getHttpPost(url, userAgent); httpPost.setRequestEntity(new MultipartRequestEntity(parts, httpPost.getParams())); int statusCode = httpClient.executeMethod(httpPost); if (statusCode != HttpStatus.SC_OK) { responseBody = httpPost.getResponseBodyAsString(); break; } else if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY || statusCode == HttpStatus.SC_MOVED_TEMPORARILY) { Log.e(TAG, httpPost.getResponseHeader("Location") .getValue()); responseBody = post(httpPost.getResponseHeader("Location") .getValue(), params, files); break; } else { responseBody = SERVER_CONNECT_ERROR; break; } } catch (Exception e) { time++; if (time < RETRY_TIME) { try { Thread.sleep(CONNECT_ERROR_SLEEP_TIME); } catch (InterruptedException e1) { } continue; } responseBody = SERVER_CONNECT_ERROR; } finally { // ???? if (null != httpPost) { httpPost.releaseConnection(); } httpClient = null; } } while (time < RETRY_TIME); Logger.d(TAG, "post response: " + url + "/r/n" + responseBody); return responseBody; } /** * ??post?? * * @param url * @param params * @param files * @throws AppException */ public static String postJson(String url, String json) { Logger.d(TAG, "post request: " + url + " params: " + json); String userAgent = getUserAgent(); HttpClient httpClient = null; PostMethod httpPost = null; String responseBody = ""; int time = 0; do { try { httpClient = getHttpClient(); httpPost = getHttpPost(url, userAgent); StringRequestEntity requestEntity = new StringRequestEntity( json, "application/json", "UTF-8"); httpPost.setRequestEntity(requestEntity); int statusCode = httpClient.executeMethod(httpPost); if (statusCode != HttpStatus.SC_OK) { responseBody = SERVER_CONNECT_ERROR; break; } else { responseBody = httpPost.getResponseBodyAsString(); break; } } catch (Exception e) { time++; if (time < RETRY_TIME) { try { Thread.sleep(1000); } catch (InterruptedException e1) { } continue; } responseBody = SERVER_CONNECT_ERROR; } finally { // ???? if (null != httpPost) { httpPost.releaseConnection(); } httpClient = null; } } while (time < RETRY_TIME); Logger.d(TAG, "post response: " + url + " /r/n " + responseBody); return responseBody; } }