Ad

Send Php File1 Response To Android Immediately And Then Call Another Php File2 From File1

- 1 answer

I call file1.php from android and I want it to send xml response to android and immediately after that call file2.php from file1.php. I also want to send an array from file1.php to file2.php. How can this be done without delaying sending the response to android as android needs response only from file1. But file2 input is file1's output. So is there a way in which i can send file1's xml output to android and then call file2 immediately after that?

Details : Android :-> Display near by restaurants using google places api.

File1.php :-> Get the list of near by restaurants google places api and send it to android. Immediately after this I want to call File2.php from File1.php where I send the reference id's of the restaurants(o/p of File1) so that i can get each restaurants details.

Android Code:

ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
            nameValuePairs.add(new BasicNameValuePair("latitude",latt));
            nameValuePairs.add(new BasicNameValuePair("longitude", longi));

            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost("http://www.xyz.com/file1.php");

            httpPost.setEntity(new UrlEncodedFormEntity      (nameValuePairs));

            HttpResponse httpResponse = httpClient.execute(httpPost);

            HttpEntity httpEntity = httpResponse.getEntity();
            line = EntityUtils.toString(httpEntity);

File1.php

 $url = 'https://maps.googleapis.com/maps/api/place/search/xml?....';
 //get xml response and send it to android

//call file2.php and send reference id's so as ot get each restaurant details

File2.php

for(each restaurant)
//get details and do some back end processing.

Main thing is android should receive response before file2 starts executing.

Ad

Answer

You can't execute an external php-file (that is actually make a new http-request) in parallel to existing request-response flow. There exist 2 possible solutions.

  1. You can implement a task queue on your server (usually a dedicated table in DB), so while processing a request by file1.php you should add new task in the queue. In parallel to main web-server process, you should have a php "daemon" always running, which looks for new tasks and processes them in background (fetches required details by file2.php). Once a task is ready (or a part of it, for example several first records with details are available), mark it appropriately in the queue. Android app should periodically check new details and load them using another access point (say file3.php), which returns available details from partially or completely executed task. I don't know which server-side framework do you use (if any), but some of them provide "batch jobs" based on cron. This looks not as easy to go way though. Most appropriate method would be to use push notification, that is the server should notify your client about new available details for items in the list already sent to the client.

  2. You can move part of control logic into the Android app. This way it requests a list of objects, then in a loop send new requests for object details, which are proxied to Google Places by means of file2.php. In other words, file2.php is executed by client instead of the server.

In addition, you should possibly look at some server-side tools other than php, for example Node.js, which is much better suited for implementing long polling and websockets (choose one of them).

Ad
source: stackoverflow.com
Ad