Retrofit PUT不起作用

Retrofit PUT不起作用

问题描述:

我正在使用“Discord API”开发Android应用程序。Retrofit PUT不起作用

首先,如果我叫相同的API在邮差,它工作正常。 但在我的android应用程序,它不起作用。

我想用什么样的API是这样的:“https://discordapp.com/developers/docs/resources/guild#add-guild-member

enter image description here

我想要做同样的事情在我使用“改造库” 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"]} 

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

我找到了解决方案。 我将“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);