1

I have this json string to parse {"uid":8,"totalPoints":"7740"} I have written the below class for this.

public class Points extends WebRequest implements IWebRequest {

    private static final String CLASS_TAG = Points.class.getSimpleName();
    private WebAPIResponse mWebAPIResponse;
    private int mUserId;

    /**
     * Initialize object
     * @param urlEndPoint
     * @param uiDelegate
     * @param appContext
     * @param webServiceRequestCallback
     */
    public Points(String urlEndPoint, IUIDelegate uiDelegate,
            WeakReference<Context> appContext,
            IWebServiceRequestCallback webServiceRequestCallback) {
        super(urlEndPoint, uiDelegate, appContext, webServiceRequestCallback);
    }

    @Override
    public String parseResponse(String responseString) {

        if (MBUtil.isEmpty(responseString)) {
            return "";
        }

        String errMsg = "";

        try {

            ObjectMapper objectMapper = new ObjectMapper();
            objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
            objectMapper.configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true);

            if (!objectMapper.canDeserialize(objectMapper.constructType(WebAPIResponse.class))) {
                return getAppContext().getString(R.string.msg_error_in_reading_format);
            }

            WebAPIResponse webAPIResponse = objectMapper.readValue(responseString, WebAPIResponse.class);
            this.mWebAPIResponse = webAPIResponse;

            Errors errors = webAPIResponse.getErrors();

            if (errors != null) {
                errMsg = errors.getMsg();
            }

        } catch (Exception e) {
            Log.e(CLASS_TAG, e.getMessage());
            errMsg = e.getMessage();
        }
        return errMsg;
    }

    @Override
    public JSONObject buildRequestBody() {

        JSONObject jsonObject = new JSONObject();
        Context context = getAppContext();
        if(context == null) {
            return jsonObject;
        }

        try {

            // Authentication body parameters
            JSONObject authenticationJsonObject = new JSONObject();
            authenticationJsonObject.put(context.getString(R.string.key_points_uid), mUserId);
            return authenticationJsonObject;

        } catch (Exception e) {
            Log.e(CLASS_TAG, e.getMessage());
        }
        return jsonObject;
    }

    public int getUserId() {
        return mUserId;
    }

    public void setUserId(int mUserId) {
        this.mUserId = mUserId;
    }

    public WebAPIResponse getWebAPIResponse() {
        return mWebAPIResponse;
    }

    public void setWebAPIResponse(WebAPIResponse mWebAPIResponse) {
        this.mWebAPIResponse = mWebAPIResponse;
    }

    public static class WebAPIResponse {

        private Data pointsData;
        private Errors errors;

        public Data getPointsData() {
            return pointsData;
        }

        public void setPointsData(Data pointsData) {
            this.pointsData = pointsData;
        }

        public Errors getErrors() {
            return errors;
        }

        public void setErrors(Errors errors) {
            this.errors = errors;
        }
    }

    public static class Data {

        @JsonProperty("uid")
        private int uid;
        @JsonProperty("totalPoints")
        private int totalPoints;

        public int getUid() {
            return uid;
        }

        public void setUid(int uid) {
            this.uid = uid;
        }

        public int getTotalPoints() {
            return totalPoints;
        }

        public void setTotalPoints(int totalPoints) {
            this.totalPoints = totalPoints;
        }
    }
}

I getting proper response in parseResponse() method which is,

responseString = {"uid":8,"totalPoints":"7740"}

But in the same pasreResponse() method once it reached to this line

if (!objectMapper.canDeserialize(objectMapper.constructType(WebAPIResponse.class))) {
                return getAppContext().getString(R.string.msg_error_in_reading_format);
            }

            WebAPIResponse webAPIResponse = objectMapper.readValue(responseString, WebAPIResponse.class);

Not responding any thing and unable to parse the string. Please anyone check whether my parsing class is correct or not and why it is not parsing.

1
  • What does the Errors class look like? Make sure it is serializable in a way ObjectMapper can understand. The other two classes included above look ok Commented Jun 18, 2015 at 9:33

1 Answer 1

2

With your responseString = {"uid":8,"totalPoints":"7740"} you just can deserialize it by Data object only.

Data data = objectMapper.readValue(responseString, Data.class);

If you want to deserialize your JSON String to WebAPIResponse object, your responseString must be:

{"pointsData":{"uid":8,"totalPoints":"7740"}, "errors": ...}
Sign up to request clarification or add additional context in comments.

1 Comment

I did it, but still it is the same issue. Even i debug it the at the same line it is opening DexFile.class in new tab in which showing the message Class File Editor "source not found". What to do for this?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.