4

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.

2
  • May i ask why you use PHP ? Plus, i think you can't run PHP on android. You need apache + php installed, that's why it works on the computer, not an android. If you want to use a php file, put it on a server then request it from your app, using ajax. (IE code in android app doesn't mean anything) Commented Apr 24, 2014 at 8:40
  • sorry not mentioning it, but this file is on the server. Commented Apr 24, 2014 at 8:49

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.