Java setReadTimeout()不起作用
setReadTimeout或setConnectTimeout的实现方式和位置? 在编译之前,我的测试总是抛出未定义的错误 当没有连接时,它会在in.readLine()处阻塞,应用程序将永远等待Java setReadTimeout()不起作用,java,android,sockets,Java,Android,Sockets,setReadTimeout或setConnectTimeout的实现方式和位置? 在编译之前,我的测试总是抛出未定义的错误 当没有连接时,它会在in.readLine()处阻塞,应用程序将永远等待 try { URL url = new URL("http://mydomain/myfile.php"); //url.setReadTimeout(5000); does not work InputStreamReader testi= new InputStrea
try {
URL url = new URL("http://mydomain/myfile.php");
//url.setReadTimeout(5000); does not work
InputStreamReader testi= new InputStreamReader(url.openStream());
BufferedReader in = new BufferedReader(testi);
//in.setReadTimeout(5000); does not work
stri = in.readLine();
Log.v ("GotThat: ",stri);
in.close();
} catch (MalformedURLException e) {
} catch (IOException e) {
}
谢谢你的帮助
chris使用URL连接
URL url = new URL("http://mydomain/myfile.php");
URLConnection connection = url.openConnection()
int timeoutMs = 2000;
connection.setReadTimeout(timeoutMs );
InputStream urlInputStream = connection.getInputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(urlInputStream));
String firstLine = in.readLine();
System.out.println("GotThat: " + firstLine);
in.close();
这对我有用。
由于你的评论christian Muller,我在几个网站上试用了它,并调整了timeoutMs
值。将timeoutMs
设置为250 ms会导致SocketTimeoutException
。如果你一步一步地增加它,你会看到最后你读了一行
例如,如果我尝试:
URL url = new URL("http://msdn.microsoft.com/en-US/");
URLConnection connection = url.openConnection();
int timeoutMs = 250;
connection.setReadTimeout(timeoutMs);
我明白了
如果我对550timeoutMs
进行同样的尝试,它会起作用:
GotThat: <!DOCTYPE html>
明白了:
使用URL连接
URL url = new URL("http://mydomain/myfile.php");
URLConnection connection = url.openConnection()
int timeoutMs = 2000;
connection.setReadTimeout(timeoutMs );
InputStream urlInputStream = connection.getInputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(urlInputStream));
String firstLine = in.readLine();
System.out.println("GotThat: " + firstLine);
in.close();
这对我有用。
由于你的评论christian Muller,我在几个网站上试用了它,并调整了timeoutMs
值。将timeoutMs
设置为250 ms会导致SocketTimeoutException
。如果你一步一步地增加它,你会看到最后你读了一行
例如,如果我尝试:
URL url = new URL("http://msdn.microsoft.com/en-US/");
URLConnection connection = url.openConnection();
int timeoutMs = 250;
connection.setReadTimeout(timeoutMs);
我明白了
如果我对550timeoutMs
进行同样的尝试,它会起作用:
GotThat: <!DOCTYPE html>
明白了:
bufferedreader没有setReadTimeout方法OK。。谢谢任何演示如何执行与上述示例相同的操作,但仅包括超时?bufferedreader没有setReadTimeout方法OK。。谢谢有没有演示过如何与上面的示例相同,但只包含一个超时?谢谢。。但是,我现在如何进入我的stri=in.readLine();Log.v(“gotthe:,stri”);in.close();无论如何,我需要阅读文件中的文本行:如果你能引导我到那一点,包括阅读超时,我将不胜感激。。as stri=in.readLine();在你很好的演示之后,就是不起作用了谢谢。。但是,我现在如何进入我的stri=in.readLine();Log.v(“gotthe:,stri”);in.close();无论如何,我需要阅读文件中的文本行:如果你能引导我到那一点,包括阅读超时,我将不胜感激。。as stri=in.readLine();在你漂亮的演示之后,就是不起作用