我目前正在开发一个用于家庭作业的 CRUD android 应用程序,我遇到了一个奇怪的问题,这在老师提供的示例中没有发生。
我们正在使用 Docker JSON Server 并向该服务器添加数据,但是当我单击按钮转到活动时出现错误。
接下来我将粘贴 logcat:
接下来,我正在复制导致错误的 Java 类“Cliente”:
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class Cliente {
private static final String URL = "http://localhost:3000/";
private static Retrofit retrofit = null;
public static Retrofit obtenerCliente(){
if(retrofit == null){
retrofit = new Retrofit.Builder()
.baseUrl(URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
}
然后,ActivityPokemones 上的第 80 行:
retrofit = Cliente.obtenerCliente();
我真的不知道哪个是问题,但我在老师给出的例子中没有那个问题,几乎是复制粘贴。
如果您使用模拟器:
The bad news first: this solution only work with the emulator and not with your actual devices.
This should enable you to catch the incoming request on the localhost server. In my case, this quickly resolved my issue.
Obviously, the use of this method is not limited to debugging. If you are on the train and have no Internet connection, you can use this approach to develop your app anyway.
最后一个提示:如果您使用 GenyMotion 作为模拟器,请使用http://10.0.3.2作为 localhost 重新路由地址。
您正在使用设备:
如果您想避免使用模拟器,则需要采用不同的方法。但是,此解决方案要求您位于同一 Wi-Fi 网络上:
再一次,所有请求都应该针对您计算机上运行的本地主机服务器。
如果您有任何疑问或问题,请告诉我们:@futurestud_io
快乐编码!
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句