Android HttpUtil工具类

/**
 *  get 方式 请求  返回指定类型
 * @param mContext
 * @param url 路径
 * @param requestMap 参数值,转json 传输
 * @param callback 结果回调
 */
public void get(final Context mContext, String url, RequestMapBean requestMap, final ResultCallback callback) {
    RequestParams param = RequestEncryDecryptUtil.commonJsonRequestParams(mContext, requestMap);
    AsyncHttpUtil.get(mContext, url, param, new BaseJsonHttpResponseParse(mContext) {
        @Override
        public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
            super.onSuccess(statusCode, headers, response);
               if (callback.mType == String.class) {
                    callback.onSuccess(response);
                } else {
					Object o = mGson.fromJson(string, callback.mType);
					callback.onSuccess(o);
                }
        }

        @Override
        public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
            super.onFailure(statusCode, headers, throwable, errorResponse);
            callback.onError(errorResponse);
        }
    });
}

public  abstract class ResultCallback<T>
{
    Type mType;

    public ResultCallback()
    {
        mType = getSuperclassTypeParameter(getClass());
    }

    static Type getSuperclassTypeParameter(Class<?> subclass)
    {
        Type superclass = subclass.getGenericSuperclass();
        if (superclass instanceof Class)
        {
            throw new RuntimeException("Missing type parameter.");
        }
        ParameterizedType parameterized = (ParameterizedType) superclass;
        return $Gson$Types.canonicalize(parameterized.getActualTypeArguments()[0]);
    }

    public abstract void onSuccess(T response);

    public abstract void onError(JSONObject errorResponse);
}

public class AsyncHttpUtil {

    private static final String TAG = "AsyncHttpUtil";

    private static AsyncHttpClient client = new AsyncHttpClient();

    public static void get(Context context, String url, RequestParams param, AsyncHttpResponseHandler handler) {
        if (PhoneUtil.isConnectNet(context)) {;
            client.get(context, getAbsoluteUrl(url), param, handler);
        } else {
            ToastUtil.showMsg(context, "网络已断开");
            handler.sendFinishMessage();
            handler.onCancel();
        }
    }

}


转载于:https://my.oschina.net/u/2363160/blog/531037

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值