我使用改造将json对象发送到服务器,我试图创建一个用户,我发送这个json对象
public class FocusDataTrigger : StateTriggerBase
{
#region DataValue
public static readonly DependencyProperty DataValueProperty =
DependencyProperty.RegisterAttached("DataValue", typeof(object),
typeof(FocusDataTrigger), new PropertyMetadata(null, DataValueChanged));
public static object GetDataValue(DependencyObject obj)
{
return obj.GetValue(DataValueProperty);
}
public static void SetDataValue(DependencyObject obj, object value)
{
obj.SetValue(DataValueProperty, value);
}
#endregion
#region TriggerValue
public static readonly DependencyProperty TriggerValueProperty =
DependencyProperty.RegisterAttached("TriggerValue", typeof(object),
typeof(FocusDataTrigger), new PropertyMetadata(false, TriggerValueChanged));
public static object GetTriggerValue(DependencyObject obj)
{
return obj.GetValue(TriggerValueProperty);
}
public static void SetTriggerValue(DependencyObject obj, object value)
{
obj.SetValue(TriggerValueProperty, value);
}
#endregion
private static void TriggerStateCheck(DependencyObject target, object datavalue, object triggerValue)
{
FocusDataTrigger trigger = target as FocusDataTrigger;
if (trigger == null) return;
trigger.SetActive(triggerValue == datavalue);
}
private static void TriggerValueChanged(DependencyObject target, DependencyPropertyChangedEventArgs e)
{
object datavalue = target.GetValue(FocusDataTrigger.DataValueProperty);
TriggerStateCheck(target, datavalue, e.NewValue);
}
private static void DataValueChanged(DependencyObject target,
DependencyPropertyChangedEventArgs e)
{
object triggerValue = target.GetValue(FocusDataTrigger.TriggerValueProperty);
TriggerStateCheck(target, e.NewValue, triggerValue);
}
}
在这个例子中我创建了一个无效的用户,所以服务器响应带有一个json对象,其中代码的代码是错误的,并告诉它错误的解释
{"apellido":"prueba","email":"prueba@hotmail.com","fechaDeNacimiento":"11/29/1998","formaDeRegistro":"Android","nombre":"prueba","password":"12345678","username":"prueba"}
接口
[{"code":"5","message":"The email is in use"}]
}
数据模型
public interface UserClient {
@POST("usuarios")
Call<Usuarios> create(@Body Usuarios usuario);
}
和改造实施
import com.google.gson.annotations.SerializedName;
public class Usuarios {
@SerializedName("username")
String username;
@SerializedName("email")
String email;
@SerializedName("password")
String password;
@SerializedName("nombre")
String nombre;
@SerializedName("apellido")
String apellido;
@SerializedName("fechaDeNacimiento")
String fechaDeNacimiento;
@SerializedName("formaDeRegistro")
String formaDeRegistro;
String message;
public Usuarios(String email, String username, String password, String nombre, String apellido, String fechaDeNacimiento, String formaDeRegistro){
this.username=username;
this.email=email;
this.password=password;
this.nombre = nombre;
this.apellido= apellido;
this.fechaDeNacimiento = fechaDeNacimiento;
this.formaDeRegistro = formaDeRegistro;
}
public String getMessage(){
return message;
}
所以我可以看到logcat响应但是在它指定为null的正文中,我如何能够访问&#34;消息&#34;?
答案 0 :(得分:0)
该错误是因为您的响应不成功因此您需要解析错误正文,请尝试使用以下代码:
if (response.isSuccessful()) {
// Do your success stuff...
} else
{
try
{
JSONObject jObjError = new JSONObject(response.errorBody().string());
Toast.makeText(getContext(), jObjError.getString("message"), Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(getContext(), e.getMessage(), Toast.LENGTH_LONG).show();
}
}
当然,您可以使用其他解析器,如Gson。