简体   繁体   中英

Exception:org.json.JSONException: Value of type java.lang.String cannot be converted to JSONObject in android

API Response:

{
  "Response": {
    "message": "Success",
    "url": "http://makemyreport.com/MobileApps/",
    "cor": "DEMO",
    "headerMsg": "DEMO",
    "subheadMsg": "DEMO"
  }
}

Asynctask code:

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 {
    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();              
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM