Ich bekomme null, wenn ich meine json Fehlerantwort von der Nachrüstung auf ein Objekt umwandle.gson.fromJson gibt null android Retrofit zurück und gson
JSON-String:
{"messageType":1,"hasErrors":true,"isSuccess":false,"message":"Invalid username or password"}
Die Klasse I erstellt:
public class ValidationContainer {
@SerializedName("messageType")
@Expose
private int MessageType;
@SerializedName("hasErrors")
@Expose
private Boolean HasErrors;
@SerializedName("isSuccess")
@Expose
private Boolean IsSuccess;
@SerializedName("message")
@Expose
private String Message;
public ValidationContainer() {
}
public String getMessage() {
return Message;
}
public void setMessage(String message) {
this.Message = message;
}
}
Mein Code:
public void onResponse(Call<UserDTO> call, Response<UserDTO> response) {
if (response.isSuccessful()) {
//TODO: Save response user properties to shared constants
Intent intent = new Intent(getBaseContext(), MainActivity.class);
startActivity(intent);
finish();
} else if (response.code() == 400) {
Gson gson = new GsonBuilder().create();
ValidationContainer container = new ValidationContainer();
try {
Log.e(Tag, response.errorBody().string());
container = gson.fromJson(response.errorBody().string(), ValidationContainer.class);
Log.e(Tag, container.getMessage());
} catch (IOException e) {
Log.e(Tag, e.getMessage());
}
}
}
Als ich log response.errorBody().string()
ich folgendes erhalten:
In build.gradle
compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
compile 'com.google.code.gson:gson:2.6.1'
Was mache ich falsch?
Versuch zu ändern 'UserDTO' zu' ValidationContainer' implementiert haben. –
@DheerubhaiBansal Aber ich möchte den Fehler konvertieren nicht die Erfolgsantwort –
freundlicherweise ändern Sie Ihre Bibliothek Version zu 'com.google.code.gson: gson: 2.6.2' –