首页 > 解决方案 > 如何在改造中使用原始 json 对象调用改造 @DELETE 方法?

问题描述

我尝试使用原始 json 对象删除 Retrofit 方法来删​​除我的列表项。我曾经将 POST 方法与 raw 方法一起使用,但我需要与 @DELETE 方法相同。这里发布方法代码:

@Headers("Content-Type:application/json")
@POST("api/tracking/post")
Call<MyResponse> getUser(@Body JsonObject jsonBody);

我需要这种类型的 DELETE 方法和带有这种类型 url 的原始对象

api/跟踪/删除/{user_id}

我试着用

@Headers("Content-Type:application/json")
@DELETE("api/tracking/delete/{user_id}")
Call<MyResponse> getUser(@Body JsonObject jsonBody);

我把我的json

{
"store_id":"oddeve78",
"user_id":1,
"api_token":"nzBxRT9lr2T0WDnd1DAA8Z6uXZRJQt3OyNkP6rfqCl8kEvNeyM7KoHvkqHU0}

标签: androidjsonhttpretrofitretrofit2

解决方案


您可以创建一个函数来为您打包。如果你真的想传递 JsonObject。

public Call<MyResponse> getUserByUserId(JSONObject json) {
   String userId = json.get("user_id");
   if (userId == null) return null;
   return getUser(userId);
}

你的电话:

@Headers("Content-Type:application/json")
@DELETE("api/tracking/delete/{user_id}")
Call<MyResponse> getUser(@Path("user_id) String userId);

推荐阅读