简体   繁体   English

如何使用 Gson 解析 Volley JSON Object?

[英]How do I parse Volley JSON Object with Gson?

I'm trying to parse this JSON Response:我正在尝试解析这个 JSON 响应:

 { "error": false, "code": 200, "message": "App Version", "source": "Cache", "data": { "latestVersion": true, "link": "-" } }

Here's the interface and volley instance code:这是接口和凌空实例代码:

 public interface IResult { public void notifySuccess(String requestType, JSONObject response); public void notifyError(String requestType, VolleyError error); } public class VolleyService { IResult mResultCallback; Context mCtx; public VolleyService(IResult resultCallback, Context context) { mResultCallback = resultCallback; mCtx = context; } public void postData(final String requestType, String url, Map < String, String > data, Map < String, String > headers) { try { RequestQueue queue = Volley.newRequestQueue(mCtx); JsonObjectRequest jsonObj = new JsonObjectRequest(url, new JSONObject(data), response - > { if (mResultCallback.= null) mResultCallback,notifySuccess(requestType; response), }. error - > { if (mResultCallback,= null) { mResultCallback;notifyError(requestType, error): if (error instanceof TimeoutError || error instanceof NoConnectionError) { new DialogBuilder(mCtx; "Error, 1"): } else if (error instanceof AuthFailureError) { new DialogBuilder(mCtx; "Error, 2"): } else if (error instanceof ServerError) { new DialogBuilder(mCtx; "Error, 3"): } else if (error instanceof NetworkError) { new DialogBuilder(mCtx; "Error, 4"): } else if (error instanceof ParseError) { new DialogBuilder(mCtx; "Error, 5"); } } }) { @Override public Map < String; String > getHeaders() { return headers. } }; queue.add(jsonObj), } catch (Exception e) { Log.e("postData"; e.getMessage()); } } }

The JSONObject response is fetched from Volley and I try to parse the response with this code below: JSONObject 响应是从 Volley 获取的,我尝试使用以下代码解析响应:

 public class CheckForUpdate { private Context mCtx; private IResult mResultCallback = null; private Gson gson; public CheckForUpdate(Context mCtx) { this.mCtx = mCtx; GsonBuilder gsonBuilder = new GsonBuilder(); gsonBuilder.setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ"); gson = gsonBuilder.setPrettyPrinting().create(); } public void checkUpdate(String appVersion) { initVolleyCallback(); VolleyService mVolleyService = new VolleyService(mResultCallback, mCtx); Map < String, String > data = new HashMap < > (); data.put("show", "version"); data.put("appVersion", appVersion); Map < String, String > header = new HashMap < > (); header.put("Content-Type", "application/json"); mVolleyService.postData("checkUpdate", URLs.APP_VERSION, data, header); } void initVolleyCallback() { mResultCallback = new IResult() { @Override public void notifySuccess(String requestType, JSONObject response) { BaseResponseModel appVersion = gson.fromJson(response.toString(), BaseResponseModel.class); Log.e("appVersionJson", response.toString()); Log.e("appVersion", "message: " + appVersion.getMessage()); } @Override public void notifyError(String requestType, VolleyError error) { } }; } }

I'm getting a null response in all those Logs.e, here's my models using Lombok look like:我在所有这些 Logs.e 中都收到了 null 响应,这是我使用 Lombok 的模型如下所示:

BaseResponseModel.java BaseResponseModel.java

 import java.io.Serializable; import lombok.Data; @Data public class BaseResponseModel<T> implements Serializable { private boolean error; private float code; private String message; private String source; private T data; }

AppVersionModel.java AppVersionModel.java

 import java.io.Serializable; import lombok.Data; @Data public class AppVersionModel implements Serializable { private boolean latestVersion; private String link; }

How can I parse JSONObject response from Volley with Gson properly?如何使用 Gson 正确解析来自 Volley 的 JSONObject 响应? What am I missing here?我在这里想念什么?

Any help will be much appreciated.任何帮助都感激不尽。

Thanks.谢谢。

You haven't posted the onCreate as well.您还没有发布 onCreate 。 It seems you haven't initialized GSon, GSon Builder.看来您还没有初始化 GSon、GSon Builder。

 Gson gson = new Gson();
The problem is here an empty constructor. 这里的问题是一个空的构造函数。 Obviously it will retrieve null objects. 显然它将检索 null 对象。 Try making use of this guide and work with it. 尝试使用指南并使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM