异常:org.json.JSONException:java.lang.String类型的值无法在android中转换为JSONObject
API响应:异常:org.json.JSONException:java.lang.String类型的值无法在android中转换为JSONObject,android,json,Android,Json,API响应: { "Response": { "message": "Success", "url": "http://makemyreport.com/MobileApps/", "cor": "DEMO", "headerMsg": "DEMO", "subheadMsg": "DEMO" } } 异步任务代码: JSONObject jsonObject = new JSONObject(response); JSONObject resp
{
"Response": {
"message": "Success",
"url": "http://makemyreport.com/MobileApps/",
"cor": "DEMO",
"headerMsg": "DEMO",
"subheadMsg": "DEMO"
}
}
异步任务代码:
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");
日志cat错误:
Exception: org.json.JSONException: Value of type java.lang.String cannot be converted to JSONObject
有人能帮我解决这个问题吗?提前感谢。在
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();
}
我怀疑这不是当前代码或预期的JSON。您的代码看起来应该正常工作。代码中没有问题如果您认为这段代码是完美的,那么它为什么会抛出此异常?您是否检查了
response
的内容,该内容用于JSONObject JSONObject=new JSONObject(response)代码>