简体   繁体   中英

How do I parse Volley JSON Object with Gson?

I'm trying to parse this JSON Response:

 { "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:

 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:

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

 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? What am I missing here?

Any help will be much appreciated.

Thanks.

You haven't posted the onCreate as well. It seems you haven't initialized GSon, GSon Builder.

 Gson gson = new Gson();
The problem is here an empty constructor. Obviously it will retrieve null objects. Try making use of this guide and work with it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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