0

My problem:

I have a https request that returns a json file. For example: https:www.example.com/example/my_variable

The ''my_variable'' at the end needs one of those values : 1 , 2 , 3, or 4

my_variable is stored in a variable in my main activity in :

my_variable = getIntent().getStringExtra("my_variable");

How can I use my_variable in the API interface ?

So something like:

@GET("https:www.example.com/example/" + my_variable)
ʍѳђઽ૯ท
  • 16,646
  • 7
  • 53
  • 108
olii
  • 3
  • 1
  • 4

1 Answers1

0

You simply need to pass the variable while you make the request as follows:

First, in your Api interface (Kotlin):

@GET("yourBaseURL" + "{id}")
fun yourFunc(@Path("id") id: Int?): Call<YourServerResponse>

And then, while you make the request, it will ask you for an {id} which you've specified in the Api interface:

val yourVariable = intent.getStringExtra("yourData")
        val service = ServiceBuilder.buildService(ApiService::class.java)
        service.yourFunc(yourVariable)
            .enqueue(object : Callback<YourServerResponse?> {

                override fun onResponse(
                    call: Call<YourServerResponse?>,
                    response: Response<YourServerResponse?>
                ) {

                    if (response.code() == 200 && response.isSuccessful) {

                       // successful

                    } else {

                       // failed

                    }
                }

                override fun onFailure(call: Call<YourServerResponse?>, t: Throwable) {

                      // Error
                  
                }
            })

Take a look:

Retrofit and GET using parameters

Retrofit 2 - URL Query Parameter

ʍѳђઽ૯ท
  • 16,646
  • 7
  • 53
  • 108