返回 登录
0

AsyncHttpClient 请求

阅读6430

要用到的jar包:
android-async-http-1.4.8.jar

AysnHttpUtils:

package com.xxx.xxx.xxx;

import org.apache.http.protocol.HTTP;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.BinaryHttpResponseHandler;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.TextHttpResponseHandler;

/**
 * Created by sj on 2016/3/4.
 */
public class AysnHttpUtils {

    private static AsyncHttpClient client = new AsyncHttpClient(); // 实例话对象

    static {
        client.setTimeout(11000); // 设置链接超时,如果不设置,默认为10s
    }

    /**
     * 用一个完整url获取一个string对象
     * 
     * @param urlString
     * @param res
     */
    public static void get_String(String urlString, TextHttpResponseHandler res) {

        client.get(urlString, res);
    }

    /**
     * 获取json对象或者数组
     * 
     * @param urlString
     * @param res
     */
    public static void get_Json(String urlString, JsonHttpResponseHandler res) {
        client.get(urlString, res);
    }

    /**
     * 下载数据使用,会返回byte数据
     * 
     * @param uString
     * @param bHandler
     */
    public static void get_Byte(String uString,
            BinaryHttpResponseHandler bHandler) {
        client.get(uString, bHandler);
    }

    /*
     * //创建工厂对象 BitmapFactory bitmapFactory = new BitmapFactory();
     * //工厂对象的decodeByteArray把字节转换成Bitmap对象 Bitmap bitmap =
     * bitmapFactory.decodeByteArray(binaryData, 0, binaryData.length);
     */

    /**
     * url里面带参数
     * 
     * @param urlString
     * @param params
     * @param res
     */
    public static void post_String(String urlString, RequestParams params,
            TextHttpResponseHandler res) {
        params.setContentEncoding(HTTP.UTF_8);
        client.post(urlString, params, res);
    }

    /**
     * 带参数,获取json对象或者数组
     * 
     * @param urlString
     * @param params
     * @param res
     */
    public static void post_Json(String urlString, RequestParams params,
            JsonHttpResponseHandler res) {
        params.setContentEncoding(HTTP.UTF_8);
        client.post(urlString, params, res);
    }

    /**
     * 下载数据使用,会返回byte数据
     * 
     * @param uString
     * @param bHandler
     */
    public static void post_Byte(String uString, RequestParams params,
            BinaryHttpResponseHandler bHandler) {
        params.setContentEncoding(HTTP.UTF_8);
        client.post(uString, params, bHandler);
    }

    public static AsyncHttpClient getClient() {
        return client;
    }
}

用:

AysnHttpUtils.get_String(这里放你的访问网址url, new TextHttpResponseHandler() {
            @Override
            public void onFailure(int i, Header[] headers, String s, Throwable throwable) {
                Toast.makeText(getApplicationContext(),"网络错误",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onSuccess(int i, Header[] headers, String s) {
              Toast.makeText(getApplicationContext(),s,Toast.LENGTH_SHORT).show();
            }
        });
评论