使用 httpurlconnection 在android上post操作
来源:互联网 发布:网络专升本 编辑:程序博客网 时间:2024/04/30 18:35
由于httppost被遗弃了,将项目中的一些post操作改成了httpurlconnection
相比httppost,httpurlconnection麻烦的地方在于需要处理输入输出流和将参数转化成字符串
标准的写法应该是
try { URL url = new URL(API_model.getInstance().record_submit_single()); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.addRequestProperty("encoding","UTF-8"); conn.setDoInput(true); conn.setDoOutput(true); conn.setRequestMethod("POST"); OutputStream os = conn.getOutputStream(); OutputStreamWriter osw = new OutputStreamWriter(os); BufferedWriter writer = new BufferedWriter(osw); List<NameValuePair> params = new ArrayList<>(); params.add(new BasicNameValuePair("member_id", Global.getInstance().getMember_id())); params.add(new BasicNameValuePair("answer", checklist_choice)); params.add(new BasicNameValuePair("record_item_id", level2_record_item_id)); params.add(new BasicNameValuePair("attempt", Attempt)); writer.write(getQuery(params)); writer.flush(); InputStream is = conn.getInputStream(); InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr); String line; StringBuilder builder = new StringBuilder(); while ((line = br.readLine())!= null){ builder.append(line); } writer.close(); osw.close(); os.close(); br.close(); isr.close(); is.close(); System.out.println("builder:"+builder.toString()); } catch (ProtocolException e) { e.printStackTrace(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }API_model.getInstance().record_submit_single()就是目标地址
getQuery()就是将参数转化成字符串
他的代码如下
private String getQuery(List<NameValuePair> params) throws UnsupportedEncodingException { StringBuilder result = new StringBuilder(); boolean first = true; for (NameValuePair pair : params) { if (first) first = false; else result.append("&"); result.append(URLEncoder.encode(pair.getName(), "UTF-8")); result.append("="); result.append(URLEncoder.encode(pair.getValue(), "UTF-8")); } System.out.println("result:" + result); return result.toString(); }builder就是返回的网页内容
0 0
- 使用 httpurlconnection 在android上post操作
- Android 怎么样使用HttpURLConnection POST?
- Android 使用HttpURLConnection发送Post/Get请求
- android使用HttpURLConnection发送Post/Get请求
- Android HttpURLConnection post请求
- android HttpURLConnection post 参数
- Android上使用HttpURLConnection发送HTTP请求
- 在JAVA中使用HttpURLConnection发送POST请求
- android 使用HttpURLConnection方式提交get/post请求
- Android使用HttpURLConnection发送Post请求到服务端
- Android使用HttpURLConnection进行POST请求,向服务器上传数据
- Android中如何使用HttpURLConnection实现GET POST JSON数据
- Android使用HttpURLConnection向服务器发送post和get请求
- android 使用httpURLConnection post方式访问接口的简单工具包
- Android HttpURLconnection使用POST请求参数是json
- android post HttpURLConnection乱码解决
- Android中httpURLconnection-post+get
- Android httpUrlConnection POST 文件上传
- chrome禁用缓存:调试html5方便
- ZOJ 3203 Light Bulb
- jsp中显示汉字,引入包,打印时间
- 6、React中的表单
- 线性表之静态链表
- 使用 httpurlconnection 在android上post操作
- 最基础的 iOS 推送流程 —— 仅供开发时真机调试走通推送流程使用
- Bitmapfun -- ImageFetcher类 翻译
- LeetCode 326:Power of Three
- java字符串按照逗号分开
- Mysql中DATETIME、DATE和TIMESTAMP类型的特征和区别
- 流数据平台阅读笔记
- Retrofit+RxJava实战日志(3)-网络异常处理
- 哲学家就餐问题