0

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.

6
  • 2
    I suspect that is the not the current code or expected JSON. Commented Oct 27, 2017 at 5:34
  • 1
    Your code looks like it should be working. Commented Oct 27, 2017 at 5:34
  • 2
    no issue in your code Commented Oct 27, 2017 at 5:44
  • If you guys thinks this code is perfect, then why it is throwing this exception? Commented Oct 27, 2017 at 6:00
  • Did you check the content of response, which is used in JSONObject jsonObject = new JSONObject(response); Commented Oct 27, 2017 at 6:20

1 Answer 1

1

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();              
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.