2017-12-20 5 views
0

Moshiを使用してサーバーから呼び出しを解析しようとしています。これは私の応答オブジェクトです。SerializedName注釈がMoshiで動作しないようです

public class TokenResponse { 
    @SerializedName("accessToken") 
    public String accessToken; 
    public String token_type; 
    public int expires_in; 
    public String userName; 
    public String name; 
    @SerializedName(".issued") 
    public String issued; 
    @SerializedName(".expires") 
    public String expires; 
    public String Roles; 

} 

これは私のエンドポイントの定義である(実際には重要ではありませんが、私はとにかくそれを含めます)

public interface ServerService { 

     @POST("/token") 
     @FormUrlEncoded 
     Call<TokenResponse> getToken(@Field("username") String username, 
            @Field("password") String password, @Field("grant_type") String grant_type); 

    } 

そして、これは私が呼び出すために使用しているコードです。

Retrofit retrofit = new Retrofit.Builder() 
       .baseUrl("https://xxx/") 
       .addConverterFactory(MoshiConverterFactory.create()) 
       .build(); 

     ServerService service = retrofit.create(ServerService.class); 

     Call<TokenResponse> call = service.getToken("[email protected]", "password1!", "password"); 

     call.enqueue(new Callback<TokenResponse>() { 
      @Override 
      public void onResponse(Call<TokenResponse> call, Response<TokenResponse> response) { 
       if (response.isSuccessful()) { 
        // tasks available 
        TextView tv = (TextView)findViewById(R.id.tvToken); 
        tv.setText(response.body().accessToken); 


       } else { 
        // error response, no access to resource? 
       } 
      } 
     }); 

onResponseメソッドでは、私のresponse.body()は常にaccessTokenを発行し、nullとして失効します。私は他のパラメータの値を取得します。 Android Profilerを使用して、これが応答として返されていることは確かです。

{ 
    "access_token":"_xxx", 
    "token_type":"bearer", 
    "expires_in":1209599, 
    "userName":"xxx", 
    "name":"LOURDES RILEY", 
    ".issued":"Tue, 19 Dec 2017 23:37:06 GMT", 
    ".expires":"Tue, 02 Jan 2018 23:37:06 GMT", 
    "Roles":"[\"Admin\"]" 
} 

私は間違っていますか? SerializedNameが動作しないのはなぜですか?

答えて

3

@SerializedName("accessToken")は、それは

@Json(name="access_token")

でなければなりませんGson

です

関連する問題