File download fails on Android

In the Corona Simulator and on iPhone, the following code prints the contents if the remote file perfectly.

However, on Android, only garble is printed, and the file length is said to be about half of the actual length.

What is wrong?

[lua]function io.load(name)

local path = system.pathForFile(name, system.DocumentsDirectory)
local file = io.open(path, “r”)
if file then
local data = file:read("*a")
io.close(file)
return data
end

end

function main()

local file = “index.html”

local function listener(event)

for key, value in pairs(event) do
print(key, value)
end

local load = io.load(file)

print(“length”, string.len(load))

local buffer = “”
for i=1, string.len(load) do
local char = string.sub(load, i, i)
buffer = buffer … char
if char == “\n” or string.len(buffer) >= 80 then
print(buffer)
buffer = “”
end
end

end

local url = “http://www.example.com/
network.download(url, “GET”, listener, file, system.DocumentsDirectory)

end

main()[/lua]
[import]uid: 73434 topic_id: 35581 reply_id: 335581[/import]

Your server is gzip’ing the content and Android doesn’t handle unzipping it correctly.

See this post:

http://developer.coronalabs.com/forum/2012/12/31/cant-read-xml-file

[import]uid: 199310 topic_id: 35581 reply_id: 141433[/import]

@Rob: You’re right! But the server sends gzip’ed data even if I specify Accept-Encoding. What now?

[lua]function io.load(name)
local path = system.pathForFile(name, system.DocumentsDirectory)
local file = io.open(path, “r”)
if file then
local data = file:read("*a")
io.close(file)
return data
end
end

function string.dump(buf)
local width = 16
for byte=1, #buf, width do
local chunk = buf:sub(byte, byte+width-1)
local line = string.format(’%08X ‘,byte-1)
chunk:gsub(’.’, function © line = line … string.format(’%02X ‘,string.byte©) end)
line = line … string.rep(’ ‘,3*(width-#chunk))
line = line … ’ ’ … chunk:gsub(’%c’,’.’) … “\n”
print(line)
end
end

function main()
local file = “index.html”

local function listener(event)
local load = io.load(file)
string.dump(load)
end

local url = “http://www.example.com/
local headers = {
[“Accept-Encoding”] = “” – tried “identity” too
}
network.download(url, “GET”, listener, headers, file, system.DocumentsDirectory)
end

main()[/lua] [import]uid: 73434 topic_id: 35581 reply_id: 141439[/import]

I think these are the headers:

ACCEPT: */*
ACCEPT-LANGUAGE: en-us
ACCEPT-ENCODING: gzip, deflate

See http://en.wikipedia.org/wiki/HTTP_compression for more info.
[import]uid: 199310 topic_id: 35581 reply_id: 141444[/import]

I had forgot to enclose the headers variable in a table. It works now, even with deflate.

[lua]local headers = {
[“Accept-Encoding”] = “deflate”,
}
network.download(url, “GET”, listener, {headers=headers}, file, system.DocumentsDirectory)[/lua]
[import]uid: 73434 topic_id: 35581 reply_id: 141449[/import]

Your server is gzip’ing the content and Android doesn’t handle unzipping it correctly.

See this post:

http://developer.coronalabs.com/forum/2012/12/31/cant-read-xml-file

[import]uid: 199310 topic_id: 35581 reply_id: 141433[/import]

@Rob: You’re right! But the server sends gzip’ed data even if I specify Accept-Encoding. What now?

[lua]function io.load(name)
local path = system.pathForFile(name, system.DocumentsDirectory)
local file = io.open(path, “r”)
if file then
local data = file:read("*a")
io.close(file)
return data
end
end

function string.dump(buf)
local width = 16
for byte=1, #buf, width do
local chunk = buf:sub(byte, byte+width-1)
local line = string.format(’%08X ‘,byte-1)
chunk:gsub(’.’, function © line = line … string.format(’%02X ‘,string.byte©) end)
line = line … string.rep(’ ‘,3*(width-#chunk))
line = line … ’ ’ … chunk:gsub(’%c’,’.’) … “\n”
print(line)
end
end

function main()
local file = “index.html”

local function listener(event)
local load = io.load(file)
string.dump(load)
end

local url = “http://www.example.com/
local headers = {
[“Accept-Encoding”] = “” – tried “identity” too
}
network.download(url, “GET”, listener, headers, file, system.DocumentsDirectory)
end

main()[/lua] [import]uid: 73434 topic_id: 35581 reply_id: 141439[/import]

I think these are the headers:

ACCEPT: */*
ACCEPT-LANGUAGE: en-us
ACCEPT-ENCODING: gzip, deflate

See http://en.wikipedia.org/wiki/HTTP_compression for more info.
[import]uid: 199310 topic_id: 35581 reply_id: 141444[/import]

I had forgot to enclose the headers variable in a table. It works now, even with deflate.

[lua]local headers = {
[“Accept-Encoding”] = “deflate”,
}
network.download(url, “GET”, listener, {headers=headers}, file, system.DocumentsDirectory)[/lua]
[import]uid: 73434 topic_id: 35581 reply_id: 141449[/import]