【问题标题】:I can't connect to the server via Retrofit我无法通过 Retrofit 连接到服务器
【发布时间】:2021-07-07 20:52:14
【问题描述】:

无法通过 android 连接到本地服务器。服务器工作正常(通过控制台检查+通过电话控制台检查)。 在服务器上,端口指定为 3000 本地服务器设备 ID 192.168.0.107

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        ApiInterface.create()
    }
}
interface ApiInterface {
    @GET("volley_array.json")
    fun getMovies() : List<String>

    companion object {
        private const val BASE_URL = "https://192.168.0.107:3000"

        fun create() : ApiInterface {
            val retrofit = Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create())
                .baseUrl(BASE_URL)
                .build()
            return retrofit.create(ApiInterface::class.java)

        }
    }
}

【问题讨论】:

  • 将 / 添加到基本 url 的末尾。

标签: java android kotlin retrofit


【解决方案1】:

您可以通过ngrok (https://ngrok.com/download) 尝试添加此/ 基本网址结尾:

ngrok http 3000

【讨论】:

    【解决方案2】:

    检查设备是否连接在同一个调制解调器或路由器上。

    示例: 服务器IP:192.168.0.107 设备IP:192.168.0.56

    如果您使用端口 3000,则必须在调制解调器或路由器中打开它,并参考您的服务器或应用程序。

    【讨论】:

      猜你喜欢
      • 2010-09-16
      • 2013-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-17
      • 2016-08-12
      • 2020-01-01
      • 2017-07-17
      相关资源
      最近更新 更多