(原创)分享一个自己封装的HttpURLConnection网络请求框架

这个框架是自己闲来无事封装的,主要用于请求json

1请求数据

public class HttpUtil {

    /**
     * 下载资源
     * @return
     */
    public static byte[] requestURL(String urlStr){
        InputStream in = null;
        ByteArrayOutputStream out = null;
        try {
            URL url = new URL(urlStr);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            conn.setReadTimeout(3000);
            conn.connect();
            if(conn.getResponseCode() == 200){
                in = conn.getInputStream();
                out = new ByteArrayOutputStream();

                byte[] buffer = new byte[1024 * 8];
                int len;
                while((len = in.read(buffer)) != -1){
                    out.write(buffer, 0, len);
                }

                //得到下载好的json字符串
                return out.toByteArray();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if(in != null){
                    in.close();
                }

                if(out != null){
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}

2基于刚才的类加上接口回调

public class DownUtil {

    /*
    创建一个拥有5个线程的线程池
     */
    private static ExecutorService executor = Executors.newFixedThreadPool(5);

    private OnDownListener onDownListener;

    private Handler handler = new Handler();

    //设置解析JSON的接口回调
    public DownUtil setOnDownListener(OnDownListener onDownListener) {
        this.onDownListener = onDownListener;
        return this;
    }

    /**
     * 下载JSON数据
     */
    public void downJSON(final String url){
        executor.submit(new Runnable() {
            @Override
            public void run() {
                //在子线程中执行
                byte[] bytes = HttpUtil.requestURL(url);
                if(bytes != null){
                    String json = new String(bytes);
                    //解析JSON
                    if(onDownListener != null){
                        final Object object = onDownListener.paresJson(json);
                        //将解析的结果回传给主线程
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                //在主线程中执行
                                onDownListener.downSucc(object);
                            }
                        });
                    }
                }
            }
        });
    }



    /**
     * 接口回调
     */
    public interface OnDownListener{
        //解析JSON时回调
        Object paresJson(String json);

        //解析完成后回调
        void downSucc(Object object);
    }
}

3用法

new DownUtil().setOnDownListener(上下文).downJSON(请求的url);

重写以下两个方法

 @Override
    public Object paresJson(String json) {
// 这里便是得到的json,你需要对其判断或者解析
        Log.d("print", "onSuccess: -->" + json);
        return null;
    }

    @Override
    public void downSucc(Object object) {
//回到主线程
    }

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值