简体   繁体   English

Exception:org.json.JSONException:java.lang.String类型的值无法在android中转换为JSONObject

[英]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.

相关问题 Android / JSON:org.json.JSONException:类型java.lang.String的值不能转换为JSONObject - Android/JSON: org.json.JSONException: Value of type java.lang.String cannot be converted to JSONObject android错误org.json.JSONException:值 - android error org.json.JSONException: Value <!DOCTYPE of type java.lang.String cannot be converted to JSONObject org.json.JSONException:成功值 - org.json.JSONException: Value success<br of type java.lang.String cannot be converted to JSONObject android org.json.JSONException: 类型 java.lang.String 的值无法转换为 JSONObject android - org.json.JSONException: Value of type java.lang.String cannot be converted to JSONObject android org.json.JSONException:值 - org.json.JSONException: Value <?xml of type java.lang.String cannot be converted to JSONObject in Android studio Android org.json.JSONException:类型java.lang.String的值无法转换为JSONObject - Android org.json.JSONException: Value of type java.lang.String cannot be converted to JSONObject JSON org.json.JSONException:类型为java.lang.String的值错误无法转换为JSONObject - JSON org.json.JSONException: Value Error of type java.lang.String cannot be converted to JSONObject org.json.JSONException:类型java.lang.String的值Dev无法转换为JSONObject - org.json.JSONException: Value Dev of type java.lang.String cannot be converted to JSONObject 解析数据org.json.JSONException时出错:值 - Error parsing data org.json.JSONException: Value <!— of type java.lang.String cannot be converted to JSONObject 错误。 org.json.JSONException:值 - error. org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM