MultipartRequest on iOS

113 views
Skip to first unread message

BenW

unread,
Oct 25, 2013, 11:04:58 AM10/25/13
to codenameone...@googlegroups.com
I'm having trouble sending a MultipartRequest in iOS that contains images. If the post contains only text arguments, the request is received on my server with no problems. This works flawlessly when built for Android, but it seems that the request.addData() function might be causing trouble on my iPhone.

Sample code:
...
MultipartRequest request = new MultipartRequest();
request.setURL( ServerURL );
request.setPost( true );
request.addArgumentNoEncoding( "test", "blah" );
Image image = Image.createImage( FileSystemStorage.getInstance().openInputStream( pathToImageFile ) );
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.getImageIO().save( image, baos, FORMAT_PNG, 1.0f );
request.addData( arg, baos.toByteArray(), "image/png" );
request.setFilename( arg, filename );

NetworkManager.getInstance().addToQueueAndWait( request );
...

Just to illustrate what I'm trying to do. Submission hangs for a long time and then seems satisfied, but _POST contains no information on the server side. Again, this works perfectly on an Android device and does work in iOS if the request contains String args, but no images (binary data). Is there anything that I'm missing out? Any iOS quirks that might be presenting themselves?

Thanks

Shai Almog

unread,
Oct 25, 2013, 12:41:54 PM10/25/13
to codenameone...@googlegroups.com, wal...@samsix.com
Use the pathToImage when adding the entry. You are creating a rather large allocation that could translate to a huge allocation in RAM.
addData(String name, String filePath, String mimeType)


BenW

unread,
Oct 28, 2013, 3:56:08 PM10/28/13
to codenameone...@googlegroups.com, wal...@samsix.com
Thank you very much for your response Shai.

I've made the change you suggested and I can now see the request being successfully submitted to the server side. I've also resized the image files as I don't need them to be at full resolution anyway and would rather have the request go through as quickly as possible. The problem I'm having now is that if I add multiple images to the MultipartRequest (say 2 images), I am receiving two copies of the same image with different filenames on the server side. The image received (multiple copies) is always the last one added to the MultipartRequest. Again, this works perfectly with the same code built for Android.

Any help you could provide would be greatly appreciated.

Thanks,

Ben

Shai Almog

unread,
Oct 29, 2013, 2:07:01 AM10/29/13
to codenameone...@googlegroups.com, wal...@samsix.com
I'm unaware of such an issue on our side. We use the multipart request quite a lot.

BenW

unread,
Oct 30, 2013, 10:28:31 AM10/30/13
to codenameone...@googlegroups.com, wal...@samsix.com

Hello Shai,

I just wanted to get back to you and let you know that I figured out the problem I was having. Apparently on iOS, the capturePhoto function writes to the same (temporary) file every time, whereas on Android, it writes to a new file. When I got the request to go through (thanks to your suggested change), I was passing the same path multiple times when building the MultipartRequest, which would of course point to the most recently captured image. Now, instead of just grabbing the path from evt.getSource() after the capture, I'm copying to a new file and everything is working well.

Thank you very much for your help on this,

Ben
Reply all
Reply to author
Forward
0 new messages