这是一种快速简单的方法,如果您扩展以进行更多的实际使用会更好:
1-下载phonegap
2- 使用 this tutorial 或使用 phonegap 构建您的第一个应用程序如何获得关注
3- 一旦你有了,让我们去服务器端.. 我们需要一个 API,最简单的方法是这样的:
<?php
header('Access-Control-Allow-Origin: *');
header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Content-type: application/json');
if($_GET['nameofFunction'] == 'getHomepageContent'){
require_once('controllers/homeController.php');
$objHome = new homeController();
$jsonReturn = $objHome->gethome();
echo($jsonReturn);
}
?>
4- 创建该控制器来控制来自 API 的请求,例如:
<?php
class homeController {
public function __contruct(){
}
public function gethome(){
//do what ever you need here, sql query, errors handling.. and return it
//back to the api.
//for example you could use something like this to return json array to the api
// without making much effort
if(mysql_num_rows($yourquery) > 0){
while($us = mysql_fetch_assoc($yourqueryResult)){
$output[]=$us;
$homeJsonResponse = json_encode($output);
}
return $homeJsonResponse;
}
}
?>
5- 我们回到 phonegap 应用程序,现在确保您包含所有需要的文件,jquery、jquerymobile、cordovajs、itouch、iscroll....
6- 创建将在加载时执行的函数并对 api 进行 ajax 调用,这将返回 json,just parse with jquery,您可以开始了。