我正在使用以下两个函数从服务器下载字符串。我也记录了下载文本所需的时间,无论是客户端还是服务器都可以看到。下载的字符串从不相同。下载字符串时超时
服务器时间只有几毫秒,但客户端看到的时间平均为100毫秒,具体取决于wifi信号。有时候,即使服务器时间仍在可接受的范围内,客户端时间也会上升到3000毫秒(但从不高于3200毫秒)。
我开始认为超时是在某处定义的,但我不知道它在哪里。这不在我的代码中,我在开发者网站和谷歌环顾四周,没有结果。
我希望有人可以给我一些线索,可以定义这种延迟,并确认它默认为3000毫秒。
private String DownloadText(String URL)
{
String str = "";
int BUFFER_SIZE = 2000;
InputStream in = null;
try{
in = OpenHttpConnection(URL);
} catch (IOException e1) {
e1.printStackTrace();
return "";
}
catch(ArithmeticException ae){
//
}
try{
InputStreamReader isr = new InputStreamReader(in);
int charRead;
char[] inputBuffer = new char[BUFFER_SIZE];
try {
while ((charRead = isr.read(inputBuffer))>0)
{
//---convert the chars to a String---
String readString =
String.copyValueOf(inputBuffer, 0, charRead);
str += readString;
inputBuffer = new char[BUFFER_SIZE];
}
in.close();
} catch (IOException e) {
e.printStackTrace();
return "";
}
} catch (Exception e) {
e.printStackTrace();
}
return str;
}
与
private InputStream OpenHttpConnection(String urlString) throws IOException {
InputStream in = null;
int response = -1;
URL url = new URL(urlString);
URLConnection conn = url.openConnection();
if (!(conn instanceof HttpURLConnection)) throw new IOException("Not an HTTP connection");
try{
HttpURLConnection httpConn = (HttpURLConnection) conn;
httpConn.setAllowUserInteraction(false);
httpConn.setInstanceFollowRedirects(true);
httpConn.setRequestMethod("GET");
httpConn.connect();
response = httpConn.getResponseCode();
if (response == HttpURLConnection.HTTP_OK) {
in = httpConn.getInputStream();
}
}
catch (Exception ex) {
throw new IOException("Error connecting");
}
return in;
}
BTW:我从谷歌的搜索结果借了这两种功能。
编辑:我从线程内调用DownloadText(url)。我开始认为这可能与超时有关。可以 ?
这并不回答我的问题。我不想自己定义它,而是找出暂停的原因。 – skari