android - Android - 无法连接到 REST API - 错误请求
问题描述
我正在尝试将我的 android 应用程序与在我的 PC 上运行的 API 连接起来。
我正在我的实际手机上测试该应用程序。基本 URL 是http://localhost:5000/api/。
object RequestManager {
val interceptor = HttpLoggingInterceptor()
val client = OkHttpClient.Builder().addInterceptor(interceptor).build()
init {
//TODO must be None at live
interceptor.level = HttpLoggingInterceptor.Level.BODY
}
val retrofit = Retrofit.Builder()
.baseUrl("http://localhost:5000/energy/api/")
.addConverterFactory(GsonConverterFactory.create())
.client(client)
.build()
val service = retrofit.create(Api::class.java)
}
我尝试使用 http://< myIP >:5000/energy/api/ 但收到错误的请求消息。
> D/OkHttp: <-- 400 Bad Request
有什么帮助吗?
解决方案
尝试使用ngrok。当您插入 localhost 和端口时,它可以解决您的问题,它为您提供了一个可以使用的 URL。
推荐阅读
- java - JAVA 序列化和反序列化:在一个文件下有自己的数组列表的两个独立对象
- android - 如何在recyclerview中显示所有复选框?
- sql - 如何将 UNIX 纪元日转换为 Snowflake 中的 DATE?
- wavesplatform - 如何从其他 Waves Oracle 读取数据?
- pentaho - 如何用百万个寄存器在 Pentaho 中搜索寄存器
- sql - postgres SQL 中的函数
- python - 如何使用复杂的条件以有效的方式在熊猫中逐列填充?
- python - 如何使用熊猫数据框在一列中获取所有列?
- ruby-on-rails - Rails + Devise:在初始登录时强制重置密码
- python - 如何在 Python 中制作 2D 列表的正确副本