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

标签 android retrofit retrofit2

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

非常感谢任何帮助..

最佳答案

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

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

这里的位置是 pojo 对象:

public class Location {
String lat,lng;

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

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

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

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

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

关于android - 如何在 Retrofit 2 (Android) 中将 JSON OBJECT 作为参数发送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34564982/

相关文章:

android - IdleConnectionHandler 删除了我的 Android 应用程序中不存在的连接

java - OkHttp 是否支持接受自签名 SSL 证书?

java.lang.IllegalArgumentException : @Url cannot be used with @GET URL (parameter #1) 异常

android - 如何在带有改造的android中显示HTML响应

android - Retrofit 多部分请求是否可以暂停和恢复?

android - Dagger 2 注入(inject)无效

android - 需要来自 payu money Android 的 JSON/String 完整响应。只获取 PaymentId

java - Android多个activity访问一个java类

android - 启动通用 Activity 并等待 AsyncTask 完成

java - 如何使用 Retrofit/OkHttp 使并发请求更快?