Android Open Source - Peterson Http Request






From Project

Back to project page Peterson.

License

The source code is released under:

GNU General Public License

If you think the Android project Peterson listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*******************************************************************************
 * Copyright (c) 2013 Zheng Sun.//  ww  w .  j av  a  2s  . c om
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v3.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/gpl.html
 * 
 * Contributors:
 *     Zheng Sun - initial API and implementation
 ******************************************************************************/

package tv.huohua.peterson.network;

import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

public class HttpRequest {
    final public static String HTTP_METHOD_DELETE = "DELETE";
    final public static String HTTP_METHOD_GET = "GET";
    final public static String HTTP_METHOD_POST = "POST";
    final public static String HTTP_METHOD_PUT = "PUT";

    private HttpEntity entity;
    private SortedMap<String, String> headers;
    final private String httpMethod;
    private SortedMap<String, String> params;
    final private String url;

    public HttpRequest(final String url) {
        this(url, HTTP_METHOD_GET, new TreeMap<String, String>());
    }

    public HttpRequest(final String url, final String httpMethod) {
        this(url, httpMethod, new TreeMap<String, String>());
    }

    public HttpRequest(final String url, final String httpMethod, final SortedMap<String, String> params) {
        this.httpMethod = httpMethod;
        this.url = url;
        this.params = params;
        this.headers = new TreeMap<String, String>();
    }

    public void addHeader(final String name, final String value) {
        headers.put(name, value);
    }

    public HttpEntity getEntity() {
        return entity;
    }

    public String getHeader(final String name) {
        return headers.get(name);
    }

    public SortedMap<String, String> getHeaders() {
        return headers;
    }

    public String getHttpMethod() {
        return httpMethod;
    }

    public SortedMap<String, String> getParams() {
        return params;
    }

    public byte[] getParamsAsByteArray() {
        return getParamsAsString().getBytes();
    }

    public List<NameValuePair> getParamsAsList() {
        final List<NameValuePair> result = new ArrayList<NameValuePair>();
        if (params != null) {
            final Iterator<Map.Entry<String, String>> iterator = params.entrySet().iterator();
            while (iterator.hasNext()) {
                final Map.Entry<String, String> kv = iterator.next();
                result.add(new BasicNameValuePair(kv.getKey(), kv.getValue()));
            }
        }
        return result;
    }

    @SuppressWarnings("deprecation")
    public String getParamsAsString() {
        if (params == null) {
            return "";
        } else {
            final StringBuilder builder = new StringBuilder();
            final Iterator<Map.Entry<String, String>> iterator = params.entrySet().iterator();
            while (iterator.hasNext()) {
                final Map.Entry<String, String> kv = iterator.next();
                if (builder.length() > 0) {
                    builder.append("&");
                }
                builder.append(URLEncoder.encode(kv.getKey()));
                builder.append("=");
                builder.append(URLEncoder.encode(kv.getValue()));
            }
            return builder.toString();
        }
    }

    public String getUrl() {
        return url;
    }

    public void setEntity(final HttpEntity entity) {
        this.entity = entity;
    }

    public void setHeaders(final SortedMap<String, String> headers) {
        this.headers = headers;
    }

    public void setParams(final SortedMap<String, String> params) {
        this.params = params;
    }
}




Java Source Code List

tv.huohua.peterson.api.AbsApi.java
tv.huohua.peterson.api.AbsListApi.java
tv.huohua.peterson.api.ApiCallResponse.java
tv.huohua.peterson.misc.ApplicationUtils.java
tv.huohua.peterson.misc.DeviceUtils.java
tv.huohua.peterson.misc.IOUtils.java
tv.huohua.peterson.misc.JavaLangUtils.java
tv.huohua.peterson.misc.Pair.java
tv.huohua.peterson.network.HttpRequest.java
tv.huohua.peterson.network.NetworkMgr.java
tv.huohua.peterson.network.NetworkUtils.java
tv.huohua.peterson.social.ISocialAuthorizer.java
tv.huohua.peterson.social.TencentWeiboAccessTokenKeeper.java
tv.huohua.peterson.social.TencentWeiboAuthorizer.java
tv.huohua.peterson.social.WeiboAccessTokenKeeper.java
tv.huohua.peterson.social.WeiboApiCaller.java
tv.huohua.peterson.social.WeiboAuthorizer.java
tv.huohua.peterson.view.FixedAspectRatioImageView.java
tv.huohua.peterson.view.FixedAspectRatioRelativeLayout.java
tv.huohua.peterson.view.HHListView.java
tv.huohua.peterson.view.HorizontalPagerListener.java
tv.huohua.peterson.view.HorizontalPager.java