【问题标题】:Retrofit PUT doesn't work改造 PUT 不起作用
【发布时间】:2018-04-02 09:16:07
【问题描述】:

我正在使用“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"]}

你能告诉我我做错了什么吗?

【问题讨论】:

    标签: java android retrofit2 discord


    【解决方案1】:

    我找到了解决方案。 我将“Body”参数的类型从“String”更改为“Object”。

    代码前:

    @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);
    

    【讨论】:

      猜你喜欢
      • 2017-07-28
      • 2018-12-16
      • 1970-01-01
      • 2017-06-27
      • 2016-08-15
      • 1970-01-01
      • 2015-10-07
      • 2018-02-04
      • 1970-01-01
      相关资源
      最近更新 更多