Gpat Plus
Gpat Plus

Reputation: 9

Android, org.json.JSONException: No value for

I am getting following json from webservice

"alert": { 
    "long_msg": "Thank you for downloading PHARMAPLUS APP, if you found our APP helpful please rate us five star on play store", 
    "short_msg": "Welcome to PHARMAPLUS", 
    "title": "Thank you" 
 } 

and my code is

 if (json.has("alert")) {
     String alert = json.getString("alert");
     String _title = json.getString(Constansts.PARSE.NOTIFICATION.TITLE);
     String _shortMsg = json.getString(Constansts.PARSE.NOTIFICATION.SHORT_MSG);
     String _longMsg = json.getString(Constansts.PARSE.NOTIFICATION.LONG_MSG);

Constants are defined like below

 public static final String TITLE = "title";
 public static final String SHORT_MSG = "short_msg";
 public static final String LONG_MSG = "long_msg";

getting error like below

org.json.JSONException: No value for title

Upvotes: 0

Views: 488

Answers (3)

Ravindra Kushwaha
Ravindra Kushwaha

Reputation: 8042

IF you want to over come from these problem than you have to make check that the json or string exit in the response or not just like

JSONObject jsonObject = new JSONObject(respons);///Here response is your response

if(jsonObject.has("alert"))
{
 JSONObject alert = json.getJSONObject("alert");

 if(alert.has("long_msg"))
 {
  String _longMsg = alert.getString(Constansts.PARSE.NOTIFICATION.LONG_MSG);
 }

if(alert.has("short_msg"))
 {
  String _shortMsg = alert.getString(Constansts.PARSE.NOTIFICATION.SHORT_MSG);
 }

if(alert.has("title"))
 {
  String _title = alert.getString(Constansts.PARSE.NOTIFICATION.TITLE);
 }
}

It will never throw the exception....which are you getting now

Upvotes: 0

Nishant Srivastava
Nishant Srivastava

Reputation: 4791

Geet the JSON under alert first and then under that reference the other string values

JSONObject alert=json.getJSONObject("alert");;
String _title = alert.getString(Constansts.PARSE.NOTIFICATION.TITLE);

Upvotes: 0

Nigam Patro
Nigam Patro

Reputation: 2770

As per the JSON

"alert": { 
   "long_msg": "Thank you for downloading PHARMAPLUS APP, if you found our APP helpful please rate us five star on play store", 
   "short_msg": "Welcome to PHARMAPLUS", 
   "title": "Thank you" 
 }

Here alert is a JSONObject. And the values title, long_msg, short_msg are inside alert

So, you need to modify this part of code

if (json.has("alert")) {
     String alert = json.getString("alert");
     String _title = json.getString(Constansts.PARSE.NOTIFICATION.TITLE);
     String _shortMsg = json.getString(Constansts.PARSE.NOTIFICATION.SHORT_MSG);
     String _longMsg = json.getString(Constansts.PARSE.NOTIFICATION.LONG_MSG);

into

if (json.has("alert")) {
     JSONObject alert = json.getJSONObject("alert");
     String _title = alert.getString(Constansts.PARSE.NOTIFICATION.TITLE);
     String _shortMsg = alert.getString(Constansts.PARSE.NOTIFICATION.SHORT_MSG);
     String _longMsg = alert.getString(Constansts.PARSE.NOTIFICATION.LONG_MSG);

Upvotes: 3

Related Questions