如何在Retrofit 2(Android)中将JSON OBJECT作为参数发送

Rag*_*han 5 android retrofit retrofit2

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

应该非常感谢任何帮助..

Vis*_*Raj 6

您可以将参数作为hashmap或pojo发送,参数将作为JSON对象发送.如:

@POST("user/checkloc")
Call<CheckLocation> checkLocation(@Body Location location);
Run Code Online (Sandbox Code Playgroud)

这里的位置是pojo对象:

public class Location {
String lat,lng;

    public Location(String lat, String lng) {
        this.lat = lat;
        this.lng = lng;
    }
}
Run Code Online (Sandbox Code Playgroud)

它将作为JSON对象发送参数:

D/OkHttp? --> POST /api/index.php/user/checkloc HTTP/1.1
D/OkHttp?    {"lat":"28.4792293","lng":"77.043042"}
Run Code Online (Sandbox Code Playgroud)

您还可以将参数作为Hashmap发送:

@POST("user/checkloc")
Call<CheckLocation> checkLocation(@Body HashMap<String, String> hashMap);
Run Code Online (Sandbox Code Playgroud)