简体   繁体   中英

Retrofit PUT doesn't work

I am developing an Android application using "Discord API".

First, if I call the same API in Postman, it works fine. But in my android application, it doesn't work.

The API what I want to use is this: " https://discordapp.com/developers/docs/resources/guild#add-guild-member "

在此处输入图片说明

And I want to do the same thing in my Android application using "Retrofit Library".

Below is the interface.

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

And below is implement:

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

        }
    });
}

And the error is:

{"_misc": ["Only dictionaries may be used in a DictType"]}

Could you tell me what I do mistake, please?

I found the solution. I changed the "Body" parameter's type from "String" to "Object".

Before code:

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

After code:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM