![](/img/trans.png)
[英]Android java.net.SocketTimeoutException: Connection timed out
[英]HttpURLConnection error: java.net.SocketTimeoutException: Connection timed out
我正在使用這樣的簡單urlconnection:
url = URL+"getClient&uid="+cl_id;
URL url = new URL(this.url);
Log.d("Set++","get_t URL: "+ url);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
它的工作正常,但有時我會收到此錯誤:
error: java.net.SocketTimeoutException: Connection timed out
可能是什么原因? 我只有4個客戶端...所以我不認為服務器的連接超載。
編碼:
try {
URL = Settings.BASE_URL + "_interface.php?" +
"key=" + Settings.KEY +"&app_naam="+Settings.APP_NAAM+ "&action=check&setTime="+c;
URL url = new URL(URL);
if(D)Log.e("ChekTreadAanvr+url", URL);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
BufferedReader rd = new BufferedReader(
new InputStreamReader(conn.getInputStream()));
String response;
if ((response = rd.readLine()) != null) {
rd.close();
}
if(D) Log.d("WebSaveThread+","DATARESIEVED: "+response);
return response;
} catch (MalformedURLException e) {
if(D)Log.d("ERR","server chekc failure ++ ");
return success = false;
} catch (IOException ioex) {
if(D)Log.e("ERR", "error: " + ioex.getMessage(), ioex);
success = false;
}
return Boolean.toString(success);
您的Internet連接速度可能很弱。 您可以使用以下方法增加超時間隔。
httpURLConnection.setConnectTimeout( 6 * 10 * 1000 ); // One Minute
如果要連接到速度特別慢的服務器,則可以設置兩個超時: setConnectTimeout()
和setReadTimeout()
。 名稱很容易解釋。 例如:
httpURLConnection.setConnectTimeout(60*1000);
httpURLConnection.setReadTimeout(60*1000);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.