Android之用HttpURLConnection参数以XML形式封装的部分关键代码

来源:互联网 发布:cn域名过期多久删除 编辑:程序博客网 时间:2024/05/16 07:01

HttpURLConnection参数以XML形式封装

 //得到连接    public static HttpURLConnection setRequest(String urlStr) {        HttpURLConnection con = null;        try {            URL url = new URL(urlStr);            try {                con = (HttpURLConnection) url.openConnection();                con.setDoOutput(true);                con.setDoInput(true);                con.setRequestProperty("Pragma:", "no-cache");                con.setRequestMethod("POST");                con.setRequestProperty("Cache-Control", "no-cache");                con.setRequestProperty("Content-Type", "text/xml");            } catch (IOException e) {                e.printStackTrace();            }        } catch (MalformedURLException e) {            e.printStackTrace();        }        return con;    }

请求参数xml的封装,以注册为例子

//注册参数    public static String getXmlRegister(String username, String userpwd, String mobile, String email, String sex, String birthday, String address) {        StringBuilder sb = new StringBuilder();        sb.append("<xml>");        sb.append("<username>" + username + "</username>");        sb.append("<userpwd>" + userpwd + "</userpwd>");        sb.append("<mobile>" + mobile + "</mobile>");        sb.append("<email>" + username + "</email>");        if (sex != null) {            sb.append("<sex>" + sex + "</sex>");//是否必填 否        }        if (birthday != null) {            sb.append("<birthday>" + birthday + "</birthday>");//是否必填 否        }        if (address != null) {            sb.append("<address>" + address + "</address>");//是否必填 否        }        sb.append("</xml>");        return sb.toString();    }

注册请求代码

 //注册请求    public static String postByRegister(String urlStr, String username, String pwd, String mobile, String email, String sex, String birthday, String address) {        OutputStreamWriter out = null;        String line = "";        BufferedReader in = null;        try {            HttpURLConnection con = setRequest(urlStr);            out = new OutputStreamWriter(con.getOutputStream());            String xmlInfo = getXmlRegister(username, pwd, mobile, email, sex, birthday, address);            System.out.println("urlStr=" + urlStr);            System.out.println("xmlInfo=" + xmlInfo);            out.write(new String(xmlInfo.getBytes("ISO-8859-1")));            out.flush();            out.close();            // 使用输出流来输出字符(可选)            if (con.getResponseCode() != 200) throw new RuntimeException("请求url失败");            InputStream is = con.getInputStream();// 获取返回数据            ByteArrayOutputStream baout = new ByteArrayOutputStream();            byte[] buf = new byte[1024];            int len;            while ((len = is.read(buf)) != -1) {                baout.write(buf, 0, len);            }            line = new String(baout.toByteArray());            System.out.println(line);            out.close();            return line;        } catch (MalformedURLException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        }        return line;    }


0 0