Android PHP服务网络交互POST
来源:互联网 发布:中国近10年gdp数据 编辑:程序博客网 时间:2024/05/29 13:39
public String post(String url, HashMap<String, String> param, List<File> files, String fileName) { String result = ""; String end = "\r\n"; String twoHyphens = "--"; String boundary = "ABCDEFG"; try { URL requestUrl = new URL(url); HttpURLConnection httpURLConnection = (HttpURLConnection) requestUrl.openConnection(); httpURLConnection.setChunkedStreamingMode(128 * 1024);//128k防止内存过高 //允许输入输出流 httpURLConnection.setDoInput(true); httpURLConnection.setDoOutput(true); httpURLConnection.setUseCaches(false); httpURLConnection.setRequestMethod("POST"); httpURLConnection.setRequestProperty("Connection", "Keep-Alive"); httpURLConnection.setRequestProperty("Charset", "UTF-8"); httpURLConnection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary); DataOutputStream dos = new DataOutputStream(httpURLConnection.getOutputStream()); //字段 Iterator iterator = param.entrySet().iterator(); while (iterator.hasNext()) { dos.writeBytes(twoHyphens + boundary + end); Map.Entry entry = (Map.Entry) iterator.next(); dos.writeBytes("Content-Disposition:form-data;name=\"" + entry.getKey() + "\"" + end); dos.writeBytes(end); dos.writeBytes(URLEncoder.encode(entry.getValue().toString(),"UTF-8")); dos.writeBytes(end); } //文件 for (File file : files) { dos.writeBytes(twoHyphens + boundary + end); dos.writeBytes("Content-Disposition:form-data;name=\"" + fileName + "\";filename=\"" + file.getName() + "\"" + end); dos.writeBytes(end); FileInputStream fis = new FileInputStream(file); byte[] buffer = new byte[8 * 1024]; int count = 0; while ((count = fis.read(buffer)) != -1) { dos.write(buffer, 0, count); } fis.close(); dos.writeBytes(end); } dos.writeBytes(twoHyphens + boundary + twoHyphens + end); dos.flush(); InputStream is = httpURLConnection.getInputStream(); InputStreamReader isr = new InputStreamReader(is, "UTF-8"); BufferedReader br = new BufferedReader(isr); result = br.readLine(); dos.close(); is.close(); } catch (Exception e) { e.printStackTrace(); Log.e("Android", e.getLocalizedMessage()); } return result; }
<pre name="code" class="java">public String post(String url, HashMap<String, String> param, List<File> files, String fileName) {}
url:请求地址
param:请求参数
files:要传送的文件
fileName:接受文件要用的名字
自己撸的服务端是PHP的
感觉自己撸的传输速度还是比较快的
0 0
- Android PHP服务网络交互POST
- Android网络交互Get,POST简介
- android 基础 网络交互 get/post
- android客户端和php服务简单交互
- android客户端和php服务简单交互
- android客户端和php服务简单交互
- android客户端和php服务简单交互
- Android 网络交互(一) HTTP post和get方法区别
- android使用Http的Post方式与网络交互通信
- java实现网络交互 get、post方法
- Android程序:使用Http的Post方式与网络交互通信
- php表单的交互(post方法)
- jquery与php交互之GET、 POST
- jquery与php交互之GET、 POST
- Android 网络交互
- android网络交互
- android php 交互
- android 基础 网络交互 HttpConnection交互
- 各种姿势折腾 QEMU
- 学习shader之前必须知道的东西之计算机图形学——渲染管线
- JSP中的EL表达式和JSTL标签库
- 安卓spinner
- angualrjs 学习笔记初篇
- Android PHP服务网络交互POST
- [leetcode]397. Integer Replacement
- Android文件三种上传方式总结
- 【BLE-CC2640】CC2640之广播MAC
- Quartz 2D 涂鸦
- 猫猫学IOS(三十四)UI之Quartz2D画画板的实现
- swift代码之路(三)AF网络请求
- iOS Quartz2D简介
- ubuntu 开发工具的安装 Ubuntu Make