OSDN Git Service

Apollo: fix npe when there is no data in the errorelement.
authorShareef Ali <shareefalis@gmail.com>
Mon, 23 Jun 2014 22:30:23 +0000 (00:30 +0200)
committerJorge Ruesga <jorge@ruesga.com>
Mon, 23 Jun 2014 23:07:48 +0000 (01:07 +0200)
PS5: fix a regression which causes a crash elsewhere.
Change-Id: Ib1464e8ca0e1f61533ce0b0cd172accc21c21ce0
Jira: CYAN-2839

src/com/andrew/apollo/lastfm/Caller.java

index 17859b3..197768a 100644 (file)
@@ -25,9 +25,11 @@ import static com.andrew.apollo.lastfm.StringUtilities.encode;
 import static com.andrew.apollo.lastfm.StringUtilities.map;
 
 import android.content.Context;
+import android.util.Log;
 
 import com.andrew.apollo.lastfm.Result.Status;
 
+import org.apache.http.HttpStatus;
 import org.w3c.dom.Document;
 import org.w3c.dom.Element;
 import org.xml.sax.InputSource;
@@ -65,6 +67,8 @@ import javax.xml.parsers.ParserConfigurationException;
  */
 public class Caller {
 
+    private final static String TAG = "LastFm.Caller";
+
     private final static String PARAM_API_KEY = "api_key";
 
     private final static String DEFAULT_API_ROOT = "http://ws.audioscrobbler.com/2.0/";
@@ -135,18 +139,26 @@ public class Caller {
                             urlConnection.getResponseMessage());
                     return lastResult;
                 }
-            } catch (final IOException ignored) {
+            } catch (final IOException ioEx) {
+                // We will assume that the server is not ready
+                Log.e(TAG, "Failed to download data", ioEx);
+                lastResult = Result.createHttpErrorResult(HttpStatus.SC_SERVICE_UNAVAILABLE,
+                        ioEx.getLocalizedMessage());
+                return lastResult;
             }
         }
 
         try {
             final Result result = createResultFromInputStream(inputStream);
             lastResult = result;
-            return result;
-        } catch (final IOException ignored) {
-        } catch (final SAXException ignored) {
+        } catch (final IOException ioEx) {
+            Log.e(TAG, "Failed to read document", ioEx);
+            lastResult = new Result(ioEx.getLocalizedMessage());
+        } catch (final SAXException saxEx) {
+            Log.e(TAG, "Failed to parse document", saxEx);
+            lastResult = new Result(saxEx.getLocalizedMessage());
         }
-        return null;
+        return lastResult;
     }
 
     /**