java - 如何在baseUrl中调用没有反斜杠的改造

标签 java android kotlin retrofit

我有两个 Activity ,在第一个 Activity 中,我通过改造调用 api

 private fun retrofitConfiguration() {
        retrofit = Retrofit.Builder()
            .baseUrl("https://abhi-debug.github.io/")
            .addConverterFactory(GsonConverterFactory.create())
            .build()
    }

和Api接口(interface)-

 interface ApiService {


      @GET("Caption/caption_home.json")
      fun fetchUser() : Call<List<Captions>>


      @GET(" ")
      fun fetchData(): Call<List<Data>>
    }

所以它给了我一个像这样的 json-

 [
     {

        "id": 14,
        "title": "Demo",
        "url": "https://abhi-debug.github.io/Caption/demo.json"
     }
    ]

这里的 url-"https://abhi-debug.github.io/Caption/demo.json "将作为下一个 Activity 的 Retrofit 的 BaseUrl 在下一个 Activity 中传递。

private fun retrofitConfiguration() {
        retrofit = Retrofit.Builder()
            .baseUrl(**myUrl**)
            .addConverterFactory(GsonConverterFactory.create())
            .build()
    }

myUrl 像这样从 (FirstActivity)json 获取-

myUrl = intent.getStringExtra("URL")

在 FirstActivity 中

 urlForNextActivity[pos] = captionsList[pos].url


  val intent = Intent(context, **SecondActivity**::class.java)
    intent.putExtra("URL", urlForNextActivity[position])
    context.startActivity(intent)

运行时出错

Caused by: java.lang.IllegalArgumentException: baseUrl must end in /: https://abhi-debug.github.io/Caption/demo.json

注意- https://abhi-debug.github.io/Caption/demo.json是固定的它不能 有/结束

那么有没有什么方法可以调用呢

最佳答案

您的 baseUrl 必须在 https://abhi-debug.github.io/ 这种格式。

如果您缺少最后一个 /(即正斜杠),它会给您带来错误。

关于java - 如何在baseUrl中调用没有反斜杠的改造,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59084334/

相关文章:

java - 导入org.springframework.boot.autoconfigure.web.ErrorAttributes无法解析

android - 将属性添加到列表时为 "Attempt to write to null array"

android - 错误 : Execution failed for task ': app: processDebugGoogleServices

performance - Kotlin Coroutines - 一个循环内的延迟是如何快速完成的

Kotlin 单元测试 - 如何模拟 Companion 对象的组件?

java - 我们可以创建一个值为 : operator? 的 Java 变量吗

java - 了解 JNI 参数的安全访问

java - 单击按钮后,应用程序已停止,它必须从 listview id 获取

android - 有没有办法一次运行多个 IntentService?

Android FlexboxLayoutManager 重新创建项目