我正在尝试使用 Dreamweaver 和 PhoneGap 制作 Android 应用程序。我想使用 java 脚本和 PHP 在我的应用程序上显示在线数据库中的一些内容。该代码也可以在线和本地主机运行,但只有数据库详细信息不会显示在 Android 手机上。
这是我的代码......
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
var url = "http://www.example.com/phonegap/database/json.php";
$.getJSON(url, function(result) {
console.log(result);
$.each(result, function(i, field) {
var id = field.id;
var title = field.title;
var duration = field.duration;
var price = field.price;
$("#listview").append("<a class='item' href='form.html?id=" + id + "&title=" + title + "&duration=" + duration + "&price=" + price + "'><span class='item-note'>$" + price + "</span><h2>" + title + " </h2><p>" + duration + "</p></a>");
});
});
});
</script>
</head>
<body>
<div class="bar bar-header bar-positive" style="margin-bottom:80px;">
<h1 class="title">Read Database</h1>
</div>
<br/>
<br/>
<ul class="list" id="listview">
</ul>
</body>
</html>
请有人告诉我我的代码有什么问题......?
网络权限:确保您的 Android 应用程序具有 访问外部资源所需的网络权限。在你的 应用程序的 AndroidManifest.xml 文件,您应该具有以下内容 许可:
uses-permission android:name="android.permission.INTERNET"
混合内容:如果您的在线数据库托管在以下服务器上 使用 HTTPS,请确保您的应用程序也使用 HTTPS 或允许 混合内容。某些 Android 版本限制混合内容(HTTP HTTPS 页面中的内容)。
CORS(跨域资源共享): 检查您的服务器是否允许 来自 Android 应用程序域的跨源请求。你可能需要 在您的服务器上配置 CORS 标头以允许来自 您的应用程序的域。
白名单: 确保您的 PhoneGap/Cordova config.xml 文件包含必要的白名单设置以允许访问 外部 URL。您可以像这样添加条目:
<access origin="*" /> <allow-navigation href="*" /> <allow-intent href="*" />
通过解决这些常见问题,您应该能够从在线数据库获取要在 Android 应用程序上显示的内容。