Android - 无法连接到 rest API - 错误请求
Android - Failed to connect to rest API - Bad Request
我正在尝试将我的 android 应用与在我的 PC 上运行的 API 连接。
我正在实际 phone 上测试该应用程序。
基数 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
有什么帮助吗?
如果您使用此基础 URL 是 http://localhost:5000/api/
或 <PC IP>
那么 运行 您的应用程序在模拟器上而不是在实际设备上,因为所有代码和 api 运行正在您的本地服务器中。
尝试使用 ngrok。插入本地主机和端口时可能会解决您的问题,它会为您提供 URL 以供使用。
我正在尝试将我的 android 应用与在我的 PC 上运行的 API 连接。
我正在实际 phone 上测试该应用程序。 基数 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
有什么帮助吗?
如果您使用此基础 URL 是 http://localhost:5000/api/
或 <PC IP>
那么 运行 您的应用程序在模拟器上而不是在实际设备上,因为所有代码和 api 运行正在您的本地服务器中。
尝试使用 ngrok。插入本地主机和端口时可能会解决您的问题,它会为您提供 URL 以供使用。