[英]Android check if remote server is online
我是Android編程的新手,因此一直遇到本機Java中不存在的問題。 當我在計算機上運行此代碼時,它可以正常運行。 但是,當我在設備上運行它時,卻一無所獲,我什至試圖將消息發布到UI上,因此沒有日志記錄器。 我不確定是怎么了。
try{
Socket socket = new Socket(serverAddr, SERVER_PORT);
Log.i("TAG","Socket connected");
}catch(IOException e){
Log.i("TAG","Socket not connected");
}
更新1:我只是更改了代碼。什么都沒做,並且意識到在2分鍾左右的時間里它可以完成它應該做的事情? 與保持活動標記有關嗎? 還是無論如何,我可以只運行一兩秒鍾的代碼並將其停止。 請理解,如果服務器已死,套接字創建行下面的代碼僅在2分鍾后執行。 下面是我的代碼:
try{
InetAddress serverAddr = InetAddress.getByName(serverIP);
//Line below executes with no delay
postToUI("Trying to connect to standalone server" + "\n\n");
socket = new Socket(serverAddr, SERVER_PORT);
//Line below executes after 2 minutes
postToUI("Successfully connected to standalone server" + "\n\n");
}catch(ConnectException e){
postToUI("Socket not connected");
}catch(IOException e){
postToUI("Socket not connected");
}
我很少做Andrioid開發,所以不要咬我笑臉 。
主機可能無法與客戶端通信的可能原因
對於logcat,我認為logcat僅顯示主線程的日志消息。 但是我不確定。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.