使用xutils发送POST请求,携带json和图片二进制文件数据获取服务器端返回json数据
2014-09-02 17:42
1361 查看
接口文档:
params.addBodyParameter(key, stream, length, fileName, mimeType);
ResponseStream responseStream = http.sendSync(method, url, params);
换头像 接口 user/change_avatar 发送数据 HTTP Post body(一共2对KEY-VALUE): json={"uid":"1","sid":"0123456789ABCDEF0123456789ABCDEF","ver":"1","request":{}} file=图片二进制文件数据 返回数据 {"ret":0,"response":{ "status":1, "url":"http://192.168.1.200:8088/thumb.php?src=984340199_1667541218_1540991412.jpg&t=a&w=112&h=112" } } 遇到的问题: 首先在封装二进制请求体参数时,传递参数不足,导致服务器端不识别POST请求。
HttpUtils http = new HttpUtils(); RequestParams params = new RequestParams(); params.addBodyParameter("json", json); for (int i = 0; i < files.size(); i++) { if (files.get(i) != null) { try { params.addBodyParameter("file"+i, new FileInputStream( files.get(i)), files.get(i).length(), files.get(i) .getName(), "application/octet-stream"); } catch (FileNotFoundException e) { e.printStackTrace(); } } }
params.addBodyParameter(key, stream, length, fileName, mimeType);
然后在发送POST请求时,使用的是异步方法,导致不能返回服务器返回的值。
String s = null; try { // 同步方法,获取服务器端返回的流 ResponseStream responseStream = http.sendSync(HttpMethod.POST, url, params); s = responseStream.readString(); } catch (Exception e) { e.printStackTrace(); } return s;
ResponseStream responseStream = http.sendSync(method, url, params);
下面附上发送POST请求的完整代码:
/** * 发送POST请求,携带json和多个文件参数得到服务器返回的结果(json格式),必须要开启子线程调用。否则得不到数据。 * * @param url 请求地址 * @param json 请求体中封装的json字符串 * @param List<File> 上传多个文件 * @return String 服务器返回的结果 */ public static String sendPost(String url, String json, List<File> files) { HttpUtils http = new HttpUtils(); RequestParams params = new RequestParams(); params.addBodyParameter("json", json); if (files.size() == 1) { try { params.addBodyParameter("file", new FileInputStream(files.get(0)), files.get(0) .length(), files.get(0).getName(), "application/octet-stream"); } catch (FileNotFoundException e) { e.printStackTrace(); } } else { for (int i = 0; i < files.size(); i++) { if (files.get(i) != null) { try { params.addBodyParameter("file" + i, new FileInputStream(files.get(i)), files.get(i) .length(), files.get(i).getName(), "application/octet-stream"); } catch (FileNotFoundException e) { e.printStackTrace(); } } } } String s = null; try { // 同步方法,获取服务器端返回的流 ResponseStream responseStream = http.sendSync(HttpMethod.POST, url, params); s = responseStream.readString(); } catch (Exception e) { e.printStackTrace(); } return s; }
相关文章推荐
- 使用handler和Message获取xutils发送POST请求从服务器端返回数据
- 使用handler和Message获取xutils发送POST请求从服务器端返回数据
- 使用jQuery发送POST,Ajax请求返回JSON格式数据
- Android使用HttpClient以Post、Get请求服务器发送数据的方式(普通和json)
- 使用web压力测试工具 siege 和 ab 发送带json数据的post请求
- jquery中使用ajax发送post请求返回数据在error中
- Android使用HttpClient以Post、Get请求服务器发送数据的方式(普通和json)
- 初涉Ajax,以post或get方法发送数据,以json或xml形式接收服务器返回的请求
- jQuery从服务器获取数据使用post(字典查询的功能),服务器端php返回数据
- jQuery以Post方式发送请求并获取返回的文件供下载!
- PHP 使用curl库来发送GET,POST请求,处理json格式数据
- Ajax在静态页面中向指定url发送json请求获取返回的json数据
- Android Studio利用异步任务AsyncTask发送post请求获取json数据
- android菜鸟学习笔记25----与服务器端交互(二)解析服务端返回的json数据及使用一个开源组件请求服务端数据
- java中使用scoket模拟http post请求发送图片或文件
- 实习第一天——做一个录音上传服务器的小功能pttp协议post发送文件和参数到服务器,解析返回的json数据
- 页面使用$.getJSON只获取一次数据,导致多个请求返回的都是第一个结果
- Android使用HttpClient以Post、Get请求服务器发送数据的方式(普通和json)
- java中使用scoket模拟http post请求发送图片或文件
- 【转】asp.net(c#)使用HttpWebRequest附加携带请求参数以post方式模拟上传大文件(以图片为例)到Web服务器端