简体   繁体   中英

http multipart post request with json and an image with koush/ion library

I'm trying to post a multipart data consisting of image and json object parameter "status" from Android OS to Ruby rails server. I'm doing an android version of a completed iOS app so the server side works.

But I can't seem to get it to work on android with or without any libraries.

On the server side, this has to be received for it to work.

 >>>> params: {"status"=>{"body"=>"Make love, not Warcraft. "}, 
"photo"=>#<ActionDispatch::Http::UploadedFile:0x007f36a89e5738 
@tempfile=#<Tempfile:/tmp/RackMultipart20140414-29241-gy4qeo>, @original_filename="iUEb2W9cnxG6", 
@content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"photo\"; 
filename=\"iUEb2W9cnxG6\"\r\nContent-Type: image/jpeg\r\n">, "format"=>"json", 
"action"=>"create", "controller"=>"api/statuses", "version"=>"1"}

But when I do this, it doesn't work.

JsonParser jp = new JsonParser();
JsonObject postbody = (JsonObject)jp.parse("{\"status\": { \"body\": \""+ text.getText().toString() +"\", \"venue\": null}}");

System.out.println("Start uploading image using Ion Library");


            Ion.with(it, The.url("/1/statuses"))
            .setHeader("uuid", The.uuid(it).toString())
            .setHeader("Content-Type","application/json")
            .setMultipartParameter("status", postbody.toString())
            .setMultipartFile("image", "image/jpeg", selectedFile)
            .asJsonObject()
            .setCallback(new FutureCallback<JsonObject>() {
               @Override
                public void onCompleted(Exception e, JsonObject result) {
                   System.out.println("Posted result "+ e + " " + result+ The.url("/1/statuses"));
               }});
            }

        }

What I get from the server is basically parsing error.

I, [2014-04-14T17:21:01.411626 #29241]  INFO -- : Started POST "/1/statuses" for 64.179.216.50 at 2014-04-14 17:21:01 -0400
D, [2014-04-14T17:21:01.415493 #29241] DEBUG -- : Error occurred while parsing request parameters.
Contents:

------------------------------806fe35005b7480e81c10fc5c7ce33ef
Content-Disposition: form-data; name="status"

{"body":"i h8 u","venue":null}
------------------------------806fe35005b7480e81c10fc5c7ce33ef
Content-Disposition: form-data; name="photo"; filename="img.jpg"
Content-Type: image/jpeg

????E?ExifII*
?2?i???(SAMSUNGGT-I9500I9500UBUEMK12014:04:14 17:20:46HH?,??4"?'???0220?<?P?
d?l?
t?
|???    ?
??|?bH(???%??0100????8??(??? ?
                              <(?d2014:04:14 17:20:462014:04:14 17:20:46?d?d????d
?d?dASCIId?
           ?????????#??M?-?@??????????M?-?,??????????M?-???????????M?-???????????M?-????????????M?-????????????M?-???????????IJX??-д\?????X.p/???P?00?????/?BO?
                           ??
???????????????!?o??P???P??????
???
   ???0?@?A?0F???????????????@?
                               ?????????#??M?-?@??????????M?-?,??????????M?-???????????M?-???????????M?-????????????M?-????????????M?-???????????IJX??-д\?????X.p/???P?00?????/?BO?
                                               ??
???????????????!?o??P???P??????
???
   ???0?@?A?0F???????????????n)?p)?Ăf)?e)?g)?a)?h
A??i)???d)???u)???o)?O?x)??)???)???)???)?L??)???h?  (?A??c)??s)?
                                                                    ??)?#??)?(? pG??5?%?????? !??? ` !??@`` !????` !?`??`A?Y??? B???;???F?????????????????????????O?@p??
??M?@??(FP????*FhFP????jF(FO?????*FhFO?????jF(FO???|?*FhFO?????jF(FO???w?*FhFO???~?jF(FO???r?*FhFO???{?jF(FO???m?*FhFO???z?jF(FO???h?*FhFO???w?jF(FO???c?*FhFO??u?jF(FO??^?*FhFO??r?jF(FO??qH?X?O.

I have tried without the library and all kinds of stuff for the past 8 hours and researched for a long time. I still feel like I'm missing something so easy and obvious but I haven't done a lot of image post requests so far... Any solution with any library will be appreciated.

Edit: .setBodyParameter(....) was a typo and fixed to .setMultipartParameter(...) That was the code that resulted in this result. If someone could give me a piece of code that will do what I want to do, it'll be extremely appreciated. Thank you internet!

instead of setBodyParameter, use set setMultipartParameter. I'm not sure how you're invoking that, as that won't compile on my end.

What version of ion are you on? There was a fix to remove extraneous CRLF in multipart payloads a month ago:

https://github.com/koush/AndroidAsync/commit/efb6c018bd15060e44086d4f76c3d419d26065ba

I had the same problem;

instead of setting multipart file; ... .setMultipartFile("image", "image/jpeg", fileToUpload)

setting filebody solved my problem. ... .setFileBody(fileToUpload)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM