我想使用OkHttpClient连接这样的服务器:
为什么连接到127.0.0.1?
fun connectUrl(serverAddress: String): Boolean{
return try {
val client = OkHttpClient()
val request = Request.Builder().url(serverAddress).build()
val response = client.newCall(request).execute()
LogUtils.showLog(TAG,"$serverAddress, connectUrl success,${response.code()} ${response.body().string()}")
true
}catch (e: java.lang.Exception){
LogUtils.showLog(TAG,"$serverAddress, connectUrl fail")
e.printStackTrace()
false
}
}
但是此函数引发异常:
I/EpgClient(16422): http://otta.bbtv.cn/tv, connectUrl fail
W/System.err(16422): java.net.ConnectException: Failed to connect to localhost/127.0.0.1:28080
W/System.err(16422): at okhttp3.internal.connection.RealConnection.connectSocket(RealConnection.java:189)
W/System.err(16422): at okhttp3.internal.connection.RealConnection.buildConnection(RealConnection.java:173)
W/System.err(16422): at okhttp3.internal.connection.RealConnection.connect(RealConnection.java:114)
W/System.err(16422): at okhttp3.internal.connection.StreamAllocation.findConnection(StreamAllocation.java:193)
W/System.err(16422): at okhttp3.internal.connection.StreamAllocation.findHealthyConnection(StreamAllocation.java:129)
W/System.err(16422): at okhttp3.internal.connection.StreamAllocation.newStream(StreamAllocation.java:98)
W/System.err(16422): at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:42)
W/System.err(16422): at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
W/System.err(16422): at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
W/System.err(16422): at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:109)
W/System.err(16422): at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
W/System.err(16422): at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
W/System.err(16422): at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
W/System.err(16422): at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
W/System.err(16422): at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:124)
W/System.err(16422): at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
W/System.err(16422): at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
W/System.err(16422): at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:170)
W/System.err(16422): at okhttp3.RealCall.execute(RealCall.java:60)