Network Reachability

Would anyone be able to help me with this? My code works fine on iOS, but doesn't work when I test on Android devices.

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
function displayFeed(feedName, feedURL)
 
print("entering displayFeed")
 
    local function processRSSFeed(file, path)
        print("Parsing the feed")
        --native.showAlert("processRSSFeed", "before rss.feed", {"OK"})
        local story = {}
        stories = rss.feed(file, path)
        if stories == nil then 
            local alert = native.showAlert( "We're Sorry.", "Looks like there is a problem connecting to the internet. Please make sure you are connected to the internet.", 
                                        { "OK" }, onAlertComplete )
        else
            --native.showAlert("processRSSFeed", "after rss.feed", {"OK"})
            --print_r(stories)
            print("Num stories: " .. #stories)
            print("Got ", #stories, " stories, now show the tableView")
            showTableView()
        end
    end
    
    local function onAlertComplete( event )
        native.setActivityIndicator( false )
                director:changeScene("Region_Africa")
                return true             
    end
    
    local networkListener = function( event )
        utility.print_r(event)
        if ( event.isError ) then
            print("Using Cached Copy")
            processRSSFeed(feedName, systemDocumentsDirectory)
        else
            print("calling processRSSFeed because the feed is avaialble")
            processRSSFeed(feedName, system.DocumentsDirectory)
        end
        return true
    end
    
    function MyNetworkReachabilityListener(event)
        --native.showAlert("network", "is reachable", {"OK"})
        print( "isReachable", event.isReachable )
        network.setStatusListener( "www.awanainternational.org", nil )
        if event.isReachable then
            -- download the latest file
            native.setActivityIndicator( true )
            --native.showAlert("network", "downloading", {"OK"})
            network.download(feedURL, "GET", networkListener, feedName, system.DocumentsDirectory)
        else
            native.setActivityIndicator( true )
            print("not reachable")
            --native.showAlert("network", "using cached copy", {"OK"})
            -- look for an existing copy
            local path = system.pathForFile(feedName, system.DocumentsDirectory)
            local fh, errStr = io.open( path, "r" )
            if fh then
                io.close(fh)
                print("calling processRSSfeed because the network isn't reachable")
                processRSSFeed(feedName, system.DocumentsDirectory)
            else
                local alert = native.showAlert( "We're Sorry.", "Looks like there is a problem connecting to the internet. Please make sure you are connected to the internet.", 
                                            { "OK" }, onAlertComplete )
            end
        end
        return true
    end
   
    local platform = system.getInfo("platformName")
    if platform == "Android" then
        -- Android doesn't support network detection at this point, so skip it.
        native.setActivityIndicator( true )
        network.download(feedURL, "GET", networkListener, feedName, system.DocumentsDirectory)
    else
        if network.canDetectNetworkStatusChanges then
            network.setStatusListener( "www.awanainternational.org", MyNetworkReachabilityListener )
        else
            native.showAlert("network", "not supported", {"OK"})
            print("network reachability not supported on this platform")
        end
    end
end

I think network.canDetectNetworkStatusChanges is always false on Android. I don't think we have a solution for this yet on Android.

views:2220 update:2012/2/9 11:37:26
corona forums © 2003-2011