user11022957
user11022957

Reputation:

Query in Retrofit2

I am using the retrofit GET method. How do I pass a query parameter to it?

@GET("/datas/")
Call<List<Data>> getDataInfo();

Upvotes: 1

Views: 97

Answers (4)

Shivam Yadav
Shivam Yadav

Reputation: 1028

Above answers will be suitable if you want to send one or two parameters only , if you want to send multiple parameters then you can send it like below-

@GET("/datas/")
Call<List<Data>> getDataInfo(@QueryMap HashMap<String, String> params);

And put data in hashmap like below-

 HashMap<String, String> params = new HashMap<>();
        params.put("data1", "abc");
        params.put("data2", "50");

Upvotes: 2

Hemant N. Karmur
Hemant N. Karmur

Reputation: 880

In Retrofit use api as below:

GET Method (with parameters):

@GET("doctor_review.php")
Call<DoctorReview> getreview(@Query("doctor_id") String Id);

POST Method (with parameters):

@FormUrlEncoded
@POST("update_doctor_status.php")
Call<UpdateDoctorStatus> updateDoctorStatus(@Field("user_id") String doctor_id, @Field("status") String status, @Field("type") String type);

POST Method with json object as Body:

@POST("SocialLogin")
Call<LoginResponse> socialLogin(@Body JsonObject body);

// take below object as reference that will be passed in above post api as body
JsonObject jsonObjectLogin = new JsonObject();
    jsonObjectLogin.addProperty("email", profileEmail);
    jsonObjectLogin.addProperty("password", password);
    jsonObjectLogin.addProperty("deviceToken", refreshedToken);
    jsonObjectLogin.addProperty("Timezone", Utility.getTimeZone());

Upvotes: 0

HussainAbbas
HussainAbbas

Reputation: 262

try this one, use this "drivers" instead of "/drivers/"

@GET("drivers")
Call<List<Data>> getDataInfo(@Query("data_id") int dtaID);

Upvotes: 0

ppreetikaa
ppreetikaa

Reputation: 1247

Use the following code to pass query parameter.

@GET("YOUR_URL")
 Call<List<Data>> getDataInfo(@Query("YOUR_KEY") String your_data);

Upvotes: 0

Related Questions