1

I have two activity and in First Activity I am calling api with retrofit

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

and Api Interface -

 interface ApiService {


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


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

so it give me a json like this-

 [
     {

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

Here the url-"https://abhi-debug.github.io/Caption/demo.json" will pass in next activity as BaseUrl of Retrofit for Next Activity.

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

myUrl is getting from (FirstActivity)json like this-

myUrl = intent.getStringExtra("URL")

in FirstActivity

 urlForNextActivity[pos] = captionsList[pos].url


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

Error at runtime

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

Note- https://abhi-debug.github.io/Caption/demo.json is fixed it cant have / at end

So is there any way to call it

2 Answers2

-1

Your baseUrl must be in https://abhi-debug.github.io/ this format.

If you have last / (i.e Forward slash) missing it gives you error.

Ashish
  • 77
  • 11
-1

You can add the "/" to your base url statically or use .plus("/") after your base url variable

ex.

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

I hope this helped, Panos.

Panos Gr
  • 667
  • 5
  • 13