2017-10-21 2 views
0

"Discord API"を使用してAndroidアプリケーションを開発しています。更新機能PUTが機能しません

まず、私がPostmanで同じAPIを呼び出すと、正常に動作します。 しかし、私のアンドロイドアプリケーションでは、動作しません。

私が使用したい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"]} 

あなたは、私が間違えないものを私にしてください教えてもらえますか?

答えて

0

解決策が見つかりました。 "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); 
関連する問題