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.
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
Zuerst müssen wir Ihre Service-Schnittstelle sehen –