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 greatly appreciated.

+5
source share
1 answer

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

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

Here the location is a pojo object like:

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

and it will send the parameters as a JSON object like:

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

You can also send the parameter as a hashmap:

 @POST("user/checkloc") Call<CheckLocation> checkLocation(@Body HashMap<String, String> hashMap); 
+5
source

Source: https://habr.com/ru/post/1239666/


All Articles