hengsokchamroeun / javapns

Automatically exported from code.google.com/p/javapns
0 stars 0 forks source link

aps dictionary not automatically created #154

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
/** Construct a Payload object from a JSON-formatted string.
         * If an aps dictionary is not included, one will be created automatically.
     * @param rawJSON a JSON-formatted string (ex: {"aps":{"alert":"Hello World!"}} )
     * @throws JSONException thrown if a exception occurs while parsing the JSON string
     */
    public PushNotificationPayload(String rawJSON) throws JSONException {
        super(rawJSON);
        try {
            JSONObject payload = getPayload();
            this.apsDictionary = payload.getJSONObject("aps");
            if (this.apsDictionary == null) {
                this.apsDictionary = new JSONObject();
                payload.put("aps", this.apsDictionary);
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

This code will not create an aps dictionary if you pass it a simple "{}";
It just catches and prints the following exception, instead of throwing it so 
the caller can handle it.  Its very misleading to throw the same type of 
exception that you swallow.

org.json.JSONException: JSONObject["aps"] not found.
    at org.json.JSONObject.get(JSONObject.java:496)
    at org.json.JSONObject.getJSONObject(JSONObject.java:579)

Original issue reported on code.google.com by Scunderw...@gmail.com on 13 Nov 2012 at 9:11

GoogleCodeExporter commented 8 years ago

Original comment by sype...@gmail.com on 5 Feb 2013 at 7:26

GoogleCodeExporter commented 8 years ago
I have added the same issue once again. Starring this and closing the other 
one. Exception should not be caught here. Its very important that it is handled 
by caller.

Original comment by arun.geo...@gmail.com on 21 Nov 2013 at 1:50

GoogleCodeExporter commented 8 years ago
This one is a no-brainer isn't it? The API describes the behavior the user is 
expecting. The only thing to do it to replace:

this.apsDictionary = payload.getJSONObject("aps");

with:

this.apsDictionary = payload.optJSONObject("aps");

This method won't throw any exception if the element "ape" is not present yet. 
Only the call to the put method should be surrounded by try-catch then. Please 
fix this one.

Original comment by sascha.w...@gmail.com on 23 Jun 2014 at 3:59

GoogleCodeExporter commented 8 years ago
Fixed in r392

Original comment by sype...@gmail.com on 30 Sep 2014 at 4:08