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

来源:互联网 发布:windows 10的偏好设置 编辑:程序博客网 时间:2024/05/16 14:41

这个框架是自己闲来无事封装的,主要用于请求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) {//回到主线程    }


0 0