AsyncHttpClient 请求

要用到的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();
            }
        });
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值