2011-07-11 27 views
3

我正在開發一個Android應用程序,並且正在將照片上傳到我的rails服務器上。如何 - 從android上載圖像到rails服務器 - multipart

如果我從瀏覽器上傳廣告圖片,服務器給我這個(我使用回形針寶石上傳):

提交代碼

[...] , "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">, 
[...] 

我的問題是:我怎麼能使用multipart正確上傳?在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"); 

UPDATE

我已經試過

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/ enter image description here

這在Android ...沒有「插入「......爲什麼?

http://imageshack.us/photo/my-images/13/andvv.png/ enter image description here

注意如果我發送相同的職位,而不添加圖片... 「INSERT INTO」 的作品

+0

我注意到你沒有發送android設備中的authentication_token。您的模型是否需要用戶進行身份驗證? – Howler

+0

你設法弄清楚了嗎? –

回答

-1

這裏是一個很好的文章link。這有助於我從android設備上傳圖片到服務器。我希望它也會對你有所幫助。

+0

我想用multipart做到這一點:) –

+0

嘿,夥計們,這裏其實是一篇很不錯的文章。我能夠在後衛機器上找到它。 http://web.archive.org/web/20130627091411/http://blog.sptechnolab.com/2011/03/09/android/android-upload-image-to-server/ – Austio

相關問題