【发布时间】:2018-03-30 00:51:26
【问题描述】:
现在,我正在使用 ASYNC 任务来加载数据。在当前情况下,php 回显完整的 JSON,然后 android 读取整个字符串并显示它。我在这里面临的问题是,php 需要时间来回显完整的数据。php 确实会在 2 秒内回显一个条目。我想要的是,为了让我的 android 设备在响应即将到来时读取响应,不要等待 JSON 完成,而是在它即将到来并显示时读取它。这是当前代码的样子
PHP:
<?php
while(entry!=0){
data[] = processentry(entry);//will take 1-2 secs to process a single entry
entry--;
}
echo json_encode(data);
?>
安卓端:
URLConnection con = new URL("someURL").openConnection();
con.setDoOutput(true);
OutputWriter out = new OutputWriter(con.getOutputSream());
out.write(/* post params */);
out.close();
BufferedReader br = new BufferedReader(new InputReader(con.getInputStream));
while( (tmp = br.readline()) != null)
decodedStr += tmp;
decodedStr 包含完整的 json,然后可以被 android 设备读取 我希望 PHP 在 while 循环本身中回显,并在接收到数据时让 android 读取。 有什么建议吗?
【问题讨论】:
-
你试过像github.com/violet-php/streaming-json-encoder这样的第三方库吗?它应该适合您的需求。
-
to read the response as it is coming, dont wait for the JSON to complete???您的 Android 代码即将到来时已经在读取。不清楚你想要什么。 -
decodedStr contained the complete json which can then be read by android device。如果 decodedStr 包含完整的 json,那么您的应用程序已经读取了所有内容。不清楚你在说哪里。你的帖子没有意义。 -
Php does echo one entry in 2 sec.这和你之前说的不符。 -
再次:你的。 Android 应用程序即将到来时已经在阅读。但是您以错误的方式将所有内容收集在一个字符串中。然后你显示你说的那个字符串。但是要显示的代码不存在。
标签: php android json asynchronous