I'm developing an Android app and I'm working on photo uploading to my rails server.
If I upload ad image from browser, server gives me this (I'm using paperclip gem to upload):
COMMIT CODE
[...] , "immagine"=>#
,
@original_filename="24e14f4a46269f89ace12297b9ed4060aa3b44f5.jpeg">,
[...]
My question is: how can I use multipart to upload correctly? On android, working with json objects I used:
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
I've tried
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();
}
.. but in this case server receives well but doesn't "insert into".
Please look at the screens ... This screen is the post from browser ... all goes well
http://imageshack.us/photo/my-images/219/brozk.png/
This from android ... no "insert into" ... WHY??
http://imageshack.us/photo/my-images/13/andvv.png/
NOTE THAT if I send the same post without adding an image ... "INSERT INTO" works
JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)