com.drive.student.xutils.HttpUtils.java Source code

Java tutorial

Introduction

Here is the source code for com.drive.student.xutils.HttpUtils.java

Source

/**
 * Copyright (c) 2013. wyouflf (wyouflf@gmail.com)
 * <p/>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p/>
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.drive.student.xutils;

import android.text.TextUtils;

import com.drive.student.util.LogUtil;
import com.drive.student.xutils.exception.HttpException;
import com.drive.student.xutils.http.HttpCache;
import com.drive.student.xutils.http.HttpHandler;
import com.drive.student.xutils.http.RequestParams;
import com.drive.student.xutils.http.ResponseStream;
import com.drive.student.xutils.http.SyncHttpHandler;
import com.drive.student.xutils.http.callback.HttpRedirectHandler;
import com.drive.student.xutils.http.callback.RequestCallBack;
import com.drive.student.xutils.http.client.DefaultSSLSocketFactory;
import com.drive.student.xutils.http.client.HttpRequest;
import com.drive.student.xutils.http.client.HttpRequest.HttpMethod;
import com.drive.student.xutils.http.client.RetryHandler;
import com.drive.student.xutils.http.client.entity.GZipDecompressingEntity;
import com.drive.student.xutils.task.PriorityExecutor;
import com.drive.student.xutils.util.OtherUtils;

import org.apache.http.Header;
import org.apache.http.HeaderElement;
import org.apache.http.HttpEntity;
import org.apache.http.HttpRequestInterceptor;
import org.apache.http.HttpResponse;
import org.apache.http.HttpResponseInterceptor;
import org.apache.http.HttpStatus;
import org.apache.http.HttpVersion;
import org.apache.http.NameValuePair;
import org.apache.http.client.CookieStore;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.protocol.ClientContext;
import org.apache.http.conn.params.ConnManagerParams;
import org.apache.http.conn.params.ConnPerRouteBean;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HTTP;
import org.apache.http.protocol.HttpContext;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.UUID;

public class HttpUtils {

    private static HttpUtils httpUtils = null;

    public final static HttpCache sHttpCache = new HttpCache();

    private final DefaultHttpClient httpClient;

    private HttpRedirectHandler httpRedirectHandler;

    /**
     * ?HttpContext
     */
    public static HttpContext httpContext = new BasicHttpContext();

    /************************************ default settings & fields ****************************/

    /**
     * response ?
     */
    private String responseTextCharset = HTTP.UTF_8;

    private long currentRequestExpiry = HttpCache.getDefaultExpiryTime();
    /**
     * 
     */
    private final static int DEFAULT_CONN_TIMEOUT = 1000 * 15;
    /**
     * ?
     */
    private final static int DEFAULT_SO_TIMEOUT = 1000 * 10;
    /**
     * ?
     */
    private final static int DEFAULT_RETRY_TIMES = 2;
    /**
     * ???
     */
    private static final String HEADER_ACCEPT_ENCODING = "Accept-Encoding";
    /**
     * ??
     */
    private static final String ENCODING_GZIP = "gzip";
    /**
     * 
     */
    private final static int DEFAULT_POOL_SIZE = 3;

    private final static PriorityExecutor EXECUTOR = new PriorityExecutor(DEFAULT_POOL_SIZE);

    /**
     * 
     */
    /*
    private static final String VERSION_CODE = MainApplication.getInstance().getAPKVersionCode();
    *//**  **/
    /*
    private static final String OS_VERSION = Build.VERSION.RELEASE;
    *//** ? **/
    /*
    private static final String OS = "android";
    *//** ?? **/

    /*
    private static final String USER_NAME = MainApplication.getInstance().getUserName();
    */

    /**
     *
     * ?,???.
     */
    public static HttpUtils getInstance() {
        if (httpUtils == null) {
            httpUtils = new HttpUtils();
        }
        //      validateCookie();
        return httpUtils;
    }

    /**
     *
     * cookie?.
     * @return truefalse
     */
    /*private synchronized static boolean validateCookie() {
       CookieStore cookies = (CookieStore) httpContext.getAttribute(ClientContext.COOKIE_STORE);
       if (cookies == null || cookies.getCookies() == null) {
     httpContext = new BasicHttpContext();
     httpContext.setAttribute(ClientContext.COOKIE_STORE, MainApplication.getInstance().getCookieStore());
     return false;
       }
        
       try {
     HashMap<String, String> cookieMap = new HashMap<String, String>();
     for (Cookie cookie : cookies.getCookies()) {
        cookieMap.put(cookie.getName(), cookie.getValue());
     }
        
     boolean mfgMemberSginBool = (cookieMap.containsKey("mfg_membersgin") && !StringUtil.isEmpty(cookieMap.get("mfg_membersgin")));
     boolean mfgMemberIdBool = (cookieMap.containsKey("mfg_memberid") && !StringUtil.isEmpty(cookieMap.get("mfg_memberid")));
        
     if (mfgMemberSginBool && mfgMemberIdBool) {
        return true;
     } else {
        httpContext = new BasicHttpContext();
        httpContext.setAttribute(ClientContext.COOKIE_STORE, MainApplication.getInstance().getCookieStore());
     }
       } catch (Exception e) {
     // handle exception
       }
       return false;
    }*/

    /**
     * ?cookie
     */
    /*public static String getCookieStr() {
       String cookieStr = "";
       try {
     CookieStore cookieStore = (CookieStore) httpContext.getAttribute(ClientContext.COOKIE_STORE);
     if (cookieStore == null || cookieStore.getCookies() == null || cookieStore.getCookies().size() == 0) {
        return cookieStr;
     }
     for (Cookie c : cookieStore.getCookies()) {
        cookieStr += "; " + c.getName() + "=" + c.getValue() + "##" + c.getDomain();
     }
       } catch (Exception e) {
     return "";
       }
       return null == cookieStr ? "" : cookieStr;
    }*/
    public HttpUtils() {
        this(HttpUtils.DEFAULT_CONN_TIMEOUT, null);
    }

    public HttpUtils(int connTimeout) {
        this(connTimeout, null);
    }

    public HttpUtils(String userAgent) {
        this(HttpUtils.DEFAULT_CONN_TIMEOUT, userAgent);
    }

    public HttpUtils(int connTimeout, String userAgent) {
        HttpParams params = new BasicHttpParams();

        // configCookieStore(cookieStore); //?cookies
        ConnManagerParams.setTimeout(params, connTimeout);
        HttpConnectionParams.setSoTimeout(params, connTimeout);
        HttpConnectionParams.setConnectionTimeout(params, connTimeout);

        if (TextUtils.isEmpty(userAgent)) {
            userAgent = OtherUtils.getUserAgent(null);
        }
        HttpProtocolParams.setUserAgent(params, userAgent);

        ConnManagerParams.setMaxConnectionsPerRoute(params, new ConnPerRouteBean(10));
        ConnManagerParams.setMaxTotalConnections(params, 10);

        HttpConnectionParams.setTcpNoDelay(params, true);
        HttpConnectionParams.setSocketBufferSize(params, 1024 * 8);
        HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);

        SchemeRegistry schemeRegistry = new SchemeRegistry();
        schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
        schemeRegistry.register(new Scheme("https", DefaultSSLSocketFactory.getSocketFactory(), 443));

        httpClient = new DefaultHttpClient(new ThreadSafeClientConnManager(params, schemeRegistry), params);

        httpClient.setHttpRequestRetryHandler(new RetryHandler(DEFAULT_RETRY_TIMES));

        httpClient.addRequestInterceptor(new HttpRequestInterceptor() {
            @Override
            public void process(org.apache.http.HttpRequest httpRequest, HttpContext httpContext)
                    throws org.apache.http.HttpException, IOException {
                if (!httpRequest.containsHeader(HEADER_ACCEPT_ENCODING)) {
                    httpRequest.addHeader(HEADER_ACCEPT_ENCODING, ENCODING_GZIP);
                }
            }
        });

        httpClient.addResponseInterceptor(new HttpResponseInterceptor() {
            @Override
            public void process(HttpResponse response, HttpContext httpContext)
                    throws org.apache.http.HttpException, IOException {
                final HttpEntity entity = response.getEntity();
                if (entity == null) {
                    return;
                }
                final Header encoding = entity.getContentEncoding();
                if (encoding != null) {
                    for (HeaderElement element : encoding.getElements()) {
                        if (element.getName().equalsIgnoreCase("gzip")) {
                            response.setEntity(new GZipDecompressingEntity(response.getEntity()));
                            return;
                        }
                    }
                }
            }
        });
    }

    public HttpClient getHttpClient() {
        return this.httpClient;
    }

    /***************************************** config *******************************************/

    public HttpUtils configResponseTextCharset(String charSet) {
        if (!TextUtils.isEmpty(charSet)) {
            this.responseTextCharset = charSet;
        }
        return this;
    }

    public HttpUtils configHttpRedirectHandler(HttpRedirectHandler httpRedirectHandler) {
        this.httpRedirectHandler = httpRedirectHandler;
        return this;
    }

    public HttpUtils configHttpCacheSize(int httpCacheSize) {
        sHttpCache.setCacheSize(httpCacheSize);
        return this;
    }

    public HttpUtils configDefaultHttpCacheExpiry(long defaultExpiry) {
        HttpCache.setDefaultExpiryTime(defaultExpiry);
        currentRequestExpiry = HttpCache.getDefaultExpiryTime();
        return this;
    }

    public HttpUtils configCurrentHttpCacheExpiry(long currRequestExpiry) {
        this.currentRequestExpiry = currRequestExpiry;
        return this;
    }

    public HttpUtils configCookieStore(CookieStore cookieStore) {
        httpContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
        return this;
    }

    public HttpUtils configUserAgent(String userAgent) {
        HttpProtocolParams.setUserAgent(this.httpClient.getParams(), userAgent);
        return this;
    }

    public HttpUtils configTimeout(int timeout) {
        final HttpParams httpParams = this.httpClient.getParams();
        ConnManagerParams.setTimeout(httpParams, timeout);
        HttpConnectionParams.setConnectionTimeout(httpParams, timeout);
        return this;
    }

    public HttpUtils configSoTimeout(int timeout) {
        final HttpParams httpParams = this.httpClient.getParams();
        HttpConnectionParams.setSoTimeout(httpParams, timeout);
        return this;
    }

    public HttpUtils configRegisterScheme(Scheme scheme) {
        this.httpClient.getConnectionManager().getSchemeRegistry().register(scheme);
        return this;
    }

    public HttpUtils configSSLSocketFactory(SSLSocketFactory sslSocketFactory) {
        Scheme scheme = new Scheme("https", sslSocketFactory, 443);
        this.httpClient.getConnectionManager().getSchemeRegistry().register(scheme);
        return this;
    }

    public HttpUtils configRequestRetryCount(int count) {
        this.httpClient.setHttpRequestRetryHandler(new RetryHandler(count));
        return this;
    }

    public HttpUtils configRequestThreadPoolSize(int threadPoolSize) {
        HttpUtils.EXECUTOR.setPoolSize(threadPoolSize);
        return this;
    }

    /***************************************** send request *******************************************/

    public <T> HttpHandler<T> send(HttpRequest.HttpMethod method, String url, RequestCallBack<T> callBack) {
        return send(method, url, null, callBack);
    }

    public <T> HttpHandler<T> send(HttpRequest.HttpMethod method, String url, RequestParams params,
            RequestCallBack<T> callBack) {
        if (url == null) {
            throw new IllegalArgumentException("url may not be null");
        }
        if (params == null) {
            params = new RequestParams();
        }
        buildHttpHeaderForHP(params);// 
        if (method == HttpMethod.GET) {// ??
            params.addQueryStringParameter("t", UUID.randomUUID().toString());
        }
        HttpRequest request = new HttpRequest(method, url);
        return sendRequest(request, params, callBack);
    }

    public ResponseStream sendSync(HttpRequest.HttpMethod method, String url) throws HttpException {
        return sendSync(method, url, null);
    }

    public ResponseStream sendSync(HttpRequest.HttpMethod method, String url, RequestParams params)
            throws HttpException {
        if (url == null) {
            throw new IllegalArgumentException("url may not be null");
        }
        if (params == null) {
            params = new RequestParams();
        }
        buildHttpHeaderForHP(params);// 
        if (method == HttpMethod.GET) {// ??
            params.addQueryStringParameter("t", UUID.randomUUID().toString());
        }
        HttpRequest request = new HttpRequest(method, url);
        return sendSyncRequest(request, params);
    }

    /***************************************** download *******************************************/

    public HttpHandler<File> download(String url, String target, RequestCallBack<File> callback) {
        return download(HttpRequest.HttpMethod.GET, url, target, null, false, false, callback);
    }

    public HttpHandler<File> download(String url, String target, boolean autoResume,
            RequestCallBack<File> callback) {
        return download(HttpRequest.HttpMethod.GET, url, target, null, autoResume, false, callback);
    }

    public HttpHandler<File> download(String url, String target, boolean autoResume, boolean autoRename,
            RequestCallBack<File> callback) {
        return download(HttpRequest.HttpMethod.GET, url, target, null, autoResume, autoRename, callback);
    }

    public HttpHandler<File> download(String url, String target, RequestParams params,
            RequestCallBack<File> callback) {
        return download(HttpRequest.HttpMethod.GET, url, target, params, false, false, callback);
    }

    public HttpHandler<File> download(String url, String target, RequestParams params, boolean autoResume,
            RequestCallBack<File> callback) {
        return download(HttpRequest.HttpMethod.GET, url, target, params, autoResume, false, callback);
    }

    public HttpHandler<File> download(String url, String target, RequestParams params, boolean autoResume,
            boolean autoRename, RequestCallBack<File> callback) {
        return download(HttpRequest.HttpMethod.GET, url, target, params, autoResume, autoRename, callback);
    }

    public HttpHandler<File> download(HttpRequest.HttpMethod method, String url, String target,
            RequestParams params, RequestCallBack<File> callback) {
        return download(method, url, target, params, false, false, callback);
    }

    public HttpHandler<File> download(HttpRequest.HttpMethod method, String url, String target,
            RequestParams params, boolean autoResume, RequestCallBack<File> callback) {
        return download(method, url, target, params, autoResume, false, callback);
    }

    public HttpHandler<File> download(HttpRequest.HttpMethod method, String url, String target,
            RequestParams params, boolean autoResume, boolean autoRename, RequestCallBack<File> callback) {

        if (url == null) {
            throw new IllegalArgumentException("url may not be null");
        }
        if (target == null) {
            throw new IllegalArgumentException("target may not be null");
        }

        HttpRequest request = new HttpRequest(method, url);

        HttpHandler<File> handler = new HttpHandler<File>(httpClient, httpContext, responseTextCharset, callback);

        handler.setExpiry(currentRequestExpiry);
        handler.setHttpRedirectHandler(httpRedirectHandler);

        if (params != null) {
            request.setRequestParams(params, handler);
            handler.setPriority(params.getPriority());
        }
        handler.executeOnExecutor(EXECUTOR, request, target, autoResume, autoRename);
        return handler;
    }

    // //////////////////////////////////////////////////////////////////////////////////////////////
    private <T> HttpHandler<T> sendRequest(HttpRequest request, RequestParams params, RequestCallBack<T> callBack) {

        HttpHandler<T> handler = new HttpHandler<T>(httpClient, httpContext, responseTextCharset, callBack);

        handler.setExpiry(currentRequestExpiry);
        handler.setHttpRedirectHandler(httpRedirectHandler);
        request.setRequestParams(params, handler);

        if (params != null) {
            handler.setPriority(params.getPriority());
        }
        handler.executeOnExecutor(EXECUTOR, request);
        return handler;
    }

    private ResponseStream sendSyncRequest(HttpRequest request, RequestParams params) throws HttpException {

        SyncHttpHandler handler = new SyncHttpHandler(httpClient, httpContext, responseTextCharset);

        handler.setExpiry(currentRequestExpiry);
        handler.setHttpRedirectHandler(httpRedirectHandler);
        request.setRequestParams(params);

        return handler.sendRequest(request);
    }

    /**
     *
     * HttpClient??get?.
     */
    public HttpResponse sendHttpGet(String urlpath, boolean isGzip) {
        HttpClient httpclient = new DefaultHttpClient();
        try {
            HttpGet request = new HttpGet(urlpath);
            buildHttpHeaderForGet(request);// 
            if (isGzip) {
                request.addHeader("accept-encoding", "gzip");
            }
            httpclient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT,
                    HttpUtils.DEFAULT_CONN_TIMEOUT); // 
            httpclient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, HttpUtils.DEFAULT_SO_TIMEOUT); // ?
            return httpclient.execute(request, httpContext);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * HttpClient??get?.
     */
    public HttpResponse sendHttpGet(String url, Map params, boolean isGzip) {
        HttpClient httpclient = new DefaultHttpClient();
        httpclient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT,
                HttpUtils.DEFAULT_CONN_TIMEOUT); // 
        httpclient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, HttpUtils.DEFAULT_SO_TIMEOUT); // ?

        /* HTTPGet */
        String paramStr = "";
        if (params != null) {
            for (Object o : params.entrySet()) {
                Entry entry = (Entry) o;
                Object key = entry.getKey();
                Object val = entry.getValue();
                paramStr += paramStr = "&" + key + "=" + val;
            }
        }
        if (!paramStr.equals("")) {
            paramStr = paramStr.replaceFirst("&", "?");
            url += paramStr;
        }
        HttpGet request = new HttpGet(url);
        buildHttpHeaderForGet(request);// 
        if (isGzip) {
            request.addHeader("accept-encoding", "gzip");
        }
        try {
            /* ??? */
            HttpResponse response = httpclient.execute(request, httpContext);
            /* ??200 ok */
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                return response;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     *
     * HttpClient??post?.
     */
    public HttpResponse sendHttpPost(String url, Map<String, String> params, boolean isGzip) {
        try {
            List<NameValuePair> param = new ArrayList<NameValuePair>(); // ?
            if (params != null) {
                for (Entry<String, String> entry : params.entrySet()) {
                    param.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
                }
            }
            HttpPost request = new HttpPost(url);
            buildHttpHeaderForPost(request);// 
            if (isGzip) {
                request.addHeader("accept-encoding", "gzip");
            }
            HttpEntity entity = new UrlEncodedFormEntity(param, "UTF-8");
            request.setEntity(entity);
            HttpClient client = new DefaultHttpClient();

            client.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT,
                    HttpUtils.DEFAULT_CONN_TIMEOUT); // 
            client.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, HttpUtils.DEFAULT_SO_TIMEOUT); // ?
            HttpResponse response = client.execute(request, httpContext);
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                LogUtil.e("hxk", "upload file success -->>");
                return response;
            } else {
                LogUtil.e("hxk", "upload fail -->>" + response.getStatusLine().getStatusCode());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        LogUtil.e("hxk", "upload fail -->>");
        return null;
    }

    /**
     * Server
     *
     * @param urlStr
     *            ?
     * @param serverFileName
     *            ????? image.jpg
     * @param uploadFile
     *            ? /sdcard/a.jpg
     */
    public void uploadFile(String urlStr, String serverFileName, File uploadFile) {
        try {
            URL url = new URL(urlStr);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setChunkedStreamingMode(1024 * 1024);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("X-Requested-With", "XMLHttpRequest");
            conn.setRequestProperty("connection", "Keep-Alive");
            conn.setRequestProperty("Charsert", "UTF-8");
            conn.setRequestProperty("Content-Type", "multipart/form-data;file=" + uploadFile.getName());
            conn.setRequestProperty("filename", uploadFile.getName());
            OutputStream out = new DataOutputStream(conn.getOutputStream());
            DataInputStream in = new DataInputStream(new FileInputStream(uploadFile));
            int bytes = 0;
            byte[] bufferOut = new byte[1024];
            while ((bytes = in.read(bufferOut)) != -1) {
                out.write(bufferOut, 0, bytes);
            }
            in.close();
            out.flush();
            out.close();

            int response = conn.getResponseCode();

            if (response == 200) {
                BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                String line = null;
                while ((line = reader.readLine()) != null) {
                    System.out.println(line);
                }
                LogUtil.e("hxk", "upload file success-->>");
            } else {
                LogUtil.e("hxk", "upload file fail-->> response = " + response);
            }
        } catch (Exception e) {
            e.printStackTrace();
            LogUtil.e("hxk", "upload file fail-->>");
        }
    }

    /**
     *
     * ??().
     */
    private String printResponse(HttpURLConnection conn) {
        StringBuilder sb = new StringBuilder();
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String line;
            while ((line = reader.readLine()) != null) {
                sb.append("\n").append(line);
            }
            // System.out.println("==>" + sb);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return sb.toString();
    }

    private void buildHttpHeaderForHP(RequestParams params) {
        /*params.addHeader("versionCode", VERSION_CODE);
        params.addHeader("os", OS);
        params.addHeader("osVersion", OS_VERSION);*/
    }

    private void buildHttpHeaderForGet(HttpGet httpGet) {
        /*httpGet.addHeader("versionCode", VERSION_CODE);
        httpGet.addHeader("os", OS);
        httpGet.addHeader("osVersion", OS_VERSION);*/
    }

    private void buildHttpHeaderForPost(HttpPost httpPost) {
        /*httpPost.addHeader("versionCode", VERSION_CODE);
        httpPost.addHeader("os", OS);
        httpPost.addHeader("osVersion", OS_VERSION);*/
    }
}