立即发送php file1响应到android,然后从file1调用另一个php file2


Send php file1 response to android immediately and then call another php file2 from file1

我从android调用file1.php,我希望它发送xml响应到android,然后立即从file1.php调用file2.php。我还想发送一个数组从file1.php到file2.php。如何做到这一点,而不会延迟发送响应到android,因为android只需要从file1响应。但是file2的输入就是file1的输出。所以有一种方式,我可以发送file1的xml输出到android,然后调用file2之后立即?

细节:Android:->显示附近的餐馆使用谷歌的地方api。

File1.php:->获取附近的餐馆谷歌的地方api的列表,并将其发送到android。在此之后,我想立即从File1.php调用File2.php,在那里我发送餐馆的参考id (File1的o/p),以便我可以获得每个餐馆的详细信息。

Android代码:

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

php

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

主要是android应该在file2开始执行之前收到响应。

您不能与现有的请求-响应流并行执行外部php文件(实际上是创建一个新的http-请求)。有两种可能的解决方案

  1. 您可以在服务器上实现任务队列(通常是DB中的专用表),因此在处理file1.php的请求时,您应该在队列中添加新任务。与主web服务器进程并行,你应该有一个php"守护进程"总是在运行,它寻找新的任务并在后台处理它们(通过file2.php获取所需的详细信息)。一旦任务准备好了(或它的一部分,例如有几个带有详细信息的第一个记录可用),就在队列中适当地标记它。Android应用程序应该定期检查新的细节,并使用另一个接入点(如file3.php)加载它们,这将从部分或完全执行的任务返回可用的细节。我不知道您使用哪个服务器端框架(如果有的话),但是其中一些提供基于cron的"批处理作业"。但这看起来不那么容易。最合适的方法是使用推送通知,即服务器应该通知您的客户端关于列表中已经发送到客户端的项目的新可用详细信息。

  2. 你可以将部分控制逻辑移动到Android应用程序中。通过这种方式,它请求对象列表,然后在循环中发送对象详细信息的新请求,这些请求通过file2.php代理到Google Places。也就是说,file2.php由客户端执行,而不是由服务器执行。

此外,您可能应该考虑一些php以外的服务器端工具,例如Node.js,它更适合实现长轮询和websockets(选择其中之一)。