2017-12-20 13 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); 

    } 

을 그리고 이것이 내가 전화를 사용하고 코드입니다. onResponse 방법에

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? 
       } 
      } 
     }); 

, 내 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")는 가

그것이 있어야 GSON

이다

@Json(name="access_token")