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
- Android之用HttpURLConnection参数以XML形式封装的部分关键代码
- android 短信发信器的部分关键代码
- android开发之res下的menu (xml+代码的形式)
- /*以流的形式获取请求参数*/
- 关于在struts2中interceptor 中使用ActionInvocation 对象获得HttpServletRequest中封装的参数,以Map的形式
- Android中以JAR形式封装控件 或者类库
- Android中以JAR形式封装控件或者类库
- Android中以JAR形式封装控件 或者类库
- Android中以JAR形式封装控件 或者类库
- Android中以JAR形式封装控件或者类库
- 个人的关键代码段封装
- 自定义回调 并将之以参数的形式传入
- xml 将xml文档以表格的形式输出
- 上线代码关键部分
- 一个HttpURLConnection的封装
- 将NoseXUnit生成的XML以报表的形式显示~
- android HttpURLConnection post 参数
- 获取request中的参数,以Map的形式返回
- LintCode-剑指Offer-(204)单例
- [super dealloc]内存释放的先后顺序
- iOS内存话题: performSelector 导致不立即 dealloc
- centos 5.x install vestaCP 0.9.18-15
- JAVA基础课总结二
- Android之用HttpURLConnection参数以XML形式封装的部分关键代码
- 杭电ACM 1090
- Huffman树
- 对Dreamweaver的简单了解
- pthread_join()和pthread_detach()
- JAVA基础课总结三
- 一起talk C栗子吧(第六十一回:C语言实例--字符串连接)
- HNOI2002 营业额统计
- 多网卡的7种bond模式原理