我试图做出HttpPost
与multiPart/form-data
通过我的Android应用程序.我有一个邮递员测试,正在使用我的api和postman中的请求预览,看起来像这样:
POST /api/0.1/content/upload HTTP/1.1 Host: 54.221.194.167 X-AUTHORIZATION: 166e649911ff424eb14446cf398bd7d6 Cache-Control: no-cache Postman-Token: 2412eba9-f72d-6f3b-b124-6070b5b26644 ----WebKitFormBoundaryE19zNvXGzXaLvS5C Content-Disposition: form-data; name="file01" {"mime_type":"image/jpeg","title":"IMG_20140131_111622"} ----WebKitFormBoundaryE19zNvXGzXaLvS5C Content-Disposition: form-data; name="file01"; filename="addedaslib.jpg" Content-Type: image/jpeg ----WebKitFormBoundaryE19zNvXGzXaLvS5C
我试图使用我的android HttpPost使用multipart/form-data复制它,但它似乎没有工作.有没有办法"预览"我的请求,看看它实际上是如何发布到api?我究竟做错了什么?我的代码:
public HttpResponse invokeXAUTHPOSTService(String url, String token, File file) { client = new DefaultHttpClient(); HttpPost request = new HttpPost(url); HttpResponse response = null; MultipartEntity mpe = new MultipartEntity(); try { Log.v("API", "URL:"+url); request.setHeader("Content-Type", "multipart/form-data"); request.addHeader("X-AUTHORIZATION",token); request.addHeader("Cache-Control", "no-cache"); DRPContentForUpload content = new DRPContentForUpload(file); String jsonObject = DRPJSONConverter.toJson(content); FormBodyPart part1= new FormBodyPart("file01", new StringBody(jsonObject)); FormBodyPart part2= new FormBodyPart("file01", new FileBody(file)); mpe.addPart(part1); mpe.addPart(part2); // request.setEntity(mpe); Log.v("RAW REQUEST", "request looks like:"+mpe.toString()); response = client.execute(request);
编辑
我能够和我的API团队交谈,他们说我的帖子实际上是这样的:
--0ieMJK6PPwcrM_K3KQvl6eNDGqooZPzJcvHOm0 Content-Disposition: form-data; name="file01" Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 8bit {"mime_type":"image/jpeg","title":"IMG_20140131_111622"} --0ieMJK6PPwcrM_K3KQvl6eNDGqooZPzJcvHOm0 Content-Disposition: form-data; name="file01"; filename="IMG_20140131_111622.jpg" Content-Type: application/octet-stream Content-Transfer-Encoding: binary
这说它仍然没有工作,并吐出一个错误,我错过了params
这是我项目中包含的库的屏幕截图:
所以在搜索到高分和低分以及几乎放弃之后,这个链接终于有了帮助
这是最终的工作代码:
public HttpResponse invokeXAUTHPOSTService(String url, String token, File file) { client = new DefaultHttpClient(); HttpPost request = new HttpPost(url); HttpResponse response = null; DRPContentForUpload content = new DRPContentForUpload(file); String jsonObject = DRPJSONConverter.toJson(content); String BOUNDARY= "--eriksboundry--"; request.setHeader("Content-Type", "multipart/form-data; boundary="+BOUNDARY); request.addHeader("X-AUTHORIZATION",token); MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE,BOUNDARY,Charset.defaultCharset()); try { entity.addPart("file01", new StringBody(jsonObject)); entity.addPart("file01", new FileBody(file)); request.addHeader("Accept-Encoding", "gzip, deflate"); } catch (UnsupportedEncodingException e) { Log.v("encoding exception","E::: "+e); e.printStackTrace(); } request.setHeader("Accept", "application/json"); request.setHeader("Content-Type", "multipart/form-data; boundary="+BOUNDARY); request.setEntity(entity); try { response = client.execute(request); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return response; }