我有一个服务器,我需要从中获取一些数据。在一开始,我就利用curl
来获得原始的JSON
响应,这是快速的,但对我来说还不够。我需要将原始JSON
转换为某些特殊格式。
因此,我尝试使用java.net.HttpURLConnection
编写这种脚本。
这是我的代码段:
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.*;
........
url="xxxx" //It is not the localhost but the server on other region
HttpURLConnection conn = (HttpURLConnection) new URL(url.toString()).openConnection();
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setConnectTimeout(5000);
conn.setRequestMethod("GET");
if (conn.getResponseCode() == 200) {
StringBuffer buffer = new StringBuffer();
InputStream inputStream = conn.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String str = null;
long startTime = System.currentTimeMillis();
//The format of response is JSON
while ((str = bufferedReader.readLine()) != null) {
//read and convert the raw data to some special data struct
//The time overhead of this part is trivial
}
..........
当我尝试获取少量数据(即仅包含几行)时,上面的代码片段不会明显停留在while(xxxx)
上。但是,随着数据量的增加(超过3000行),while ((str = bufferedReader.readLine()) != null)
的次数过多使总时间变得无法接受。
任何人都可以给我一些建议:
HttpURLConnection
,如何优化我的代码以减少额外的时间?HttpURLConnection
的Java第三部分替代品,并且适合我在如此高延迟要求的情况下工作?while
循环?其他:
来自本地主机: