我正在使用“Discord API”开发Android应用程序。
首先,如果我在Postman中调用相同的API,它可以正常工作。 但在我的Android应用程序中,它不起作用。
我想要使用的API是:“https://discordapp.com/developers/docs/resources/guild#add-guild-member”
我想在我的Android应用程序中使用“Retrofit Library”做同样的事情。
以下是界面。
@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"]}
你能告诉我我的错误吗?
答案 0 :(得分:0)
我找到了解决方案。 我更改了&#34; Body&#34;参数的类型来自&#34; String&#34; to&#34; Object&#34;。
在代码之前:
@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);