network.request not working on device but fine in simulator

I am using google's Places API to retrieve a list of nearby places and display them in a list. It works fine in the simulator and does not work in the device (HTC Thunderbolt). It is like the listener never fires and the app will eventualy crash without displaying data.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
...
--listener for Places API request
local function networkListener( event )
 
        if ( event.isError ) then
--              myText.text = "Network error!"
        else 
--              myText.text = "See Corona Terminal for response"
                
                --decode json table
       local response = event.response
       response = json.decode(response )
       local status = response.status
       print(status)  
       local rTest = display.newText(status, 0, 0, native.systemFontBold,22)
 
       if status == "OK" then
       local testValue = response.results[1].geometry.location.lat
       local testValue2 = response.results[1].geometry.location.lng
 
       Gdata= response.results
       local i = 1
       while Gdata[i] do
          Gdata[i]["title"]=Gdata[i]["name"]
          Gdata[i]["category"]=Gdata[i]["types"][1]
                        print ( "title: " .. Gdata[i]["title"] )
                print ( "category: " .. Gdata[i]["category"] )
       i=i+1
       end--do
                print ( "RESPONSE:LAT: " .. testValue )
                print ( "RESPONSE:LNG: " .. testValue2 )
        ReFresh()
        end
       end
end
 
--call to request a list of places
local function RequestPlaces(lLat,lLng)
      local lRadius = 500 --update from user defined range
      local lTypes = "bar|convenience_store|liquor_store"--update from user defined filter
      local lSensor= "true"
      local lKey = "Sorry_not_sharing_my_key"
      network.request( "https://maps.googleapis.com/maps/api/place/search/json?location="..lLat..","..lLng.."&radius=".. lRadius.."&types="..lTypes.."&sensor="..lSensor.."&key="..lKey, "GET", networkListener )
        print("requested")
return true
end
 
x = RequestPlaces(currentLatitude,currentLongitude)
print("DONE")

The listener does fire.... a response can be captured from other URL's .... this URL works in the simulator and not on the device...

I am at a loss as to why this does not work. Has anyone else successfully made a request to Google's places api using Corona?
(Maybe there is something in the response that causes it to fail on the device? Illegal characters or some such?)

Update:
hmm... path works when pasted into the browser on the device...

When the request is sent by Corona does it send it in the same case as what the string is formated? (If it converts CAPS to lowercase it could be causing my api key to fail)

....(I'll continue to mutter to myself and pull my hair out)...

... still nothing.... I tried saving the result to a file(used the asynch http image download sample app) and it again works in the simulator and not on the device.... looking at the traffic for the API (tracked by my key) it never recieves the request when it is running on the device. So.... the request is never sent from the device or the request is somehow malformed when it is sent from the device. (It would have to be something that would only effect some url's.)

....(very little hair left and have now been fitted for my strait jacket)....

Hi.

I have the same problem. I user network.request work fine on simulator but not in my phone (iphone 4).

I dont understand where is my error.

I you know how solve this problem please post the response..

Still scratching my head on this (ok... I gave up on my app that uses this for now.) I have tried the most recent builds and still no joy. Have entered a support request.... no usefull response... :( ..... would be nice if someone from Ansca would comment ( I would like a meaningfull comment not just a quick quip.)

Did you verify your internet settings are configured correctly on your device? Also, we don't support proxy servers.

We have 3 different automated tests that test our async network stuff and they have been passing. We haven't changed the code in a long while either, so I'm not sure why things aren't working for you.

Below is what one of our tests looks like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
require('CoronaTest')
 
local goodURL = "http://developer.anscamobile.com/sites/default/files/test.txt"
local badURL = "666.66.66.666"
local secureURL = "https://encrypted.google.com"
local postURL = "http://www.anscamobile.com/formecho.php"
 
local url = goodURL
 
local responseData_known = "Test file contents."
local postResponse_known = "red small."
 
local postData = "color=red&size=small"
local params = {}
params.body = postData
params.headers = nil
 
local NUMBER_OF_TESTS = 7
plan(NUMBER_OF_TESTS)
 
local function networkListener( event )
        if url == badURL then
                is( event.isError, true, "Checking for request error - bad URL" )
                -- Make post data request
                diag("making POST request")
                callPostURL()
                -- EndTest()
        else
                isnt( event.isError, true, "Checking for request error - good URL " )
                diag("parsing response")
                is(event.response, responseData_known, "Checking downloaded text.")
                callBadURL()
        end
end
 
local function postDataListener( event )
        if ( event.isError ) then
                print( "Error with POST")
        else
                        is(event.response, postResponse_known, "Checking downloaded text.")
                
                -- print ( "Post RESPONSE: " .. event.response )
        end
                isnt( event.isError, true, "Checking for POST request error " ) 
                callSecureURL()
end
 
local function secureNetworkListener( event )
        isnt( event.isError, true, "Checking for SSL request error " )  
        if ( event.isError ) then
                diag("Error with SSL response/request")
        else
                -- print ( "RESPONSE: " .. event.response )
                ok(event.response ~= nil, "Checking for non-nil SSL response.")
        end
        EndTest()
end
 
function callBadURL(  )
        url = badURL
        diag("Calling bad URL")
        network.request(badURL, "GET", networkListener)
end
 
function callPostURL ()
        diag("Calling POST URL")
        url = postURL
        network.request( url, "POST", postDataListener, params) 
end
 
function callSecureURL ()
        diag("Calling SSL URL")
        url = secureURL
        -- Access Google over SSL:
        network.request( url , "GET", secureNetworkListener )
        
end
 
function EndTest()
        done_testing(NUMBER_OF_TESTS)
        os.exit()
end
 
network.request(goodURL, "GET", networkListener)

I solve the problem.

Corona work fine, the error is in the webservice or the web app.

For any reason some responses from the server (i dont know why) generate a error for example y request 5 valuse from my web service but the first value have something (maybe wrong charracters or something) then the first value generate a error then the other 4 values dont load. The app work on simulator but not in the phone. Then i trim the first value and then work fine in both devices.

My recomendation test the app with a text file /yourdomain/test.txt try to load this and you see the app work fine in mobile and simulator.

From my privous post:

(Maybe there is something in the response that causes it to fail on the device? Illegal characters or some such?)

Update:
hmm... path works when pasted into the browser on the device...

When the request is sent by Corona does it send it in the same case as what the string is formated? (If it converts CAPS to lowercase it could be causing my api key to fail)

My internet settings are configured correctly.(it does work with other URL's)
I am NOT using a proxy server.
The url is valid (works in simulator and when posted into any browser, on the device or otherwise)

The google places API is NOT recieving the request from my Corona app when built for the device. (at least not tracked to my API key)

This leads me to believe that something is amiss with the way the request is sent from a Corona app when built for an android device(no idea if this is an issue with iOS).

Looking at the test... it AGAIN makes me wonder if the request on the device converts the URL to lower case before sending it. (the API key in my network request IS case sensitive).

Example:
local goodURL = "http://developer.anscamobile.com/sites/default/files/TEST.txt"

Would this ask for a file named "TEST.txt" or "test.txt"? (yes, I know in that situation it should not matter, most situations it would not matter, my situation, it matters a lot.)

I checked your sample code on both the Windows and Mac Simulator and an Android device (Nexus One) and found the listener was called every time. I had to enable your "Network Error!" print statement to see that it responded with an error in both the Mac Simulator and Android device. The Windows Simulator didn't response with an error.

The problem I found was because of this line:
local lTypes = "bar|Cconvenience_store|liquor_store"--update from user defined filter

The "|" characters were causing the error and need to be escaped. Here is my change:
local lTypes = "bar%7Cconvenience_store%7Cliquor_store"--update from user defined filter

With the above change, both simulators and Android send the URL with all the parameters. I didn't check it with google maps but I did verify it by sending it to a postbin.org URL. That showed the URL and all the parameters were correctly sent.

You need to be careful when you include characters in a URL because there are number of them that are special. You can Google URL Character Encoding to find out more.

Update:
Here is the response I saw in the postbin.org page after running the app on the Nexus One (I created fake lat/long values):

1
2
3
4
5
6
#1qrcrp @ 21:26 Sep 12 2011 -- 153.7.111.236 ?location=123.175,999.876&radius=500&types=bar%7Cconvenience_store%7Cliquor_store&sensor=true&key=Sorry_not_sharing_my_keyContent
key     Sorry_not_sharing_my_key
location        123.175,999.876
radius  500
sensor  true
types   bar|convenience_store|liquor_store

Thank you very much.... that did it....

I never EVER would have thought to escape the pipe. |-)

views:2998 update:2011/10/7 17:24:19
corona forums © 2003-2011