com.micro.utils.H.java Source code

Java tutorial

Introduction

Here is the source code for com.micro.utils.H.java

Source

/*
 * Copyright (C) 2012 Chengel_HaltuD
 * 
 * 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.micro.utils;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import android.content.Context;

import com.micro.http.MicroBinaryHttpResponseListener;
import com.micro.http.MicroFileHttpResponseListener;
import com.micro.http.MicroHttpClient;
import com.micro.http.MicroHttpResponseListener;
import com.micro.http.MicroRequestParams;
import com.micro.http.MicroStringHttpResponseListener;

// TODO: Auto-generated Javadoc
/**
 * 
 * @ClassName: H
 * @Description: HAbHttpUtil ??Http??getpost??,HTTP
 * @AuthorChengel_HaltuD
 * @Date2015-5-30 ?3:05:56
 * @version V1.0
 *
 */
public class H {

    /** ?. */
    private MicroHttpClient mClient = null;

    /** ?. */
    private static H mAbHttpUtil = null;

    /**
     * ???.
     *
     * @param context the context
     * @return single instance of AbHttpUtil
     */
    public static H getInstance(Context context) {
        if (null == mAbHttpUtil) {
            mAbHttpUtil = new H(context);
        }

        return mAbHttpUtil;
    }

    /**
     * ?AbHttpUtil.
     *
     * @param context the context
     */
    private H(Context context) {
        super();
        this.mClient = new MicroHttpClient(context);
    }

    /**
     * ???get.
     *
     * @param url the url
     * @param responseListener the response listener
     */
    public void get(String url, MicroHttpResponseListener responseListener) {
        mClient.get(url, null, responseListener);
    }

    /**
     * ???get.
     *
     * @param url the url
     * @param params the params
     * @param responseListener the response listener
     */
    public void get(String url, MicroRequestParams params, MicroHttpResponseListener responseListener) {
        mClient.get(url, params, responseListener);
    }

    /**
     *  
     * ???byte?().
     *
     * @param url the url
     * @param responseListener the response listener
     */
    public void get(String url, MicroBinaryHttpResponseListener responseListener) {
        mClient.get(url, null, responseListener);
    }

    /**
     * ??get.
     *
     * @param url the url
     * @param params the params
     * @param responseListener the response listener
     */
    public void get(String url, MicroRequestParams params, MicroFileHttpResponseListener responseListener) {
        mClient.get(url, params, responseListener);
    }

    /**
     * ???post.
     *
     * @param url the url
     * @param responseListener the response listener
     */
    public void post(String url, MicroHttpResponseListener responseListener) {
        mClient.post(url, null, responseListener);
    }

    /**
     * ???post.
     *
     * @param url the url
     * @param params the params
     * @param responseListener the response listener
     */
    public void post(String url, MicroRequestParams params, MicroHttpResponseListener responseListener) {
        mClient.post(url, params, responseListener);
    }

    /**
     * ??post.
     *
     * @param url the url
     * @param params the params
     * @param responseListener the response listener
     */
    public void post(String url, MicroRequestParams params, MicroFileHttpResponseListener responseListener) {
        mClient.post(url, params, responseListener);
    }

    /**
     * ??.
     *
     * @param url the url
     * @param responseListener the response listener
     */
    public void request(String url, MicroStringHttpResponseListener responseListener) {
        request(url, null, responseListener);
    }

    /**
     * ??.
     *
     * @param url the url
     * @param params the params
     * @param responseListener the response listener
     */
    public void request(String url, MicroRequestParams params, MicroStringHttpResponseListener responseListener) {
        mClient.doRequest(url, params, responseListener);
    }

    /**
     * ??(?).
     *
     * @param timeout 
     */
    public void setTimeout(int timeout) {
        mClient.setTimeout(timeout);
    }

    /**
     * ssl ??(?).
     * @param enabled
     */
    public void setEasySSLEnabled(boolean enabled) {
        mClient.setOpenEasySSL(enabled);
    }

    /**
     * ?(?).
     * @param encode
     */
    public void setEncode(String encode) {
        mClient.setEncode(encode);
    }

    /**
     * ?(?).
     * @param userAgent
     */
    public void setUserAgent(String userAgent) {
        mClient.setUserAgent(userAgent);
    }

    /**
     * HttpClient
     * HttpClient????connection manager?  
     */
    public void shutdownHttpClient() {
        if (mClient != null) {
            mClient.shutdown();
        }
    }

    /********************************************************************************************************************************************************/

    // JSON
    public static String loadJsonFromNet(String url, String method) throws Exception {
        // HttpClient
        HttpClient client = new DefaultHttpClient();
        HttpUriRequest request = null;
        if ("get".equals(method)) {// get
            // get
            request = new HttpGet(url);
        } else if ("post".equals(method)) {// post
            request = new HttpPost(url);
        }

        // 
        HttpResponse response = client.execute(request);
        // 2xx:?;3xx:??;4xx:;5xx:?
        if (response.getStatusLine().getStatusCode() == 200) {
            // "".
            HttpEntity entity = response.getEntity();
            // ??
            // InputStream content = entity.getContent();
            return EntityUtils.toString(entity);
        }

        return null;

    }

    // 
    public static byte[] loadImgFromNet(String url, String method) throws Exception {
        // HttpClient
        HttpClient client = new DefaultHttpClient();
        HttpUriRequest request = null;
        if ("get".equals(method)) {// get
            // get
            request = new HttpGet(url);
        } else if ("post".equals(method)) {// post
            request = new HttpPost(url);
        }

        // 
        HttpResponse response = client.execute(request);
        // 2xx:?;3xx:??;4xx:;5xx:?
        if (response.getStatusLine().getStatusCode() == 200) {
            // "".
            HttpEntity entity = response.getEntity();
            // ??
            // InputStream content = entity.getContent();
            return EntityUtils.toByteArray(entity);
        }

        return null;

    }

}