[英]Retrofit PUT doesn't work
我正在使用“ Discord API”开发一个Android应用程序。
首先,如果我在Postman中调用相同的API,则可以正常工作。 但是在我的Android应用程序中,它不起作用。
我要使用的API是这样的:“ https://discordapp.com/developers/docs/resources/guild#add-guild-member ”
我想使用“ Retrofit库”在Android应用程序中做同样的事情。
下面是界面。
@PUT("guilds/{guildId}/members/{userId}")
Call<RespUser> joinGuild(@Path("guildId") String guildId, @Path("userId") String userId, @Header("Authorization") String token, @Header("Content-Type") String contentType, @Body String body);
下面是工具:
@Override
public void joinGuild(DUser dUser, String authorization) {
Log.d(TAG, "[CHICKEN] joinGuild: " + dUser + ", " + authorization);
Gson gson = new Gson();
String body = gson.toJson(new DJoinBody(authorization, dUser.getUsername()));
Log.d(TAG, "[CHICKEN] joinGuild - body: " + body);
Call<RespUser> guildCall = mDiscordService.joinGuild(BuildConfig.DISCORD_GROUP_ID, dUser.getId(), BuildConfig.DISCORD_BOT_TOKEN, "application/json", body);
Log.d(TAG, "[CHICKEN] joinGuild - request method: " + guildCall.request().method());
Log.d(TAG, "[CHICKEN] joinGuild - request headers: " + guildCall.request().headers().toString());
Log.d(TAG, "[CHICKEN] joinGuild - request body.contentType: " + guildCall.request().body().contentType().toString());
Log.d(TAG, "[CHICKEN] joinGuild - request body.: " + guildCall.request().body().toString());
Log.d(TAG, "[CHICKEN] joinGuild - request: " + guildCall.request().toString());
guildCall.enqueue(new Callback<RespUser>() {
@Override
public void onResponse(Call<RespUser> call, Response<RespUser> response) {
if (response.isSuccessful()) {
RespUser result = response.body();
Log.d(TAG, "[CHICKEN] joinGuild - result: " + result);
} else {
try {
Log.e(TAG, "[CHICKEN] joinGuild - failed: " + response.code() + ": " + response.errorBody().string());
Log.d(TAG, "[CHICKEN] joinGuild - failed: " + response.raw().toString());
Log.d(TAG, "[CHICKEN] joinGuild - failed: " + response.headers().toString());
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Override
public void onFailure(Call<RespUser> call, Throwable t) {
}
});
}
错误是:
{"_misc": ["Only dictionaries may be used in a DictType"]}
你能告诉我我在做什么错吗?
我找到了解决方案。 我将“正文”参数的类型从“字符串”更改为“对象”。
之前的代码:
@PUT("guilds/{guildId}/members/{userId}")
Call<RespUser> joinGuild(@Path("guildId") String guildId,
@Path("userId") String userId,
@Header("Authorization") String token,
@Header("Content-Type") String contentType,
@Body String body);
代码后:
@Headers({"Content-Type: application/json"})
@PUT("guilds/{guildId}/members/{userId}")
Call<RespUser> joinGuild(@Path("guildId") String guildId,
@Path("userId") String userId,
@Header("Authorization") String token,
@Body DJoinBody joinBody);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.