【发布时间】:2012-06-08 18:05:10
【问题描述】:
我正在尝试使用 Apache Commons HttpClient 在 Android 设备上发布由相机拍摄的图像和一些其他参数。
我的发布请求正常工作,服务器读取参数正常,但我的图片没有上传。上传由 PHP 脚本处理,我可以确认正确的 HTTP Post 将导致图像被成功上传,因为我已经使用 HTML 表单测试了 PHP 脚本。我还可以在下面的代码中确认 photo != null。
HttpClient httpClient = new DefaultHttpClient();
HttpPost postRequest = new HttpPost("http://myurl.com/upload.php");
MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
if (photo != null) {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
photo.compress(CompressFormat.JPEG, 75, bos);
byte[] data = bos.toByteArray();
ByteArrayBody bab = new ByteArrayBody(data, "submission-1.jpg");
reqEntity.addPart("file", bab);
}
reqEntity.addPart("date", new StringBody(date));
reqEntity.addPart("lat", new StringBody(latitude));
reqEntity.addPart("long", new StringBody(longitude));
reqEntity.addPart("name", new StringBody(name));
reqEntity.addPart("email", new StringBody(email));
reqEntity.addPart("comments", new StringBody(comments));
postRequest.setEntity(reqEntity);
HttpResponse response = httpClient.execute(postRequest);
【问题讨论】:
-
检查 byte[] 数据是否有内容?
-
我刚刚验证它确实包含数据
-
@PeterWillsey:我面临同样的问题。但我没有得到 MultipartEntity ?
标签: android post httpclient android-camera