Rest API不通过android触发JavaScript但在浏览器中工作
Posted
技术标签:
【中文标题】Rest API不通过android触发JavaScript但在浏览器中工作【英文标题】:Rest API not triggering JavaScript through android but working in browser 【发布时间】:2020-05-25 11:27:45 【问题描述】: <?php
$conn = mysqli_connect('localhost','eyukti_home_roc','4nYntQuCjPYR','eyukti_home_roc');
$user_id = $_GET['user_id'];
$sql = mysqli_query($conn,"SELECT * FROM `vehicle_type` WHERE status = '1' AND is_delete !='1'");
include("../firebase.php");
?>
<script>
(function()
// Initialize the Firebase SDK
firebase.initializeApp(
databaseURL: "https://xxx-xxxx.firebaseio.com"
);
var user_new_lat = null;
var user_new_lng = null;
var ref_driver_location = firebase.database().ref('Driver/DriverLocation');
var ref_user = firebase.database().ref('User').child('<?php echo $user_id; ?>');
var ref_nearest_driver = ref_user.child('NearestDriver');
var ref_availableDriver = ref_user.child('availableDriver');
var ref_driver = firebase.database().ref('Driver');
var geoFire = new geofire.GeoFire(ref_driver_location);
var user_new_location = ref_user.child('UserLocation/UserLocationNew');
var user_old_location = ref_user.child('UserLocation/UserLocationOld');
var user_location = ref_user.child('UserLocation');
var user_new = ref_user.child('UserLocation/GeoLocation');
var geoQuery = null;
user_old_location.on("value", function(newuser_snapshot)
user_location.once("value", function(nearest_driver_snapshot)
user_new_lat = nearest_driver_snapshot.child("GeoLocation/location/l/0").val();
user_new_lng = nearest_driver_snapshot.child("GeoLocation/location/l/1").val();
var user_old_lat = nearest_driver_snapshot.child("UserLocationOld/lat").val();
var user_old_lng = nearest_driver_snapshot.child("UserLocationOld/lng").val();
var geoFireUserLocation = new geofire.GeoFire(user_new);
var geoQueryUserLocation = geoFireUserLocation.query(
//alert('yes');
center: [parseFloat(user_old_lat),parseFloat(user_old_lng)],
radius: 0.6
);
var onReadyRegistration = geoQueryUserLocation.on("ready", function()
console.log("GeoQuery has loaded and fired all other events for initial data");
);
var onKeyEnteredRegistration = geoQueryUserLocation.on("key_entered", function(key, location, distance)
geoQuery = geoFire.query(
center: [parseFloat(user_old_lat),parseFloat(user_old_lng)],
radius: 3.0
);
, function (error)
console.log("Error: " + error.code);
);
, function (error)
console.log("Error: " + error.code);
);
)();
</script>
<!--<script src="../firebase_js/insertuser.js" defer></script>-->
问题是当我从浏览器点击这个 api 时这个 firebase Geofire 工作,但当我在 android 中调用这个 api 时没有点击 我试图在邮递员中调用 API,但它给出了来自 php 的响应,但没有触发 geofire 函数,并且还给出了 javascript 代码作为响应。我无法找到解决方案,因为我无法找到问题
【问题讨论】:
在android中你是在app中使用webview还是使用chrome? 我正在通过 Retrofit 调用这个 api 【参考方案1】:如果您在android中使用webview,您需要为webview启用javascript,如下所示
WebView mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.setWebViewClient(new WebViewClient()
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
view.loadUrl(url);
return true;
);
mWebView.loadUrl("htpps://yoururl");
【讨论】:
这是一个rest api而不是一个URL 好的,可能是你在retrofit中设置了php文件路径,也就是说,它给出了整个文件作为响应,你需要在retrofir中设置API端点 是的,但问题是所有这些 GeoFire ,当我从 android 中点击此 api 时,Firebase 功能不会触发 重新验证您是否在邮递员和改造中使用相同的路径 我在邮递员中面临的问题与在 android 中相同,php 代码在邮递员中给出响应但仍未触发脚本函数编辑 - 已验证从 3 天起陷入问题以上是关于Rest API不通过android触发JavaScript但在浏览器中工作的主要内容,如果未能解决你的问题,请参考以下文章
使用 JQuery Mobile 按钮触发 Rest API 调用
Android 如何 POST 到 Django Rest Framework Json 或 API?
通过更改android中的参数来重复调用rest api的最佳方法是啥