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.
Errorsclass look like? Make sure it is serializable in a wayObjectMappercan understand. The other two classes included above look ok