2

您好,我是 phonegap 和 jquery mobile 的新手,在尝试通过 eclipse 模拟器和我的 htc 手机运行我的应用程序时遇到问题。

当我在 Firefox 中运行应用程序时,我的代码运行良好。HTML调用在tomcat上运行的PHP脚本,php脚本访问mysql并传回jsonp数据。

任何帮助将非常感激。我已经阅读了其他帖子并且没有锁定我的代码。

我已使用以下所有正确权限更新了 android 清单:

<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" />

我在下面有我的 html 文件:

    <!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>

我的工作 php 脚本如下:

<?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) . ');';
?>
4

1 回答 1

1

在安卓手机上测试时,你不能使用url:'http://localhost/getData.php' 。请记住,您不再在本地计算机上。手机的IP地址与您的服务器不同,因此您不再连接到localhost。

于 2012-12-28T22:26:09.480 回答