Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send JSON OBJECT as a parameter in Retrofit 2 (Android)

@GET
Call<List<User>> getMyFriends(@Header(GlobalDeclarationsRetrofit.HEADER_AUTHORIZATION) String lang, @Url String url, "Need to send a json object here");

Any help should be greately appreciated..

like image 256
Raghul Sugathan Avatar asked Jan 02 '16 10:01

Raghul Sugathan


People also ask

How do you send parameters in retrofit?

You can pass parameter by @QueryMap Retrofit uses annotations to translate defined keys and values into appropriate format. Using the @Query("key") String value annotation will add a query parameter with name key and the respective string value to the request url .

How do you post raw whole JSON in the body of a retrofit request?

change your call interface @Body parameter to String, don't forget to add @Headers("Content-Type: application/json") : @Headers("Content-Type: application/json") @POST("/api/getUsers") Call<List<Users>> getUsers(@Body String rawJsonString); now you can post raw json.


1 Answers

You can send parameter as hashmap or pojo, the parameters will send as JSON object. as:

@POST("user/checkloc")
Call<CheckLocation> checkLocation(@Body Location location);

Here location is pojo object as:

public class Location {
String lat,lng;

    public Location(String lat, String lng) {
        this.lat = lat;
        this.lng = lng;
    }
}

and it will send parameters as JSON object as:

D/OkHttp﹕ --> POST /api/index.php/user/checkloc HTTP/1.1
D/OkHttp﹕    {"lat":"28.4792293","lng":"77.043042"}

You can also send parameter as Hashmap:

@POST("user/checkloc")
Call<CheckLocation> checkLocation(@Body HashMap<String, String> hashMap);
like image 83
Vishal Raj Avatar answered Sep 21 '22 19:09

Vishal Raj