-1

已经在另一篇文章中询问过,但没有解决方案,请参阅下面的脚本,我该如何更改,请完整演示,以便我可以在网络中断时设置 readtimeout

根本我必须连接到一个url,只读一行,把它作为一个字符串......完成希望你能举个例子。欢迎任何其他工作方式.. thx

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) {
}
4

2 回答 2

0

如何使用 HttpURLConnection

import java.net.HttpURLConnection;

   URL url = new URL("http://mydomain/myfile.php");

   HttpURLConnection huc = (HttpURLConnection) url.openConnection();
   HttpURLConnection.setFollowRedirects(false);
   huc.setConnectTimeout(15 * 1000);
   huc.setRequestMethod("GET");
   huc.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2 (.NET CLR 3.5.30729)");
   huc.connect();
   InputStream input = huc.getInputStream();

这里挑选的代码

于 2013-09-12T19:29:44.017 回答
0

暂停:

URL url = new URL("http://www.google.com");
URLConnection conn = url.openConnection();
conn.connect();
conn.setReadTimeout(10000); //10000 milliseconds (10 seconds)
于 2013-09-12T19:32:27.990 回答