在学习第11章 11.6.1Retrofit用法,这节的时候发生的报错:书上关于这个地方并没有说。
我搜索问题的关键语句是:
java.net.ConnectException: Failed to connect to /127.0.0.1:80
Caused by: java.net.ConnectException: failed to connect to /127.0.0.1 (port 80) from /127.0.0.1 (port 32814) after 10000ms: isConnected failed: ECONNREFUSED (Connection refused)
Caused by: android.system.ErrnoException: isConnected failed: ECONNREFUSED (Connection refused)
这写出来是为了方便大家查看:
2020-04-29 15:00:59.397 12752-12752/com.swpu.ylq.retrofittest W/lq.retrofittes: Accessing hidden method Ldalvik/system/CloseGuard;->get()Ldalvik/system/CloseGuard; (greylist,core-platform-api, reflection, allowed)
2020-04-29 15:00:59.397 12752-12752/com.swpu.ylq.retrofittest W/lq.retrofittes: Accessing hidden method Ldalvik/system/CloseGuard;->open(Ljava/lang/String;)V (greylist,core-platform-api, reflection, allowed)
2020-04-29 15:00:59.397 12752-12752/com.swpu.ylq.retrofittest W/lq.retrofittes: Accessing hidden method Ldalvik/system/CloseGuard;->warnIfOpen()V (greylist,core-platform-api, reflection, allowed)
2020-04-29 15:00:59.419 12752-12752/com.swpu.ylq.retrofittest W/lq.retrofittes: Accessing hidden field Lsun/misc/Unsafe;->theUnsafe:Lsun/misc/Unsafe; (greylist, reflection, allowed)
2020-04-29 15:00:59.419 12752-12752/com.swpu.ylq.retrofittest W/lq.retrofittes: Accessing hidden method Lsun/misc/Unsafe;->allocateInstance(Ljava/lang/Class;)Ljava/lang/Object; (greylist, reflection, allowed)
2020-04-29 15:00:59.527 12752-12752/com.swpu.ylq.retrofittest W/System.err: java.net.ConnectException: Failed to connect to /127.0.0.1:80
2020-04-29 15:00:59.528 12752-12752/com.swpu.ylq.retrofittest W/System.err: at okhttp3.internal.connection.RealConnection.connectSocket(RealConnection.java:265)
2020-04-29 15:00:59.528 12752-12752/com.swpu.ylq.retrofittest W/System.err: at okhttp3.internal.connection.RealConnection.connect(RealConnection.java:183)
2020-04-29 15:00:59.528 12752-12752/com.swpu.ylq.retrofittest W/System.err: at okhttp3.internal.connection.ExchangeFinder.findConnection(ExchangeFinder.java:224)
2020-04-29 15:00:59.528 12752-12752/com.swpu.ylq.retrofittest W/System.err: at okhttp3.internal.connection.ExchangeFinder.findHealthyConnection(ExchangeFinder.java:108)
2020-04-29 15:00:59.528 12752-12752/com.swpu.ylq.retrofittest W/System.err: at okhttp3.internal.connection.ExchangeFinder.find(ExchangeFinder.java:88)
2020-04-29 15:00:59.528 12752-12752/com.swpu.ylq.retrofittest W/System.err: at okhttp3.internal.connection.Transmitter.newExchange(Transmitter.java:169)
2020-04-29 15:00:59.528 12752-12752/com.swpu.ylq.retrofittest W/System.err: at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:41)
2020-04-29 15:00:59.528 12752-12752/com.swpu.ylq.retrofittest W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:142)
2020-04-29 15:00:59.528 12752-12752/com.swpu.ylq.retrofittest W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:117)
2020-04-29 15:00:59.528 12752-12752/com.swpu.ylq.retrofittest W/System.err: at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:94)
2020-04-29 15:00:59.528 12752-12752/com.swpu.ylq.retrofittest W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:142)
2020-04-29 15:00:59.528 12752-12752/com.swpu.ylq.retrofittest W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:117)
2020-04-29 15:00:59.529 12752-12752/com.swpu.ylq.retrofittest W/System.err: at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
2020-04-29 15:00:59.529 12752-12752/com.swpu.ylq.retrofittest W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:142)
2020-04-29 15:00:59.529 12752-12752/com.swpu.ylq.retrofittest W/System.err: at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:88)
2020-04-29 15:00:59.529 12752-12752/com.swpu.ylq.retrofittest W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:142)
2020-04-29 15:00:59.529 12752-12752/com.swpu.ylq.retrofittest W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:117)
2020-04-29 15:00:59.529 12752-12752/com.swpu.ylq.retrofittest W/System.err: at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:229)
2020-04-29 15:00:59.529 12752-12752/com.swpu.ylq.retrofittest W/System.err: at okhttp3.RealCall$AsyncCall.execute(RealCall.java:172)
2020-04-29 15:00:59.529 12752-12752/com.swpu.ylq.retrofittest W/System.err: at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
2020-04-29 15:00:59.530 12752-12752/com.swpu.ylq.retrofittest W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
2020-04-29 15:00:59.530 12752-12752/com.swpu.ylq.retrofittest W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
2020-04-29 15:00:59.530 12752-12752/com.swpu.ylq.retrofittest W/System.err: at java.lang.Thread.run(Thread.java:919)
2020-04-29 15:00:59.537 12752-12752/com.swpu.ylq.retrofittest W/System.err: Caused by: java.net.ConnectException: failed to connect to /127.0.0.1 (port 80) from /127.0.0.1 (port 32814) after 10000ms: isConnected failed: ECONNREFUSED (Connection refused)
2020-04-29 15:00:59.537 12752-12752/com.swpu.ylq.retrofittest W/System.err: at libcore.io.IoBridge.isConnected(IoBridge.java:288)
2020-04-29 15:00:59.538 12752-12752/com.swpu.ylq.retrofittest W/System.err: at libcore.io.IoBridge.connectErrno(IoBridge.java:193)
2020-04-29 15:00:59.538 12752-12752/com.swpu.ylq.retrofittest W/System.err: at libcore.io.IoBridge.connect(IoBridge.java:135)
2020-04-29 15:00:59.538 12752-12752/com.swpu.ylq.retrofittest W/System.err: at java.net.PlainSocketImpl.socketConnect(PlainSocketImpl.java:142)
2020-04-29 15:00:59.538 12752-12752/com.swpu.ylq.retrofittest W/System.err: at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:390)
2020-04-29 15:00:59.538 12752-12752/com.swpu.ylq.retrofittest W/System.err: at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:230)
2020-04-29 15:00:59.538 12752-12752/com.swpu.ylq.retrofittest W/System.err: at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:212)
2020-04-29 15:00:59.538 12752-12752/com.swpu.ylq.retrofittest W/System.err: at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:436)
2020-04-29 15:00:59.538 12752-12752/com.swpu.ylq.retrofittest W/System.err: at java.net.Socket.connect(Socket.java:621)
2020-04-29 15:00:59.539 12752-12752/com.swpu.ylq.retrofittest W/System.err: at okhttp3.internal.platform.AndroidPlatform.connectSocket(AndroidPlatform.java:71)
2020-04-29 15:00:59.539 12752-12752/com.swpu.ylq.retrofittest W/System.err: at okhttp3.internal.connection.RealConnection.connectSocket(RealConnection.java:263)
2020-04-29 15:00:59.539 12752-12752/com.swpu.ylq.retrofittest W/System.err: ... 22 more
2020-04-29 15:00:59.540 12752-12752/com.swpu.ylq.retrofittest W/System.err: Caused by: android.system.ErrnoException: isConnected failed: ECONNREFUSED (Connection refused)
2020-04-29 15:00:59.540 12752-12752/com.swpu.ylq.retrofittest W/System.err: at libcore.io.IoBridge.isConnected(IoBridge.java:275)
2020-04-29 15:00:59.540 12752-12752/com.swpu.ylq.retrofittest W/System.err: ... 32 more
其实在这个报这个错误之间就怀疑了,书上的代码和原来访问的网址不对,这里我自然是写上的127.0.0.1 这个网址;
书上的代码是:
getAppDataBtn.setOnClickListener {
Log.d("MainActivity","this btn was clicked")
val retrofit = Retrofit.Builder()
.baseUrl("http://10.0.2.2")
.addConverterFactory(GsonConverterFactory.create())
.build()
val appService = retrofit.create(AppService::class.java)
appService.getAppData().enqueue(object :Callback<List<App>>{
override fun onResponse(call: Call<List<App>>, response: Response<List<App>>) {
val list = response.body()
if(list !=null){
for (app in list){
Log.d("MainActivity","id is ${app.id}")
Log.d("MainActivity","name is ${app.name}")
Log.d("MainActivity","version is ${app.version}")
}
}
}
override fun onFailure(call: Call<List<App>>, t: Throwable) {
t.printStackTrace()
}
})
}
出现问题的原因:
这里就有问题了,在.baseUrl 输入的网址; 经过查询 ,他(郭霖)这里传入进去的本机的ip地址,我使用自己的本机ip地址这个问题就解决了。
在windous上面 使用cmd 命令查看自己的IP。
注 在此之前 好像还用报错,就是使用这个库 ,好像是要保证自己的jdk至少是1.8以上的。当然你如果看到这个文章,你应该是解决了这个问题或者没有碰到这问题。
我参考的博客是:https://blog.csdn.net/qq_40892075/article/details/90347182?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-1&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-1
具体原因可能和Retrofit库内部有关吧。
有用可以点一下赞哟,相互鼓励。嘻嘻嘻