android http post提交数据
来源:互联网 发布:逆回购 知乎 编辑:程序博客网 时间:2024/05/21 05:37
public String submitPostData(String strUrlPath, Map<String, String> params, String encode) { byte[] data = getRequestData(params, encode).toString().getBytes();//获得请求体 try { java.net.URL url = new URL(strUrlPath); HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); httpURLConnection.setConnectTimeout(3000); httpURLConnection.setDoInput(true); httpURLConnection.setDoOutput(true); httpURLConnection.setRequestMethod("POST"); httpURLConnection.setUseCaches(false); httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); httpURLConnection.setRequestProperty("Content-Length", String.valueOf(data.length)); OutputStream outputStream = httpURLConnection.getOutputStream(); outputStream.write(data); int response = httpURLConnection.getResponseCode(); if (response == HttpURLConnection.HTTP_OK) { InputStream inptStream = httpURLConnection.getInputStream(); return dealResponseResult(inptStream); } } catch (IOException e) { return "err: " + e.getMessage().toString(); } return "-1"; } public StringBuffer getRequestData(Map<String, String> params, String encode) { StringBuffer stringBuffer = new StringBuffer(); try { for (Map.Entry<String, String> entry : params.entrySet()) { stringBuffer.append(entry.getKey()) .append("=") .append(URLEncoder.encode(entry.getValue(), encode)) .append("&"); } stringBuffer.deleteCharAt(stringBuffer.length() - 1); } catch (Exception e) { e.printStackTrace(); } return stringBuffer; } public String dealResponseResult(InputStream inputStream) { String resultData = null; ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); byte[] data = new byte[1024]; int len = 0; try { while ((len = inputStream.read(data)) != -1) { byteArrayOutputStream.write(data, 0, len); } } catch (IOException e) { e.printStackTrace(); } resultData = new String(byteArrayOutputStream.toByteArray()); return resultData; }
0 0
- android http post提交数据
- http post提交数据
- http post提交数据
- android Post提交数据
- android post提交数据
- Android学习之Http使用Post方式进行数据提交
- 《老罗Android》学习之HTTP (三) POST提交数据
- Android Http使用Post方式进行数据提交
- Android学习之Http使用Post方式进行数据提交
- Android学习之Http使用Post方式进行数据提交
- Android -- 提交数据到服务器,Get Post方式, 异步Http框架提交
- Android Post方式提交数据
- http使用Post方式进行提交数据
- http使用post方式进行提交数据
- Android 如何用HttpClient 以Post方式提交数据并添加http头信息
- Android----用HTTP的get,post,HttpClient三种方式向web service提交文本数据
- Android 用HttpClient 以Post方式提交数据并添加http头信息
- Android之用HTTP的get,post,HttpClient三种方式向service提交文本数据
- java提示框
- ZOJ 3474 Taekwondo
- CSS font-family常见中文字体对应的英文名称
- 【笔试面试】简单总结笔试和面试中的海量数据问题
- 解决高德地图the map must have a size 错误
- android http post提交数据
- HiveSQL 进阶指南
- 内核并发竞态解决机制
- Android---keycode
- 跟小博老师一起学习数据库 ——JDBC-上篇
- hadoop日志
- JAVA-练习MyShopping升级菜单切换
- Chrome学习笔记(二):UI组件,皮肤引擎
- linux下shell脚本学习