I am trying to add another JSONObject as child of the root JSONObject and it's giving me JSONObject. I suppose it's a silly mistake, but still I couldn't figure out the solution till now.
Here's my code -
void sendCallStatusResponse(Message lastMessage) throws JSONException {
String fromName = lastMessage.getFrom();
JSONObject requestRoot = new JSONObject(lastMessage.getBody());
JSONObject callData = requestRoot.getJSONObject("call_data");
String callToStr = callData.getString("call_to");
String serverId = requestRoot.getString("server_id");
long logTime = System.currentTimeMillis();
String lastCallDuration = getLastCallInfoStr();
JSONObject root = new JSONObject();
JSONObject callDataRoot = new JSONObject();
root.put("cmd_id", "callstatus");
root.put("stack_id", "" + logTime);
root.put("server_id", serverId);
root.put("logtime", "" + new Date());
callDataRoot.put("call_to", callToStr);
callDataRoot.put("call_time", lastCallDuration);
callDataRoot.put("ring_time", lastCallDuration);
callDataRoot.put("status", "");
callDataRoot.put("deposition_key", "");
callDataRoot.put("id_campaign", "");
if (SOME_CONDITION)
callDataRoot.put("call_type", "outgoing");
else if (SOME_CONDITION)
callDataRoot.put("call_type", "incoming");
else
callDataRoot.put("call_type", "");
root.put("call_data", callDataRoot); //this is where the exception is thrown
root.put("logtime", "" + logTime);
}
And the exception is -
09-29 12:28:14.837 8785-8785/com.appcall W/System.err﹕ org.json.JSONException: Value recording_file of type java.lang.String cannot be converted to JSONObject
09-29 12:28:14.837 8785-8785/com.appcall W/System.err﹕ at org.json.JSON.typeMismatch(JSON.java:111)
09-29 12:28:14.837 8785-8785/com.appcall W/System.err﹕ at org.json.JSONObject.<init>(JSONObject.java:159)
09-29 12:28:14.837 8785-8785/com.appcall W/System.err﹕ at org.json.JSONObject.<init>(JSONObject.java:172)
09-29 12:28:14.837 8785-8785/com.appcall W/System.err﹕ at com.appcall.XMPPService.sendCallStatusResponse(XMPPService.java:408)
09-29 12:28:14.837 8785-8785/com.appcall W/System.err﹕ at com.appcall.CallOverlay.onRefreshData(CallOverlay.java:91)
09-29 12:28:14.837 8785-8785/com.appcall W/System.err﹕ at com.appcall.XMPPService.sendRefreshData(XMPPService.java:189)
09-29 12:28:14.847 8785-8785/com.appcall W/System.err﹕ at com.appcall.XMPPService.sendExitCode(XMPPService.java:287)
09-29 12:28:14.847 8785-8785/com.appcall W/System.err﹕ at com.appcall.XMPPService.onCallStateChanged(XMPPService.java:572)
09-29 12:28:14.847 8785-8785/com.appcall W/System.err﹕ at com.appcall.PhoneStateMonitor.onCallStateChanged(PhoneStateMonitor.java:127)
09-29 12:28:14.847 8785-8785/com.appcall W/System.err﹕ at android.telephony.PhoneStateListener$2.handleMessage(PhoneStateListener.java:369)
09-29 12:28:14.847 8785-8785/com.appcall W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:102)
09-29 12:28:14.847 8785-8785/com.appcall W/System.err﹕ at android.os.Looper.loop(Looper.java:136)
09-29 12:28:14.847 8785-8785/com.appcall W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5001)
09-29 12:28:14.847 8785-8785/com.appcall W/System.err﹕ at java.lang.reflect.Method.invoke(Native Method)
09-29 12:28:14.847 8785-8785/com.appcall W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
09-29 12:28:14.847 8785-8785/com.appcall W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
I am not sure what is the problem if I use root.put() method to put another JsonObject as child? I've done that before and it's working, but not in this case.
I tried your code snippet starting JSONObject root = new JSONObject();
and replacing values with blank "" strings where needed. I was able to do root.put("call_data", callDataRoot)
without any errors. I verified by printing the root.toString()
at the end. No exceptions were seen. So adding the child JSONObject to another JSONObject as you do above works.
The error is from the values you are using, so make sure you are not getting any spurious characters in the JSON string data returned by the server, or in the values you use to create JSONObject. See this SO question on ways to debug data/strings coming in.