我尝试从局域网中的来源通过http获取xml响应中的内容。 当我在浏览器中打开网址时,加载时间是173毫秒,但是当我想要在我的APP内部加载时,加载时间超过15秒(使用我的手机测试,而不是模拟器)。
我试过这两种方式:
Log.d("start download",url);
URL serverAddress = null;
try {
serverAddress = new URL(url);
URLConnection urlConnection = serverAddress.openConnection();
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String source = "";
String tmp;
while ((tmp = reader.readLine()) != null) {
source += tmp;
}
Log.d("end download",url);
return source;
} catch (MalformedURLException e) {
e.printStackTrace();
return "FALSE";
} catch (IOException e) {
e.printStackTrace();
return "FALSE";
}
和
Log.d("Starte download",url);
try {
HttpParams httpParameters = new BasicHttpParams();
httpParameters.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
HttpConnectionParams.setConnectionTimeout(httpParameters, 5000);
HttpConnectionParams.setSoTimeout(httpParameters, 10000);
HttpClient client = new DefaultHttpClient(httpParameters);
HttpGet get = new HttpGet(url);
HttpResponse response = client.execute(get);
InputStream in = response.getEntity().getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String source = "";
String tmp;
while ((tmp = reader.readLine()) != null) {
source += tmp;
}
Log.d("Beende download",url);
return source;
} catch (IOException io) {
Log.e("Downloader", "Couldn't downlaod " + url);
io.printStackTrace();
return "FALSE";
}
还尝试了:urlConnection.setUseCaches(false)和urlConnection.setConnectTimeout(1000),结果相同
更新
我添加一些Log.d来查看哪个部分需要时间:
From Log.d(“start download”,url);直到Linereader:180ms
10 ms pro 5 - 30行
该文件有3000行