android开发http请求POST&GET封装工具

来源:互联网 发布:臻云创投投资人工智能 编辑:程序博客网 时间:2024/06/10 20:36
最近一直想着封装个网络请求框架,但是看到有很多博客有封装而且还不错,于是就想着封装一个安卓带的,以便自己以后使用.一,请求成功和失败回调接口
public interface OnResponseListner {    void onSucess(String response);    void onError(String error);}
二,post&get请求方法工具类
public class HttpUtlis {    /**     *get请求封装     */    public static void getRequest(String url, Map<String,String> params, String encode,OnResponseListner listner) {        StringBuffer sb = new StringBuffer(url);        sb.append("?");        if (params!=null && !params.isEmpty()){            for (Map.Entry<String,String> entry:params.entrySet()) {    //增强for遍历循环添加拼接请求内容                sb.append(entry.getKey()).append("=").append(entry.getValue()).append("&");            }            sb.deleteCharAt(sb.length()-1);            if (listner!=null) {                try {                    URL path = new URL(sb.toString());                    if (path!=null) {                        HttpURLConnection con = (HttpURLConnection) path.openConnection();                        con.setRequestMethod("GET");    //设置请求方式                        con.setConnectTimeout(3000);    //链接超时3秒                        con.setDoOutput(true);                        con.setDoInput(true);                        OutputStream os = con.getOutputStream();                        os.write(sb.toString().getBytes(encode));                        os.close();                        if (con.getResponseCode() == 200) {    //应答码200表示请求成功                            onSucessResopond(encode, listner, con);                        }                    }                } catch (Exception error) {                    error.printStackTrace();                    onError(listner, error);                }            }        }    }    /**     * POST请求     */    public static void postRequest(String url,Map<String,String> params,String encode,OnResponseListner listner){        StringBuffer sb = new StringBuffer();        if (params!=null && !params.isEmpty()){            for (Map.Entry<String,String> entry: params.entrySet()) {                sb.append(entry.getKey()).append("=").append(entry.getValue()).append("&");            }                sb.deleteCharAt(sb.length()-1);        }        if (listner!=null) {            try {                URL path = new URL(url);                if (path!=null){                    HttpURLConnection con = (HttpURLConnection) path.openConnection();                    con.setRequestMethod("POST");   //设置请求方法POST                    con.setConnectTimeout(3000);                    con.setDoOutput(true);                    con.setDoInput(true);                    byte[] bytes = sb.toString().getBytes();                    OutputStream outputStream = con.getOutputStream();                    outputStream.write(bytes);                    outputStream.close();                    if (con.getResponseCode()==200){                        onSucessResopond(encode, listner,  con);                    }                }            } catch (Exception e) {                e.printStackTrace();                onError(listner, e);            }        }    }    private static void onError(OnResponseListner listner,Exception onError) {        listner.onError(onError.toString());    }    private static void onSucessResopond(String encode, OnResponseListner listner, HttpURLConnection con) throws IOException {        InputStream inputStream = con.getInputStream();        ByteArrayOutputStream baos = new ByteArrayOutputStream();//创建内存输出流        int len = 0;        byte[] bytes = new byte[1024];        if (inputStream != null) {            while ((len = inputStream.read(bytes)) != -1) {                baos.write(bytes, 0, len);            }            String str = new String(baos.toByteArray(), encode);            listner.onSucess(str);        }    }}

三,activity中使用直接类名静态方法调用

   String url="";    String encode="utf-8";    public void GET(View view){        Map<String,String> map=new HashMap<>();        map.put("userName","");        map.put("pwd","");        HttpUtlis.getRequest(url, map,encode, new OnResponseListner() {            @Override            public void onSucess(String response) {            }            @Override            public void onError(String error) {            }        });    }
2 0