aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndy Doan <andy.doan@linaro.org>2011-12-16 11:22:47 -0600
committerAndy Doan <andy.doan@linaro.org>2011-12-16 14:50:25 -0600
commitd503d527ffc0ecf2f47e40293039e698f0af0a86 (patch)
tree6fb9b14bf15d0ed42857cc59315688ee9305ee50
parent39fe0b87fd0175c0f5c8c8c04fba84266f7b7d74 (diff)
downloadLinaroConnect-d503d527ffc0ecf2f47e40293039e698f0af0a86.tar.gz
allow optional fields in JSON object
The author field for postings is optional and we were logging exceptions when it failed. This allows us to work around that.
-rw-r--r--src/org/linaro/connect/JSONItem.java21
-rw-r--r--src/org/linaro/connect/JSONPostingItem.java5
2 files changed, 21 insertions, 5 deletions
diff --git a/src/org/linaro/connect/JSONItem.java b/src/org/linaro/connect/JSONItem.java
index e260d55..536a514 100644
--- a/src/org/linaro/connect/JSONItem.java
+++ b/src/org/linaro/connect/JSONItem.java
@@ -16,14 +16,27 @@ public class JSONItem {
mLabel = labelField;
}
- static protected String getField(JSONObject jso, String name) {
+ /**
+ * retrieves a field. if def != null, it will be returned in the event the
+ * field does not exist
+ */
+ static protected String getField(JSONObject jso, String name, String def) {
try {
- return jso.getString(name);
+ def = jso.getString(name);
}
catch(Throwable t) {
- Log.e(LinaroConnect.TAG, "error getting '"+name+"'", t);
+ if (def == null)
+ Log.e(LinaroConnect.TAG, "error getting '"+name+"'", t);
}
- return null;
+ return def;
+ }
+
+ /**
+ * gets a "required" field. this will log an exception if the field does
+ * not exist
+ */
+ static protected String getField(JSONObject jso, String name) {
+ return getField(jso, name, null);
}
static protected String getFieldDecoded(JSONObject jso, String name) {
diff --git a/src/org/linaro/connect/JSONPostingItem.java b/src/org/linaro/connect/JSONPostingItem.java
index c79089b..46c607a 100644
--- a/src/org/linaro/connect/JSONPostingItem.java
+++ b/src/org/linaro/connect/JSONPostingItem.java
@@ -16,7 +16,10 @@ class JSONPostingItem extends JSONItem {
}
public String getAuthor() {
- return getField(mJSO, "author");
+ String val = getField(mJSO, "author", "");
+ if ("".equals(val))
+ val = null;
+ return val;
}
public String getPubDate() {