我知道这是旧的或重复的问题。但我没有弄到错的地方。 我从这个JSON得到了结果null。
{
"error": {
"dateTimeUtc": "2017-12-26T05:46:05.1126801+00:00",
"errorReference": "sample string 2",
"errorType": "Error",
"title": "sample string 3",
"code": "sample string 4",
"messages": [
"sample string 1",
"sample string 2"
]
},
"result": {
"message": "sample string 1"
}
}
我有从Retrofit2读取的这种类型的JSON。 我创建了一个名为 RetrofitResponce
的POJO类private Error error;
private Result result;
public Result getResult ()
{
return result;
}
public void setResult (Result result)
{
this.result = result;
}
public Error getError ()
{
return error;
}
public void setError (Error error)
{
this.error = error;
}
@Override
public String toString()
{
return "ClassPojo [result = "+result+", error = "+error+"]";
}`
现在在call.enque()方法中处理响应,比如
call.enqueue(new Callback<RegisterUser>() {
@Override
public void onResponse(Call<RetrofitResponce> call, Response<RetrofitResponce> response) {
try {
RetrofitResponce retrofitResponce= response.body();
Error error = retrofitResponce.getError();
Result result=retrofitResponce.getResult();
Log.e("Eroor", "rr " + error.getTitle().toString());
} catch (Exception e) {
}
}
@Override
public void onFailure(Call<RetrofitResponce> call, Throwable t) {
Log.e("Failure ", "fail " + t.toString());
}
});
但是在这里我得到了NullPointerException,即。我在RestrofitResponce中没有得到任何回复。错误和结果都是空的。
java.lang.NullPointerException:尝试调用虚方法&net;来自net.xyc.abc.Model.Model.RegisterUser.getError()&#39;在空对象引用上
请帮帮我。试着解决我的问题。提前谢谢。
PostMan回复,
{
"error": {
"dateTimeUtc": "2017-12-26T07:05:51.1427712Z",
"errorReference": "00000000-0000-0000-0000-000000000000",
"errorType": "Error",
"title": "The request is invalid.",
"code": null,
"messages": [
"Passwords must have at least one non letter or digit character. Passwords must have at least one uppercase ('A'-'Z')."
]
},
"result": null
}
答案 0 :(得分:3)
您必须在Response类中为每个与Web服务名称相同的字段添加@SerializedName。
e.g
@SerializedName("error")
private Error error;
@SerializedName("result")
private Result result;
请使用此网站从JSON Response创建您的课程。 http://www.jsonschema2pojo.org/
之后你必须检查是否成功回应。
call.enqueue(new Callback<RegisterUser>() {
@Override
public void onResponse(Call<RetrofitResponce> call, Response<RetrofitResponce> response) {
try
{
if(response.isSuccessful() && response.body() != null)
{
RetrofitResponce retrofitResponce= response.body();
Error error = retrofitResponce.getError();
Result result=retrofitResponce.getResult();
}
} catch (Exception e) {
}
}
@Override
public void onFailure(Call<RetrofitResponce> call, Throwable t) {
Log.e("Failure ", "fail " + t.toString());
}
});
答案 1 :(得分:3)
改造回调需要响应模型,只需更改:
str1 <- "23-11-17 12:53:41 PM"
快乐的编码!!
答案 2 :(得分:2)
为RetrofitResponce
public class RetrofitResponce {
@SerializedName("error")
@Expose
private Error error;
@SerializedName("result")
@Expose
private Result result;
public Error getError() {
return error;
}
public void setError(Error error) {
this.error = error;
}
public Result getResult() {
return result;
}
public void setResult(Result result) {
this.result = result;
}
}
public class Result {
@SerializedName("message")
@Expose
private String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
public class Error {
@SerializedName("dateTimeUtc")
@Expose
private String dateTimeUtc;
@SerializedName("errorReference")
@Expose
private String errorReference;
@SerializedName("errorType")
@Expose
private String errorType;
@SerializedName("title")
@Expose
private String title;
@SerializedName("code")
@Expose
private String code;
@SerializedName("messages")
@Expose
private List<String> messages = null;
public String getDateTimeUtc() {
return dateTimeUtc;
}
public void setDateTimeUtc(String dateTimeUtc) {
this.dateTimeUtc = dateTimeUtc;
}
public String getErrorReference() {
return errorReference;
}
public void setErrorReference(String errorReference) {
this.errorReference = errorReference;
}
public String getErrorType() {
return errorType;
}
public void setErrorType(String errorType) {
this.errorType = errorType;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public List<String> getMessages() {
return messages;
}
public void setMessages(List<String> messages) {
this.messages = messages;
}
}
答案 3 :(得分:1)
我相信这会对您有所帮助。
您无法通过Single模型处理Success and Faille。 要获取错误正文,您需要调用response.errorBody() .string ()
尝试以下代码:
call.enqueue(new Callback<RegisterUser>() {
@Override
public void onResponse(Call<RetrofitResponce> call, Response<RetrofitResponce> response) {
try {
if (response.isSuccessful()) {
Log.d("Successful",response.body(););
} else {
Log.d("errorBody ", response.errorBody().string());
}
} catch (Exception e) {
Log.d("Exception ",e.toString());
}
}
@Override
public void onFailure(Call<RetrofitResponce> call, Throwable t) {
Log.e("Failure ", "fail " + t.toString());
}
});
答案 4 :(得分:1)
试试这个。
ffill