【发布时间】: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