JSON Error on Droid, Fine on IOS.

Hi,

I’ve run into a strange error with my app. It runs fine in the simulator, and on an iPhone 4s. I tried it out on a real android device for the first time today and it crashed. I’m almost sure this has to do with the placement of my JSON file within the file structure.

What I’m trying to do is write a JSON file pulled in from the web to the temp directory, and then turn it into a lua table. Here is the code:

local function networkListener( event )  
print ("in network listener")  
 if ( event.isError ) then  
 print ( "Network error - download failed" )  
 else  
 base = system.TemporaryDirectory   
 filename = "hello.json"  
  
 -- create a file path for corona i/o  
 local path = system.pathForFile( filename, base )  
  
 print ("The path is: " .. filename)  
 -- will hold contents of file  
 local contents  
 -- io.open opens a file at path. returns nil if no file found  
 local file = io.open( path, "r" )  
 if file then  
 -- read all contents of file into a string  
 contents = file:read( "\*a" )  
 io.close( file ) -- close the file after using it  
  
 end  
print("before json decode")  
local t = json.decode(contents)  
print("after json decode")  

Here is my output from the phone:

01-29 16:20:43.372 18391 18401 I Corona : in network listener
01-29 16:20:43.372 18391 18401 I Corona : The path is: /data/data/com.mypp/cache/hello.json
01-29 16:20:43.372 18391 18401 I Corona : before json decode
01-29 16:20:43.372 18391 18401 I Corona : Lua Runtime Error: lua_pcall failed with status: 2, error message is: Invalid input: '?

I’m not sure, but I think maybe that data/data/ directory is suspect.
Any ideas? [import]uid: 112807 topic_id: 21111 reply_id: 321111[/import]

Here is the droid section of my build.settings file.

androidPermissions =
{
“android.permission.ACCESS_FINE_LOCATION”
}
[import]uid: 112807 topic_id: 21111 reply_id: 83469[/import]

Is the json variable declared before you tried to run its method? Is it possible that you were trying to decode an empty string resulting in an error.

Also, your permission need to allow you to write to storage. I think it’s something like android.permissions.WRITE_EXTERNAL_STORAGE or something like that.

Open your apk file and read the permissions in your manifest. Also, can you provide a log for the Android debugging environment and see what it says? [import]uid: 49520 topic_id: 21111 reply_id: 83477[/import]

I believe you are correct regarding the android permissions, that would explain why it will run on an iPhone with no problems but not on an Android. I’m going to look into that tonight and see if I can get a build out to my friend to test tomorrow.

The short log I posted was from LogCat. I would expect if my permissions were wrong I would have seen exceptions popping up all over the place indicating that, but I don’t recall seeing any. I’ll parse the logs again when I get home tonight along with adjusting the android permissions. Thanks for the help!

[import]uid: 112807 topic_id: 21111 reply_id: 83564[/import]