Async Http Socket

I wanted to download a list of images from the web, using network.loadRemoteImage, network.download, & network.request work perfectly well on iOS & Simulator, but not for Android.

These are the sample images that not working well on Android(Nexus 5 & Samsung Galaxy Duo):

http://www.thestar.com.my/~/media/Images/TSOL/Photos-Upload/Entertainment/2014/04/Annex%20%20Rooney%20Mickey%20Love%20Finds%20Andy%20Hardy02.ashx?crop=1&w=460&h=345&

http://www.thestar.com.my/~/media/Images/TSOL/Photos-Upload/From%20DCX/2014/04/05/doc6epsigalca11fnoshl6b.ashx?crop=1&w=460&h=345&

http://www.thestar.com.my/~/media/Images/TSOL/Photos-Gallery/central/2014/04/07/metd_az_0704_pg1_guard%203.ashx?crop=1

http://www.thestar.com.my/~/media/Images/TSOL/Photos-Gallery/features/2014/04/07/str2_cb_0704_p90a%20pls%20use%20just%201%20pixpdf.ashx?crop=1&w=460&h=345&

http://www.thestar.com.my/~/media/Images/TSOL/Photos-Gallery/central/2014/04/07/metd_az_0704_pg2_pix%20for%20sidebar.ashx?crop=1&w=460&h=345&

These images can load perfectly well on any browser, iOS device & simulator (using network method above), but for Android, it shows status 400.

Using Curl, some returns “document moved”, with new url

This is the code i used:

local function networkImageListener(event)         -- do something end display.loadRemoteImage(url, "GET", networkImageListener, fileName, system.CachesDirectory)  

here comes the interesting part:

After all the network method above failed, I try to implement using socket.http, which successfully downloaded ALL images, with only 1 major drawback – not async.

I searched the WHOLE internet, found a little old implementation to make socket.http call become async, but not working, maybe not compatible anymore. Some with missing link (http://developer.coronalabs.com/code/cross-platform-asynchronous-http-request)

So, any potential solution I can have?

Which version of CoronaSDK are you using?

I am using latest daily build, 2014.2248. Previously I was using 2014.2237, having the same problem.

A status of 400 from the server indicates the request being sent is invalid.  This could be because the client (the Android device in this case) is sending a bad request or because it’s not playing by whatever rules involving things like cookies, GETs vs POSTs, etc the server has decided to enforce.  You could ask about a REST API for the server you are contacting as this is by far the most reliable way to get data from a webserver in an app.

The issue you see might be related to issues we’ve seen on some Android devices where they don’t implement redirects correctly (which are fixed in the latest Public Release).  This thread is probably worth reading:

http://forums.coronalabs.com/topic/43864-network-request-works-in-simulator-and-nexus-but-not-on-samsung-or-htc/

We worked around the worst of the Android client specific issues in our network code but it’s possible the server you are trying to contact is very fussy about the exact format of the requests it receives.

One solution to this is to use the network.request() parameter  handleRedirects and manage the HTTP conversation manually and work around whatever issues are causing the problem (this helps a lot when the issue is caused by cookies):

http://docs.coronalabs.com/api/library/network/request.html#params-optional

display.loadRemoteImage() is intended to be used when creating image objects, if you just want to download the data to a file then network.download() would be a more appropriate starting point:

http://docs.coronalabs.com/api/library/network/download.html

If after investigating with the above in mind, you remain convinced that this is a bug in the Corona SDK, please send a complete app that demonstrates the issue along with a summary of what you’ve tried to: support@coronalabs.com

Hi Perry,

Thanks for your reply. The handleRedirects method is working. I got the Location of redirect from event.responseHeaders.Location, and realize that there are a lot spaces. After did a gsub to replace with %20, I made a new network.request and it works!

Which version of CoronaSDK are you using?

I am using latest daily build, 2014.2248. Previously I was using 2014.2237, having the same problem.

A status of 400 from the server indicates the request being sent is invalid.  This could be because the client (the Android device in this case) is sending a bad request or because it’s not playing by whatever rules involving things like cookies, GETs vs POSTs, etc the server has decided to enforce.  You could ask about a REST API for the server you are contacting as this is by far the most reliable way to get data from a webserver in an app.

The issue you see might be related to issues we’ve seen on some Android devices where they don’t implement redirects correctly (which are fixed in the latest Public Release).  This thread is probably worth reading:

http://forums.coronalabs.com/topic/43864-network-request-works-in-simulator-and-nexus-but-not-on-samsung-or-htc/

We worked around the worst of the Android client specific issues in our network code but it’s possible the server you are trying to contact is very fussy about the exact format of the requests it receives.

One solution to this is to use the network.request() parameter  handleRedirects and manage the HTTP conversation manually and work around whatever issues are causing the problem (this helps a lot when the issue is caused by cookies):

http://docs.coronalabs.com/api/library/network/request.html#params-optional

display.loadRemoteImage() is intended to be used when creating image objects, if you just want to download the data to a file then network.download() would be a more appropriate starting point:

http://docs.coronalabs.com/api/library/network/download.html

If after investigating with the above in mind, you remain convinced that this is a bug in the Corona SDK, please send a complete app that demonstrates the issue along with a summary of what you’ve tried to: support@coronalabs.com

Hi Perry,

Thanks for your reply. The handleRedirects method is working. I got the Location of redirect from event.responseHeaders.Location, and realize that there are a lot spaces. After did a gsub to replace with %20, I made a new network.request and it works!