network.download() is not downloading correctly on Android

I have been trying to use network.download to save a POST/GET based API call.

http://url/test

for example will return some txt:

123
123
123

However, on any Android device such as Notes 2, Galaxy, etc, download() seem to be saving the response into a weird Gzip or empty binary file.

On iOS and Simulator however, it works perfectly.

A work around I tried is to use network request, and manually save the event.response into a file.

Problem:

Now, trying to use download() to download binary files such as zip, mp3, is also not saving correctly on Android devices, but work son Simulator and iOS.

network.download("http://URL/data/bg/"..downloadFile, "GET", onBGListener, downloadFile, system.DocumentsDirectory)

Does anyone have any ideas? [import]uid: 88608 topic_id: 33801 reply_id: 333801[/import]

I have the exact same problem with display.loadRemoteImage() wich is based on network.download().

Any ideas somebody?

Thanks. [import]uid: 123089 topic_id: 33801 reply_id: 134363[/import]

The problem is your webserver is Gzip’ing the text file and Android’s implementation doesn’t unGzip by default. I had this very same problem and the advice was to set some headers but I never got it to work. I was on a time crunch, so I wrote a quick PHP script to do my output as a JSON structure and use network.request() to fetch the data instead. [import]uid: 199310 topic_id: 33801 reply_id: 134404[/import]

Thanks Rob, that seemed to be the problem. If anybody have yhe same issue, this is how i fixed my problem on Android, Galaxy S2:

local params = {} local headers = {} headers["Content-Type"] = "image/jpeg" headers["Accept-Encoding"] = "identity" params.headers = headers network.download(PIC\_URL,"GET",listener,params,DESTINATION\_FILE, system.TemporaryDirectory) [import]uid: 123089 topic_id: 33801 reply_id: 134484[/import]

I have the exact same problem with display.loadRemoteImage() wich is based on network.download().

Any ideas somebody?

Thanks. [import]uid: 123089 topic_id: 33801 reply_id: 134363[/import]

The problem is your webserver is Gzip’ing the text file and Android’s implementation doesn’t unGzip by default. I had this very same problem and the advice was to set some headers but I never got it to work. I was on a time crunch, so I wrote a quick PHP script to do my output as a JSON structure and use network.request() to fetch the data instead. [import]uid: 199310 topic_id: 33801 reply_id: 134404[/import]

Thanks Rob, that seemed to be the problem. If anybody have yhe same issue, this is how i fixed my problem on Android, Galaxy S2:

local params = {} local headers = {} headers["Content-Type"] = "image/jpeg" headers["Accept-Encoding"] = "identity" params.headers = headers network.download(PIC\_URL,"GET",listener,params,DESTINATION\_FILE, system.TemporaryDirectory) [import]uid: 123089 topic_id: 33801 reply_id: 134484[/import]

I’m guessing Razvan’s post above helps some of you but here is the code block (complete program for the moment) that works on the simulator but not on my Galaxy S2. Would appreciate any advice. As you can see, I’m trying to download audio (mp3) files for play and eventual storage. This particular URL is just a test. I am still a trial user…I wanted to practice a bit first before deciding to plunk down my money.

local selSound
local function networkListener( event )
if ( event.isError ) then
print ( “Network error - download failed” )
else
selSound = audio.loadSound(“1acv01_fra_01a.mp3”,system.DocumentsDirectory)
audio.play(selSound)
end
print ( "RESPONSE: " … event.response )
end
network.download(“http://www.gotfuturama.com/Multimedia/MP3Sounds/1acv01_fra_01a.mp3”, “GET”, networkListener, “1acv01_fra_01a.mp3”,system.DocumentsDirectory)
[import]uid: 196941 topic_id: 33801 reply_id: 140833[/import]

What is being printed out to the console log? (adb logcat)?

[import]uid: 199310 topic_id: 33801 reply_id: 140854[/import]

If, by console log, you mean the output to the terminal window, everything looks fine when I run it on the simulator as either an iPhone or Droid. It’s not until I build it and load it on my Android that it fails to work. If there is another log where I can look, please let me know. In the meanwhile, I will search for it.

On another note, I tried adding razvan’s recommended fix, changing the content-type to mpeg, which I believe is the appropriate type for mp3. Again, it works on the simulator fine but not on my Android.

BTW: I changed the code as shown below. In the simulator, AND on the android phone, I can now verify the file is received and it’s the correct size. Really, the only thing missing appears to be the actual audio, so maybe the problem is with the audio.load or audio.play portions. I did verify the volume is up on the phone.
local selSound
local socket = require “socket”

local function testNetworkConnection()
local netConn = socket.connect(‘www.google.com’, 80)
if netConn == nil then
return false
end
netConn:close()
return true
end

local myText = display.newText(" ", 160, 20, native.systemFont, 16)
myText:setTextColor(255, 255, 255)

local function networkListener( event )
if ( event.isError ) then
myText.text = myText.text … " but Download Failed"
else
local lfs = require “lfs”
local file_path = system.pathForFile( “1acv01_fra_01a.mp3”, system.DocumentsDirectory )
local size = lfs.attributes (file_path, “size”)
myText.text = myText.text … " Size:" … size
selSound = audio.loadSound(“1acv01_fra_01a.mp3”,system.DocumentsDirectory)
audio.play(selSound)
end
print ( "RESPONSE: " … event.response )
end

if testNetworkConnection then
local params = {}
local headers = {}
local theURL = “http://www.gotfuturama.com/Multimedia/MP3Sounds/1acv01_fra_01a.mp3
headers[“Content-Type”] = “audio/mpeg”
headers[“Accept-Encoding”] = “identity”
params.headers = headers
myText.text = "Online "
network.download(theURL, “GET”, networkListener, params, “1acv01_fra_01a.mp3”,system.DocumentsDirectory)
else
myText.text = “No Network”;
end [import]uid: 196941 topic_id: 33801 reply_id: 140950[/import]

Go google “installing adb”
or google “Android Debug Bridge”

These are google’s command line tools for working with android devices. You hook your device to your computer via USB, you can use the command line command:

adb logcat

and it will dump the device’s console log and you can look for errors there. You’re print statements and Corona errors will show up there.
[import]uid: 199310 topic_id: 33801 reply_id: 140981[/import]

Rob…I downloaded the ADB onto my windows machine, and then I captured the log while I ran my little program. I could not determine the error but DANG, there sure was a LOT OF LOG for a short period of capture.

Since I had downloaded the rest of the program, I took the opportunity to do a software update on my Android. I believed it would help my program run. It did not help at all. HOWEVER, I started thinking that maybe it was mp3 files in general that would not work, so I downloaded some WAV and MP3 versions of other sound clips. I tested the MP3 first…and it worked! So, perhaps there is something about the mp3 I was initially working with that would allow it to run on the simulator but no on my android.

I wish I understood the problem better because I hate having things like that going essentially unresolved. It is good to know that I can continue working on my project though. I appreciate your help very much and just wanted to update you and to say thanks.

Jim [import]uid: 196941 topic_id: 33801 reply_id: 141448[/import]

I’m guessing Razvan’s post above helps some of you but here is the code block (complete program for the moment) that works on the simulator but not on my Galaxy S2. Would appreciate any advice. As you can see, I’m trying to download audio (mp3) files for play and eventual storage. This particular URL is just a test. I am still a trial user…I wanted to practice a bit first before deciding to plunk down my money.

local selSound
local function networkListener( event )
if ( event.isError ) then
print ( “Network error - download failed” )
else
selSound = audio.loadSound(“1acv01_fra_01a.mp3”,system.DocumentsDirectory)
audio.play(selSound)
end
print ( "RESPONSE: " … event.response )
end
network.download(“http://www.gotfuturama.com/Multimedia/MP3Sounds/1acv01_fra_01a.mp3”, “GET”, networkListener, “1acv01_fra_01a.mp3”,system.DocumentsDirectory)
[import]uid: 196941 topic_id: 33801 reply_id: 140833[/import]

What is being printed out to the console log? (adb logcat)?

[import]uid: 199310 topic_id: 33801 reply_id: 140854[/import]

If, by console log, you mean the output to the terminal window, everything looks fine when I run it on the simulator as either an iPhone or Droid. It’s not until I build it and load it on my Android that it fails to work. If there is another log where I can look, please let me know. In the meanwhile, I will search for it.

On another note, I tried adding razvan’s recommended fix, changing the content-type to mpeg, which I believe is the appropriate type for mp3. Again, it works on the simulator fine but not on my Android.

BTW: I changed the code as shown below. In the simulator, AND on the android phone, I can now verify the file is received and it’s the correct size. Really, the only thing missing appears to be the actual audio, so maybe the problem is with the audio.load or audio.play portions. I did verify the volume is up on the phone.
local selSound
local socket = require “socket”

local function testNetworkConnection()
local netConn = socket.connect(‘www.google.com’, 80)
if netConn == nil then
return false
end
netConn:close()
return true
end

local myText = display.newText(" ", 160, 20, native.systemFont, 16)
myText:setTextColor(255, 255, 255)

local function networkListener( event )
if ( event.isError ) then
myText.text = myText.text … " but Download Failed"
else
local lfs = require “lfs”
local file_path = system.pathForFile( “1acv01_fra_01a.mp3”, system.DocumentsDirectory )
local size = lfs.attributes (file_path, “size”)
myText.text = myText.text … " Size:" … size
selSound = audio.loadSound(“1acv01_fra_01a.mp3”,system.DocumentsDirectory)
audio.play(selSound)
end
print ( "RESPONSE: " … event.response )
end

if testNetworkConnection then
local params = {}
local headers = {}
local theURL = “http://www.gotfuturama.com/Multimedia/MP3Sounds/1acv01_fra_01a.mp3
headers[“Content-Type”] = “audio/mpeg”
headers[“Accept-Encoding”] = “identity”
params.headers = headers
myText.text = "Online "
network.download(theURL, “GET”, networkListener, params, “1acv01_fra_01a.mp3”,system.DocumentsDirectory)
else
myText.text = “No Network”;
end [import]uid: 196941 topic_id: 33801 reply_id: 140950[/import]

Go google “installing adb”
or google “Android Debug Bridge”

These are google’s command line tools for working with android devices. You hook your device to your computer via USB, you can use the command line command:

adb logcat

and it will dump the device’s console log and you can look for errors there. You’re print statements and Corona errors will show up there.
[import]uid: 199310 topic_id: 33801 reply_id: 140981[/import]

Rob…I downloaded the ADB onto my windows machine, and then I captured the log while I ran my little program. I could not determine the error but DANG, there sure was a LOT OF LOG for a short period of capture.

Since I had downloaded the rest of the program, I took the opportunity to do a software update on my Android. I believed it would help my program run. It did not help at all. HOWEVER, I started thinking that maybe it was mp3 files in general that would not work, so I downloaded some WAV and MP3 versions of other sound clips. I tested the MP3 first…and it worked! So, perhaps there is something about the mp3 I was initially working with that would allow it to run on the simulator but no on my android.

I wish I understood the problem better because I hate having things like that going essentially unresolved. It is good to know that I can continue working on my project though. I appreciate your help very much and just wanted to update you and to say thanks.

Jim [import]uid: 196941 topic_id: 33801 reply_id: 141448[/import]