2016-06-03 11 views
0

Ich entwickle eine Android-Anwendung mit Retrofit. Jetzt muss ich mich mit meinem Backend-Server authentifizieren. Das Ergebnis meiner „/ Token“ etwas Service-Methode wie folgt:Reftrofit und Android

{ 
    "access_token": "token_key_here", 
    "token_type": "bearer", 
    "expires_in": 86399, 
    "as:client_id": "", 
    "userName": "[email protected]", 
    ".issued": "Fri, 03 Jun 2016 10:46:44 GMT", 
    ".expires": "Sat, 04 Jun 2016 10:46:44 GMT" 
} 

Jetzt versuche ich, um dieses Ergebnis zu meiner mojo Klasse zu konvertieren, aber ohne Erfolg. Ich bekomme java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to java.lang.String Fehler. Mein Code:

POJO Klasse:

import com.google.gson.annotations.Expose; 
import com.google.gson.annotations.SerializedName; 

public class Token { 
    @SerializedName("access_token") 
    @Expose 
    private String accessToken; 

    @SerializedName("token_type") 
    @Expose 
    private String tokenType; 

    @SerializedName("expires_in") 
    @Expose 
    private Integer expiresIn; 

    @SerializedName("as:client_id") 
    @Expose 
    private String asClientId; 

    @SerializedName("userName") 
    @Expose 
    private String userName; 

    @SerializedName(".issued") 
    @Expose 
    private String issued; 

    @SerializedName(".expires") 
    @Expose 
    private String expires; 

    ... gets and meters 
} 

Login-Methode:

public Usuario login(String email, String password) { 
    Tokeb result = null; 

    AccountService service = ServiceGenerator.createService(AccountService.class); 
    Call<Object> call = service.login("password", email, password); 
    try { 
     String o = (String) call.execute().body(); 
     result = new Gson().fromJson(o, Token.class); 

    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

    return result; 
} 

Vielen Dank für Ihre Hilfe.

+0

Retrofit konvertieren automatisch die JSON-Antwort auf das Objekt. Sie müssen nicht neue Gson(). Aus Json (o, Usuario.class) ;. Aber nur: result = call.execute(). Body(); – Johann67

+0

Zuerst müssen wir Ihre Service-Schnittstelle sehen –

Antwort

0

Ihre Interface-Deklaration

@POST("/Token") 
    @FormUrlEncoded 
    @Headers({ 
      "Accept: application/json" 
    }) 
    Call<Token> getAccessToken(@Field("grant_type") String grantType, 
             @Field("username") String email, 
             @Field("password") String password); 

Ihre Erklärung

public static final String BASE_URL = "http://example.com/"; 
    Retrofit retrofit = new Retrofit.Builder() 
      .baseUrl(BASE_URL) 
      .addConverterFactory(GsonConverterFactory.create()) 
      .build(); 

Login-Methode

dieses
public Usuario login(String email, String password) { 
     MyApiEndpointInterface apiService = 
       retrofit.create(MyApiEndpointInterface.class); 
     Call<Token> call=apiService.getAccessToken("password", 
       email,password); 
     call.enqueue(new Callback<Token>() { 
      @Override 
      public void onResponse(Call<Token> call, Response<Token> response) { 
       Token token= response.body(); 

       Log.w("My App", token.accessToken); 
       Log.w("My App", token.userName); 
      } 

      @Override 
      public void onFailure(Call<AccessTokenResponse> call, Throwable t) { 
       Log.w("My App", t.getMessage()); 
      } 
     }); 
} 

versuchen aussehen sollte.