Click here to Skip to main content
15,896,444 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Friends,

After successful login, I'm trying to download data using REST web service. While parsing I'm getting error : org.json.JSONException: value at CustomerId of type java.lang.String cannot be converted to int
-----------------------
public static void Download_Data(int UserId, String wsUsername, String wsPassword, boolean isDbProd,
Object callBackClass, Method callBackMethod) throws Exception
{
downloadDataCallBackClass=callBackClass;
downloadDataCallBackMethod=callBackMethod;

try {
Method dlGetData=DL.class.getMethod("downloadDataResult",JSONObject.class,Exception.class);

Map<string,> parameters=new HashMap<string,>();
parameters.put("UserId",UserId);
parameters.put("wsUsername",wsUsername);
parameters.put("wsPassword",wsPassword);
parameters.put("IsDbProduction",isDbProd);

new WebService_Call().execute(URL, NAMESPACE, "http://tempuri.org/" + WebMethods.android_GetData.toString(),
WebMethods.android_GetData.toString(), DL.class, dlGetData, parameters);
}
catch (Exception e)
{
throw new Exception("DL.Get_Data():\n" + e.getMessage());
}
}
--------------------------------
public static void downloadDataResult(JSONObject JSO,Exception e)throws Exception {
try {
if (e == null) {
DL.downloadDataInfo = JSO.getJSONArray("Items").getJSONObject(0);
saveItems();
}
downloadDataCallBackMethod.invoke(downloadDataCallBackClass, JSO, e);
} catch (Exception ex) {
ex.printStackTrace();
}
}
--------------------------------
public static void saveItems()
{
DBHelper helper = new DBHelper(context);
ItemDL items=new ItemDL();

//parse JSON DATA...
try {
items.setItemId(DL.downloadDataInfo.getInt("ItemId"));
items.setItemCode(DL.downloadDataInfo.getString("ItemCode"));
items.setCustName(DL.downloadDataInfo.getString("CustomerName"));
items.setCustMobile(DL.downloadDataInfo.getString("CustomerMobile"));
items.setErrMsg(DL.downloadDataInfo.getString("ErrorMsg"));
items.setProcedureId(DL.downloadDataInfo.getInt("ProcedureId"));
items.setStatementId(DL.downloadDataInfo.getInt("StatmentID"));
items.setCustomerId(DL.downloadDataInfo.getInt("CustomerId")); <-- This line of code throws exception
items.setExceptionId(DL.downloadDataInfo.getInt("ExceptionID"));
items.setInserted(DL.downloadDataInfo.getBoolean("IsInserted"));

helper.insertItems(items);
} catch (Exception e) {
e.printStackTrace();
System.out.println("Thrown Exception: "+e.getMessage());
}
}
--------------------------------------
I'm getting Error from this line of code: items.setCustomerId(DL.downloadDataInfo.getInt("CustomerId"));
--------------------------------------
Returned JSON Object:

{"ItemCode":"EE4xyzxyx",
"ItemId":"55452134",
"ExceptionID":"-1",
"ErrorMsgNo":"",
"Lat":"",
"UserID":"6510",
"UserName":"",
"IsInserted":"",
"CustomerName":"",
"CustomerMobile":"",
"PostServiceId":"-1",
"Sp_Status":"",
"CustomerId":"",
"ProcedureId":"198",
"UserFullName":"aasafe",
"ImagePic":"",
"StatmentID":"-1",
"ErrorMsg":"",
"Lon":""}
-----------------------------
Kind check and let me know what I'm doing wrong.

What I have tried:

Android Studio, Error: org.json.JSONException: value at CustomerId of type java.lang.String cannot be converted to int
Posted
Updated 16-Mar-16 23:35pm

1 solution

You are calling getInt to retreive a value that is not an integer. Use getString as indicated by the error message.
 
Share this answer
 
Comments
Dev_android 17-Mar-16 7:04am    
I thought of using getString, but since I was 100% sure that value is integer as web service is developed by me therefore did not use it, but after checking your comment I tried and strangely it worked.
Thank you Richard.
Richard MacCutchan 17-Mar-16 8:42am    
Which proves that whatever you 'think' you may still be proved wrong. The value is definitely not an integer. It may be a string that only contains digits, but that is still a string.
Richard MacCutchan 17-Mar-16 8:48am    
Having looked again at your JSON, it may well be that the problem is your CustomerID contains a null string so the parser thinks it can only be a string. I have not worked with JSON so that is a bit of a guess. I expect some testing could prove it one way or the other.
Richard MacCutchan 17-Mar-16 8:47am    
deleted.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900