2

Hello i am new to phonegap and jquery mobile and have a problem when trying to run my application via eclipse emulator and also on my htc phone.

My code works perfectly when i run the application in firefox. The HTML calls a PHP script that runs on tomcat, the php script accesses the mysql and passes back jsonp data.

Any help would be much appreciated. I have read other posts and have had not lock with my code.

I have updated the android manifest with all the correct permissions as below:

<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.BROADCAST_STICKY" />

I have my html file below:

    <!DOCTYPE HTML>
<html>
<head>

<title>Search by area</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="stylesheet" href="jquery.mobile.structure-1.2.0.min.css" />
<link rel="stylesheet" href="jquery.mobile-1.2.0.min.css" />
<script type="text/javascript" charset="utf-8" src="cordova-2.2.0.js"></script>
<script type="text/javascript" src="jquery-1.8.3.min.js"></script>
<script type="text/javascript" src="jquery.mobile-1.2.0.min.js"> </script>




</head>
<body> 

    <div id="areas" data-role="page" data-add-back-btn="true">
    <script type="text/javascript">


     // alert dismissed
     function alertMessageDimissed()
     {
        // do something 
     }

     $("#areas").on('pageshow', function() {
                // get the areas from the database 
        $.ajax({
            type:'GET',
            url:'http://localhost/getData.php',
            dataType:'jsonp',
            jsonp: 'jsoncallback',
            timeout: 5000,
            success: function(data, status)
            { 
              $.each(data, function(i,item){
               var addToSession = 'sessionStorage.areaID=' + item.area_id;
                $('#list').append("<li><a href='../www/choose_restaurant.html' onclick=" + addToSession + " data-transition='slidedown'>" + item.area_name + 
                "<span class='ui-li-count'>" + item.deal_count + "</span>" +
                "</a></li>");
                $("#list").listview("refresh");


                });


            },
             error: function(data)
             {
                  // there was no connection to the internet 
                  navigator.notification.alert(
                    'No intenet connection', // message
                     alertMessageDimissed, // callback
                     'Information - Error', // title
                     'Done'                 // buttonName
                    );  

            }
        });
        return false;       
    });
    </script>

        <div data-role="header">

            <h1> Bucuresti deals</h1>

        </div>

        <div data-role="content">

            <div class="choice_list">

                <h1> In which area do you want to eat? </h1>


                <ul id="list" data-role="listview" data-inset="true" data-filter="true" >
                </ul>
            </div>
        </div>
    </div>

</body>
</html>

My working php script is below:

<?php
header('Content-type: application/json');

$server = "127.0.0.1:3306";
$username = "root";
$password = "";
$database = "deals";

$con = mysql_connect($server, $username, $password) or die ("Could not connect: " . mysql_error());
mysql_select_db($database, $con);


$sql = "SELECT a.area_id, a.name AS area_name, a.sector AS area_sector,
    (SELECT Count(*) FROM deals.main_deals b WHERE b.areaID = a.area_id) as deal_count
     FROM deals.areas a";

           $result = mysql_query($sql) or die ("Query error: " . mysql_error());

$records = array();

while($row = mysql_fetch_assoc($result)) {
    $records[] = $row;
}

mysql_close($con);

echo $_GET['jsoncallback'].'(' . json_encode($records) . ');';
?>
Gajotres
  • 57,309
  • 16
  • 102
  • 130
Ethan Richardson
  • 461
  • 2
  • 10
  • 28

1 Answers1

1

You can't use url:'http://localhost/getData.php', while testing on android phone. Remember you are not any more on your local machine. Mobile phone has different IP address then your server, so you are not connecting to localhost any more.

Gajotres
  • 57,309
  • 16
  • 102
  • 130
  • I have changed the url so that the it is now url:'http://127.0.0.1:8005/getData.php' and what i see in the android logcat is sqlite returned error code = 14 , msg = cannot open 206 bf [42537b6056] – Ethan Richardson Dec 28 '12 at 22:29
  • 127.0.0.1 is still a localhost. What platform are you testing on : Windows, Linux or MacOS? – Gajotres Dec 28 '12 at 22:29
  • then press window + R, type cmd and in command prompt type ipconfig, your computer IP should look something like this: IPv4 Address. . . . . . . . . . . : 192.168.0.10 – Gajotres Dec 28 '12 at 22:33
  • Thank you, i have changed that now and it works on firefox but not on my emulator. I get the log cat error message sqlite returned error code = 14 , msg = cannot open 206 bf [42537b6056] and my rows are not populated – Ethan Richardson Dec 28 '12 at 22:38
  • Then you need to white list your real IP in Phonegap, here you will find more: http://stackoverflow.com/questions/9174856/access-external-urlgoogle-com-from-phonegapandroid/9187479#9187479. Worst case scenario change it as you switch a platform. – Gajotres Dec 28 '12 at 22:40
  • I have added to my config.xml and still no joy, am i getting close? – Ethan Richardson Dec 28 '12 at 22:47
  • Wait this is s sqllite error, where are you using sqllite in your app? I cant find it in your code example? – Gajotres Dec 28 '12 at 22:59
  • 1
    I have resolved this, i turned on the wifi on my phone and this all connected. Thank you for you help. Many Thanks – Ethan Richardson Dec 28 '12 at 23:17
  • No problem m8, I am glad to help – Gajotres Dec 28 '12 at 23:19