【发布时间】:2011-07-11 14:34:10
【问题描述】:
我正在开发一个 Android 应用,并且正在将照片上传到我的 rails 服务器。
如果我从浏览器上传广告图片,服务器会给我这个(我正在使用回形针 gem 上传):
提交代码
[...] , "immagine"=>#
<ActionDispatch::Http::UploadedFile:0x103026dc8 @headers="Content-Disposition: form-data; name=\"segnalazione[immagine]\";
filename=\"24e14f4a46269f89ace12297b9ed4060aa3b44f5.jpeg\"\r\nContent-Type: image/jpeg\r\n", @content_type="image/jpeg", @tempfile=#<File:/var/folders/Ht/HtQoVYewE-i8RHpa2OY38U+++TI/-Tmp-/RackMultipart20110711-31693-p0wpzx-0>,
@original_filename="24e14f4a46269f89ace12297b9ed4060aa3b44f5.jpeg">,
[...]
我的问题是:如何正确使用分段上传?在 android 上,使用我使用的 json 对象:
HttpPost post = new HttpPost("http://10.0.2.2:3000/segnalaziones?format=json");
JSONObject eventObj = new JSONObject();
JSONObject holder = new JSONObject();
try {
// Genero l'oggetto JSON
eventObj.put("categoria1", c1);
eventObj.put("categoria2", c2);
eventObj.put("categoria3", c3);
eventObj.put("descrizione", descr);
eventObj.put("dove", coord);
eventObj.put("mood", mood);
eventObj.put("via", via);
//eventObj.put("android", android);
holder.put("commit", "Spedisci");
holder.put("segnalazione", eventObj);
Log.e("Event JSON", "Event JSON = "+ holder.toString());
StringEntity richiesta = new StringEntity(holder.toString());
post.setEntity(richiesta);
post.setHeader("Content-Type","application/json");
更新
我试过了
HttpPost post = new HttpPost("http://10.0.2.2:3000/segnalaziones");
MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
multipartEntity.addPart("commit", new StringBody("Spedisci"));
multipartEntity.addPart("segnalazione[immagine]", new FileBody(photo));
post.setEntity(multipartEntity);
HttpResponse response = client.execute(post);
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
resEntity.consumeContent();
}
.. 但在这种情况下,服务器接收良好但没有“插入”。
请看屏幕... 此屏幕是来自浏览器的帖子...一切顺利
http://imageshack.us/photo/my-images/219/brozk.png/
这来自 android...没有“插入”...为什么?
http://imageshack.us/photo/my-images/13/andvv.png/
注意如果我发送相同的帖子而不添加图片...“INSERT INTO”有效
【问题讨论】:
-
我注意到您没有在 android 设备中发送 authentication_token。您的模型是否需要对用户进行身份验证?
-
你弄明白了吗?
标签: android ruby-on-rails multipart