douyanpeng0748 2014-04-24 08:35
浏览 99
已采纳

当用android中的一些js代码调用php文件时,js代码就是这样一个调用的输出

I amd making this android applcation and am kinda stuck with a problem. the thing is that in my application, one activity sends the users current location to the database, and using this very location i refer another table in the database where i fetch these values, where this table consists of other users location. so now what i want to do is using the users current location and refering the other users location want to compute and than choose the closest user, i have gone ahead and made a .php file. and when run on the web browser shows me the correct ans. but does not seem to work in the application. mind you the php file tat makes this computaton uses a javascript. this is the php file that i call in my async task which finds the closest user. the php file

<?php
$response = array();
include 'db_con.php';

// run query
$query = mysqli_query($con,"SELECT *FROM friend WHERE d_status='0'");
// look through query

if (!empty($query)) {
    // check for empty result
    $response["details"] = array();
while($row =mysqli_fetch_assoc($query)){
    $details= array();
    $details["Latitude"]= $row["d_latitude"];
    $details["Longitude"]= $row["d_longitude"];
    array_push($response["details"], $details);
    }
}
        // success
    $response["success"] = 1;
    // echoing JSON response
    echo json_encode($response);

mysqli_close($con);
$lat=15.493403;
$longi=73.820617;
?>

<!DOCTYPE html>
<html>
<body>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2    /jquery.min.js"></script> 
<script type="text/javascript">
var latlon = new Array();
var map;
var lat=15.493403;//"<?php echo $lat; ?>";
var longi=73.820617;//"<?php echo $longi; ?>";
var geocoder;
var origin =new google.maps.LatLng(lat,longi); 
function mapLoad(){
            var data="<?php echo $response;?>";
            var data_parsed = JSON.parse(data);
            var resp = data_parsed['details'];  
            for (var i in resp) {

            latlon[i]=(resp[i].Latitude+","+resp[i].Longitude);
            //alert("latit:"+latlon[i]);
            }
            calculateDistances();

}
var directionsDisplay;
var directionsService = new google.maps.DirectionsService();

function calculateDistances() {

var service = new google.maps.DistanceMatrixService();
service.getDistanceMatrix({
    origins:[origin], //array of origins
    destinations: latlon, //array of destinations
    travelMode: google.maps.TravelMode.DRIVING,
    unitSystem: google.maps.UnitSystem.METRIC,
    avoidHighways: false,
    avoidTolls: false
}, callback);   //agettaxicoor
}

function callback(response, status) {

if (status != google.maps.DistanceMatrixStatus.OK) {
    alert('Error was: ' + status);
} else {
    //we only have one origin so there should only be one row
    var routes = response.rows[0];
    //need to find the shortest 
    var lowest = Number.POSITIVE_INFINITY;
    var tmp;
    var shortestRouteIdx;
    var resultText = "Possible Routes: <br/>";
    for (var i = routes.elements.length - 1; i >= 0; i--) {
        tmp = routes.elements[i].duration.value;
        resultText += "Route " + latlon[i] + ": " + tmp + "<br/>";


        if (tmp < lowest) {
            lowest = tmp;
            shortestRouteIdx = i;
        }
    }
    //log the routes and duration.
    $('#results').html(resultText);

    //get the shortest route
    var shortestRoute = latlon[shortestRouteIdx];
    var res = JSON.stringify(shortestRoute .split(","));      

    //alert(res);
    location.href="atestb.php?Result=" + res;  //this where the result is set.
    }
    }       
</script>
<body onload="mapLoad();"></body>
</body>
</html>

but the problem now is when i use the debug tool in eclipse i see that the entire code after

DOCTYPE html

becomes the output of the async task json call.

so basically all i want to do is on calling this php file from the app, it should compute the closest user and the return the latitude and longitude value of the closest user to the application.maybe by either updating the same php file variable or updating another php file variable like the above case i know this may not be the correct way to do this, so any help would be gud.

  • 写回答

1条回答 默认 最新

  • dougaodi8895 2014-04-24 10:06
    关注

    You cannot execute javascript by calling directly from your Android source code. When you make a call to the server, it returns a stream of content, that's it! Since your web-service is returning javascript, the android code shows the js code as if it was a string.

    You should either convert your js logic to php and return the computed result or use a webview within your app that would be able to execute the js and pass you the result. You may consult this: How to execute JavaScript on Android?

    Edit: There is this library that creates a webview behind the scenes and executes javascript (note: I haven't tested the library myself)

    https://github.com/evgenyneu/js-evaluator-for-android

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘