user10480468
user10480468

Reputation:

How to Pass URL String in using Retrofit in Kotlin

I have class A which contains a variable. I need to pass the variable inside Class B interface file so, I can get Dynamic output in Kotlin. If Its a constant string then I am able to retrieve the result but I am not sure how to change the value on the fly. I used this below code for my Test but this is static JSON Value.

Class:A
        val intent= Intent(customView.context,ApiMovies::class.java)
        intent.putExtra(MOVIES_TITLE_KEY,Movies?.name)
Class:B
interface ApiMovies {
//   val name= intent.getStringExtra(MOVIES_TITLE_KEY,Movies?.name)

    @GET("get_movie.php?name=DunKirk")
    fun getMovies() : Observable<MovieResponse>
}

Class:C
        val retrofit : Retrofit = Retrofit.Builder()
                    .baseUrl("https://www.imdb.com")

Class:D
class MovieResponse {

    lateinit var data : List<Movie>
}

Upvotes: 0

Views: 2633

Answers (2)

user10480468
user10480468

Reputation:

I changed my code like below.

Class:A
        val intent= Intent(customView.context,ApiMovies::class.java)
        intent.putExtra(MOVIES_TITLE_KEY,Movies?.name)

 apiMovies.getMovies($intent)
Class:B
interface ApiMovies {

    @GET("get_movie.php")
    fun getMovies(@Query("name") name:String) : Observable<MovieResponse>
}

Class:C
        val retrofit : Retrofit = Retrofit.Builder()
                    .baseUrl("https://www.imdb.com")

Class:D
class MovieResponse {

    lateinit var data : List<Movie>
}

Upvotes: 2

aolphn
aolphn

Reputation: 2998

I think you can add a params for your classB's method

Class:B interface ApiMovies { 
@GET("get_movie.php")
 fun getMovies(@QueryMap() Map<String, String> info) : Observable<MovieResponse>
 }

before invoking your method ,create a map,add k&v into map(map.add("name",xxx)),invoke it like getMovies(map),that will be ok,you needn't to pass your variable to classB.

Upvotes: 0

Related Questions