如果电话无法联系服务器,我将如何设置超时?这背后的原因是我有一个不断旋转的加载对话框,我希望连接在 10 秒后放弃,这将关闭加载对话框。
public class GetResults {
String data = null;
String URLME = null;
String Search = null;
BufferedReader inn;
@SuppressWarnings("finally")
public String GetLocationData(String THESEARCHSTRING) throws Exception {
Search = THESEARCHSTRING;
try {
URL site = new URL("http://www.google.com/");
java.net.URLConnection yc = site.openConnection();
inn = new BufferedReader(new InputStreamReader(yc.getInputStream()));
StringBuffer sb = new StringBuffer("");
String l = "";
String ln = System.getProperty("line.separator");
while ((l = inn.readLine()) != null) {
sb.append(l + ln);
}
inn.close();
data = sb.toString();
URLME = data;
return URLME;
} finally {
if (inn != null) {
try {
inn.close();
return URLME;
} catch (Exception e) {
e.printStackTrace();
}
}
return URLME;
}
}
}