When calling a php file with some js code from android, js code is the result of such a call

I am making this android and see that I have a problem. the fact is that in my application one action sends the current location of users to the database, and using this very location I refer to another table in the database, where I retrieve these values, where this table consists of the location of other users. so now what I want to do is use the current location of users and refer to the location of other users who want to calculate and then select the closest user, I went ahead and made a .php file. and when run in a web browser I see the correct en. but doesn't seem to work in the application. remember that php tat file makes this counter using javascript. This is a php file that I call in my asynchronous task, which finds the closest user. 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 that when I use the debug tool in eclipse, I see that all the code after

DOCTYPE html

becomes the jonn call output of the async call.

so basically all i want to do is call this php file from the application, it should calculate the closest user and return the latitude and longitude of the closest user to application.maybe by updating the same php file variable or updating another file variable php, as in the above case I know that this may be the wrong way to do this, so any help would be buzzing.

+7
javascript android php android-asynctask
source share
1 answer

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

You must either convert your js logic to php, or return the calculated result, or use a webview in your application that can execute js and pass the result to you. You can consult with this: How to execute JavaScript on Android?

Edit: There is this library that creates a web view behind the scenes and does javascript (note: I have not tested the library myself)

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

+1
source share

All Articles