[英]Exception:org.json.JSONException: Value of type java.lang.String cannot be converted to JSONObject in android
API Response: API响应:
{
"Response": {
"message": "Success",
"url": "http://makemyreport.com/MobileApps/",
"cor": "DEMO",
"headerMsg": "DEMO",
"subheadMsg": "DEMO"
}
}
Asynctask code: Asynctask代码:
JSONObject jsonObject = new JSONObject(response);
JSONObject responseObj = jsonObject.getJSONObject("Response");
message = responseObj.getString("message");
String url = responseObj.getString("url");
orgId = responseObj.getString("cor");
orgName = responseObj.getString("headerMsg");
String subheadMsg = responseObj.getString("subheadMsg");
log cat error: 记录猫错误:
Exception: org.json.JSONException: Value of type java.lang.String cannot be converted to JSONObject
Can anyone help me to solve this problem? 谁能帮我解决这个问题? Thanks in advance. 提前致谢。
Implement it in try catch
block also check your response string if it has expected value: 在try catch
块中实现它,还检查您的响应字符串是否具有期望值:
try {
JSONObject jsonObject = new JSONObject(response);
JSONObject responseObj = jsonObject.getJSONObject("Response");
message = responseObj.getString("message");
String url = responseObj.getString("url");
orgId = responseObj.getString("cor");
orgName = responseObj.getString("headerMsg");
String subheadMsg = responseObj.getString("subheadMsg");
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(), "Error: " + e.getMessage(),
Toast.LENGTH_LONG).show();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.