android新增修改数据 采用Post向服务器端发送请求
来源:互联网 发布:经济衰退 知乎 编辑:程序博客网 时间:2024/04/27 16:39
由于新增修改所需要传递的参数过多,牵扯到数据安全问题,所以采用post发送参数比较合理。
前端将参数打包到map中
Map<String, String> map = new HashMap<String, String>(); map.put("target_code", txt_target_code.getText().toString()); map.put("target_name", txt_target_name.getText().toString());
数据访问中,将map分解,并通过HttpURLConnection以输出流的形式将参数post传递到服务器指定的URL中
/* * Function : 发送Post请求到服务器 * Param : params请求体内容,encode编码格式 */ public static String submitPostData(String path,Map<String, String> params) { byte[] data = getRequestData(params, en_code).toString().getBytes();//获得请求体 try { URL url = new URL(path); HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection(); httpURLConnection.setConnectTimeout(3000); //设置连接超时时间 httpURLConnection.setDoInput(true); //打开输入流,以便从服务器获取数据 httpURLConnection.setDoOutput(true); //打开输出流,以便向服务器提交数据 httpURLConnection.setRequestMethod("POST"); //设置以Post方式提交数据 httpURLConnection.setUseCaches(false); //使用Post方式不能使用缓存 //设置请求体的类型是文本类型 httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); httpURLConnection.setRequestProperty("Charset", en_code); //设置请求体的长度 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(); flag = false; return dealResponseResult(inptStream); //处理服务器的响应结果 } } catch (IOException e) { e.printStackTrace(); } flag = false; return ""; } /* * Function : 封装请求体信息 * Param : params请求体内容,encode编码格式 */ public static 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(entry.getValue()) .append("&"); } stringBuffer.deleteCharAt(stringBuffer.length() - 1); //删除最后的一个"&" } catch (Exception e) { e.printStackTrace(); } return stringBuffer; } /* * Function : 处理服务器的响应结果(将输入流转化成字符串) * Param : inputStream服务器的响应输入流 */ public static 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新增修改数据 采用Post向服务器端发送请求
- Android GET,POST向服务器端发送数据(发送)
- Android GET,POST向服务器端发送数据(发送)
- 向服务器端发送请求 GET POST
- Android向Web服务器发送Post请求并返回数据
- Android向Web服务器发送Post请求并返回数据
- IOS总结_IOS以post方式向PHP服务器端发送json数据请求
- post请求向服务器发送数据
- Android客户端连接服务器端,向服务器端发送请求HttpURLConnection
- Android客户端连接服务器端,向服务器端发送请求HttpURLConnection
- Android客户端连接服务器端,向服务器端发送请求HttpURLConnection
- Android向服务器端发送json数据
- Android向服务器端发送json数据
- 采用Android中的httpclient框架发送post请求
- Android向Web站点发送GET请求、POST请求
- Android开发使用POST方式向服务器请求和发送数据
- c++ HTTP POST请求 向服务器发送json数据
- libcurl HTTP POST请求向服务器发送json数据
- UNIX 高手的 10 个习惯
- c++中创建单实例类
- 用ADT来描述学生考试成绩(仅供参考)
- 腾讯qq2014官方正式版 v6.3.12390 免费版
- web工作流管理系统开发之六 表单权限与流程的权限控制
- android新增修改数据 采用Post向服务器端发送请求
- python MySQLdb在windows环境下的快速安装、问题解决方式
- JAVA知识点总结
- C语言_变量
- 9四种布局
- 云计算受限海量数据 雾计算成驱动未来科技真正力量
- Lambda表达式详解
- 常用的正则校验
- C语言11中的auto和decltype