com.worthed.googleplus.HttpUtils.java Source code

Java tutorial

Introduction

Here is the source code for com.worthed.googleplus.HttpUtils.java

Source

/**
 * Copyright 2014 Zhenguo Jin
 *
 * 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
 *
 *   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.worthed.googleplus;

import java.io.IOException;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.params.HttpClientParams;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import android.util.Log;

/**
 * @author jingle1267@163.com
 *
 */
public class HttpUtils {
    private final String TAG = HttpUtils.class.getSimpleName();

    public static final String ERROR_PREFIX = "Error:";

    /**
     * Send post request.
     * @param url 
     * @param params
     * @return It's a error if result start with "Error:".
     */
    public String doPost(String url, List<NameValuePair> params) {
        HttpClient httpClient = getHttpClient();
        /* HTTPPost */
        HttpPost httpRequest = new HttpPost(url);
        String strResult = ERROR_PREFIX;

        try {
            /* ? */
            httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
            /* ??? */
            HttpResponse httpResponse = httpClient.execute(httpRequest);
            /* ??200 ok */
            if (httpResponse.getStatusLine().getStatusCode() == 200) {
                /* ? */
                strResult = EntityUtils.toString(httpResponse.getEntity());
            } else {
                strResult += httpResponse.getStatusLine().toString();
            }
        } catch (ClientProtocolException e) {
            strResult += e.getMessage().toString();
            e.printStackTrace();
            return null;
        } catch (IOException e) {
            strResult += e.getMessage().toString();
            e.printStackTrace();
            return null;
        } catch (Exception e) {
            strResult += e.getMessage().toString();
            e.printStackTrace();
            return null;
        }
        Log.w(TAG, strResult);
        return strResult;
    }

    private HttpClient getHttpClient() {
        //  HttpParams ? HTTP ??
        HttpParams httpParams = new BasicHttpParams();

        //  Socket ? Socket ?
        HttpConnectionParams.setConnectionTimeout(httpParams, 20 * 1000);
        HttpConnectionParams.setSoTimeout(httpParams, 20 * 1000);
        HttpConnectionParams.setSocketBufferSize(httpParams, 8192);

        // ??? true
        HttpClientParams.setRedirecting(httpParams, true);

        //  user agent
        String userAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2) Gecko/20100115 Firefox/3.6";
        HttpProtocolParams.setUserAgent(httpParams, userAgent);

        //  HttpClient 
        // ? HttpClient httpClient = new HttpClient(); Commons HttpClient
        //  Android 1.5 ? Apache ? DefaultHttpClient
        HttpClient httpClient = new DefaultHttpClient(httpParams);

        return httpClient;
    }

}