【问题标题】:How to call retrofit without back slash in baseUrl如何在baseUrl中调用没有反斜杠的改造
【发布时间】:2019-11-28 08:15:17
【问题描述】:

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

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

和 Api 接口 -

 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" 将作为下一个活动的改造的 BaseUrl 传递给下一个活动。

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

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

myUrl = intent.getStringExtra("URL")

在第一个活动中

 urlForNextActivity[pos] = captionsList[pos].url


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

运行时出错

原因:java.lang.IllegalArgumentException:baseUrl 必须以 / 结尾:https://abhi-debug.github.io/Caption/demo.json

注意-https://abhi-debug.github.io/Caption/demo.json 已修复但无法修复 有/在结尾

那么有什么办法叫它

【问题讨论】:

  • 显示你的基本网址
  • 您的基本网址应为 baseUrl = "abhi-debug.github.io" [注意:该网址在 SO 评论中显示为链接,因此请打开链接并从浏览器复制]
  • 你可以看看这个解释:stackoverflow.com/questions/38758570/…
  • 使用 // 这是一个转义字符
  • 您需要从第一个 json(子字符串)中提取 Base Url。在这种情况下是从位置 0 到第三个“/”的每一件事

标签: java android kotlin retrofit


【解决方案1】:

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

如果您缺少最后一个 /(即正斜杠),则会出现错误。

【讨论】:

    【解决方案2】:

    您可以将“/”静态添加到基本 url 或在基本 url 变量后使用 .plus("/")

    例如

    val baseUrl = "abhi-debug.github.io/" //to add it statically
    val baseUrl2 = "abhi-debug.github.io".plus("/") // to add dynamically
    val baseUrlNoSlash = "abhi-debug.github.io" //when you have no access to base url
    var baseUrlWithSlash = baseUrlNoSlash.plus("/") //now your url has slash at the end
    
    

    我希望这会有所帮助,帕诺斯。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-06
      • 2014-09-09
      • 1970-01-01
      • 1970-01-01
      • 2011-12-22
      • 1970-01-01
      相关资源
      最近更新 更多