我正在尝试解析像这样的JSON字符串
[
{
"updated_at":"2012-03-02 21:06:01",
"fetched_at":"2012-03-02 21:28:37.728840",
"description":null,
"language":null,
"title":"JOHN",
"url":"http://rus.JOHN.JOHN/rss.php",
"icon_url":null,
"logo_url":null,
"id":"4f4791da203d0c2d76000035",
"modified":"2012-03-02 23:28:58.840076"
},
{
"updated_at":"2012-03-02 14:07:44",
"fetched_at":"2012-03-02 21:28:37.033108",
"description":null,
"language":null,
"title":"PETER",
"url":"http://PETER.PETER.lv/rss.php",
"icon_url":null,
"logo_url":null,
"id":"4f476f61203d0c2d89000253",
"modified":"2012-03-02 23:28:57.928001"
}
]
Run Code Online (Sandbox Code Playgroud)
进入一个对象列表.
List<ChannelSearchEnum> lcs = (List<ChannelSearchEnum>) new Gson().fromJson( jstring , ChannelSearchEnum.class);
Run Code Online (Sandbox Code Playgroud)
这是我正在使用的对象类.
import com.google.gson.annotations.SerializedName;
public class ChannelSearchEnum {
@SerializedName("updated_at")
private String updated_at;
@SerializedName("fetched_at")
private String fetched_at;
@SerializedName("description")
private String description;
@SerializedName("language")
private String language;
@SerializedName("title")
private String title;
@SerializedName("url")
private String url;
@SerializedName("icon_url")
private String icon_url;
@SerializedName("logo_url")
private …Run Code Online (Sandbox Code Playgroud) 我想在Retrofit 2.0中处理错误
得到eg code=404和body=null,但errorBody()包含ErrorModel(Boolean status和String info)中的数据.
这是errorBody().content:[text=\n{"status":false,"info":"Provided email doesn't exist."}].
我怎样才能获得这些数据?
谢谢你的帮助!
这是我的Retrofit请求代码:
ResetPasswordApi.Factory.getInstance().resetPassword(loginEditText.getText().toString())
.enqueue(new Callback<StatusInfoModel>() {
@Override
public void onResponse(Call<StatusInfoModel> call, Response<StatusInfoModel> response) {
if (response.isSuccessful()) {
showToast(getApplicationContext(), getString(R.string.new_password_sent));
} else {
showToast(getApplicationContext(), getString(R.string.email_not_exist));
}
}
@Override
public void onFailure(Call<StatusInfoModel> call, Throwable t) {
showToast(getApplicationContext(), "Something went wrong...");
}
});
Run Code Online (Sandbox Code Playgroud) 我在我的Android项目中使用Retrofit 2.当我使用GET方法命中API端点并返回400级错误时,我可以在使用HttpLoggingInterceptor时看到错误内容,但是当我到达Retrofit OnResponse回调时,错误正文的字符串为空.
我可以看到错误有一个体,但是在Retrofit回调的上下文中我似乎无法拉出那个体.有没有办法确保身体在那里可以进入?
谢谢,亚当
编辑:我从服务器看到的响应是:{"error":{"errorMessage":"对于输入字符串:\"000001280_713870281 \"","httpStatus":400}}
我试图通过以下方式从响应中获取响应:
BaseResponse baseResponse = GsonHelper.getObject(BaseResponse.class, response.errorBody().string());
if (baseResponse != null && !TextUtils.isEmpty(baseResponse.getErrorMessage()))
error = baseResponse.getErrorMessage();
(GsonHelper只是一个帮助器,通过GSON传递JSON字符串来拉出类型的对象BaseResponse)
对response.errorBody().string()的调用导致IOException:Content-Length和流长度不一致,但我在Log Cat中看到的内容字面上是2行