Retrofit 2 - URL Query Parameter
If you specify @GET("foobar?a=5")
, then any @Query("b")
must be appended using &
, producing something like foobar?a=5&b=7
.
If you specify @GET("foobar")
, then the first @Query
must be appended using ?
, producing something like foobar?b=7
.
That's how Retrofit works.
When you specify @GET("foobar?")
, Retrofit thinks you already gave some query parameter, and appends more query parameters using &
.
Remove the ?
, and you will get the desired result.
Retrofit and GET using parameters
AFAIK, {...}
can only be used as a path, not inside a query-param. Try this instead:
public interface FooService {
@GET("/maps/api/geocode/json?sensor=false")
void getPositionByZip(@Query("address") String address, Callback<String> cb);
}
If you have an unknown amount of parameters to pass, you can use do something like this:
public interface FooService {
@GET("/maps/api/geocode/json")
@FormUrlEncoded
void getPositionByZip(@FieldMap Map<String, String> params, Callback<String> cb);
}
Retrofit: multiple query parameters in @GET command?
You should be using this syntax:
@GET("/my/API/call")
Response getMyThing(
@Query("param1") String param1,
@Query("param2") String param2);
Specifying query parameters in the URL is only for when you know both the key and value and they are fixed.
How to make a request with a param using Retrofit and coroutines
Let's imagine you have next interface:
interface Webservice {
@GET("/getrequest")
suspend fun myRequest(@Query("queryParam1") String param1): Object
}
Inside a view model that you have you can define a method that will execute retrofit call inside a coroutine:
import androidx.lifecycle.Transformations
import kotlinx.coroutines.Dispatchers
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.LiveData
class YourViewModel {
private val mutableLiveData = MutableLiveData<Object>()
val liveData = Transformations.map(mutableLiveData) { object ->
// return object or use it to calculate
// new result that will be returned by this liveData object
// e.g. if object is a List<Int> you can sort it before returning
object
}
companion object {
// Just for the sake of explaining we init
// and store Webservice in ViewModel
// But do not do this in your applications!!
val webservice = Retrofit.Builder()
.baseUrl(Constant.BASE_URL)
.addConverterFactory(yourConverter)
.build()
.create(Webservice::class.java)
}
...
fun executeNetworkRequest(String text) {
viewModelScope.launch(Dispatchers.IO) {
val result = webservice.myRequest(text)
withContext(Dispatchers.Main) {
mutableLiveData.value = result
}
}
}
}
Retrofit, request get with not-necessary parameters
You don't have to write separate code for each query parameter. Retrofit
supports optional query parameters. Here is the Java
code for your example. If you pass null for any parameter, Retrofit
will omit that query while forming the request.
@GET("user_vacancy") Single<VacanciesListResponse> getVacanciesWithFilter(
@Query("employment") String employmentType,
@Query("time") int time
);
More info here. Using QueryMap
is also a good approach as mentioned in another answer.
How to pass parameters to POST request using Retrofit and then serialize request?
You should use @Body
annotation:
Use this annotation on a service method param when you want to directly control the request body of a POST/PUT request... The object will be serialized using the Retrofit instance Converter and the result will be set directly as the request body. Body parameters may not be null.
You might use
@POST("/startup")
fun getDataVenom(
@Body body: BodyDTO
): Call<ObjectFromResponse>
where BodyDTO
is
data class BodyDTO(
@SerializedName("players") private val players: Array<String>,
@SerializedName("action") private val action: String
)
Related Topics
How to Set Ringtone with Ringtonemanager.Action_Ringtone_Picker
How to Find Memory Leak Class/Activity in Android
Fragment Standard Transition Not Animating
How to Make Part of The Text Bold in Android at Runtime
Actionlayout on Menuitem Does Nothing
Access Ordered Images and Video in Same Cursor
How to Enable Night Mode Programmatically
Android Detect Webview Url Change
Android: Execute Code in Regular Intervals
Custom Attributes in Android Fragments
How to Show Alphabetical Letters on Side of Android Listview
Simulate Killing of Activity in Emulator
Db File in Assets Folder. Will It Be Updated
How to Pick Image for Crop from Camera or Gallery in Android 7.0
How to Forward My Localhost Ip-Address to an Android Emulator
Android - Set a Progressbar to Be a Vertical Bar Instead of Horizontal