![](/img/trans.png)
[英]Android/JSON: org.json.JSONException: Value of type java.lang.String cannot be converted to JSONObject
[英]Exception:org.json.JSONException: Value of type java.lang.String cannot be converted to JSONObject in android
API响应:
{
"Response": {
"message": "Success",
"url": "http://makemyreport.com/MobileApps/",
"cor": "DEMO",
"headerMsg": "DEMO",
"subheadMsg": "DEMO"
}
}
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");
记录猫错误:
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();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.