我想知道我是否连接到服务器,如果没有连接显示消息,我一直在使用此代码,但无法使其正常工作,
CODE:
public boolean isConnectedToServer(String url, int timeout) {
try{
URL myUrl = new URL(url);
URLConnection connection = myUrl.openConnection();
connection.setConnectTimeout(timeout);
connection.connect();
return true;
} catch (Exception e) {
// IN THIS PART I ADDED A TOAST, TO SHOW A MESSAGE WHEN THERE NO CONNECTION BUT IT ALWAYS SHOWS IT WHEATER THERE IS OR NOT.
return false;
}
}
使用我在开始条件时添加的相同代码
如果:
if (!isConnectedToServer("http://vil/ong.php", 60)) {
new AlertDialog.Builder(this.getActivity())
.setTitle("Connected")
.setMessage("You are connected to server")
.create().show();
} else {
new AlertDialog.Builder(this.getActivity())
.setTitle("Not Connected")
.setMessage("You are not connected to server")
.create().show();
}
例外:
我收到了错误
java.net.SocketTimeoutException: failed to connect to http://vil/ong.php (port 80) after 60ms
at libcore.io.IoBridge.connectErrno(IoBridge.java:169)
at libcore.io.IoBridge.connect(IoBridge.java:122)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:183)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:452)
at java.net.Socket.connect(Socket.java:884)
at com.android.okhttp.internal.Platform.connectSocket(Platform.java:117)
at com.android.okhttp.internal.http.SocketConnector.connectRawSocket(SocketConnector.java:438)
at com.android.okhttp.internal.http.SocketConnector.connectCleartext(SocketConnector.java:105)
at com.android.okhttp.Connection.connect(Connection.java:1333)
at com.android.okhttp.Connection.connectAndSetOwner(Connection.java:1412)
at com.android.okhttp.OkHttpClient$1.connectAndSetOwner(OkHttpClient.java:131)
at com.android.okhttp.internal.http.HttpEngine.nextConnection(HttpEngine.java:481)
at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:462)
at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:368)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:476)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:118)
at com.example.lupitagarcia.yosoyvallarta.Reportes.isConnectedToServer(Reportes.java:408)
at com.example.lupitagarcia.yosoyvallarta.Reportes.onMapReady(Reportes.java:110)
at com.google.android.gms.maps.MapView$zza$1.zza(Unknown Source)
at com.google.android.gms.maps.internal.zzt$zza.onTransact(Unknown Source)
at android.os.Binder.transact(Binder.java:387)
at er.b(:com.google.android.gms.DynamiteModulesB@11951434:20)
at com.google.android.gms.maps.internal.bf.a(:com.google.android.gms.DynamiteModulesB@11951434:5)
at com.google.maps.api.android.lib6.impl.bc.run(:com.google.android.gms.DynamiteModulesB@11951434:5)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7406)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
但是AGAIN它显示了相同的信息,对于这两种情况,所以我真的不知道为什么会发生这种情况,我真的很感激如何解决这个问题。
答案 0 :(得分:0)
您收到SocketTimeoutException
,这表示连接到您的网址需要花费太多时间。因此,Java会输入您的catch
块,并返回false
。
您需要检查您是否可以访问互联网,以及您的网址是否正确。另请注意,只能从UI线程显示Toast
,并且应在不同的线程上调用网络操作;不是UI线程。由于您的Toast
正在显示,我假设您在UI线程上进行了错误的网络操作。