Merge "Guard against NullPointerException currently occurring in Volley when a Request is given a url whose host is null."
diff --git a/src/com/android/volley/Request.java b/src/com/android/volley/Request.java
index 0e5912f..53093e6 100644
--- a/src/com/android/volley/Request.java
+++ b/src/com/android/volley/Request.java
@@ -134,7 +134,7 @@
         mErrorListener = listener;
         setRetryPolicy(new DefaultRetryPolicy());
 
-        mDefaultTrafficStatsTag = TextUtils.isEmpty(url) ? 0: Uri.parse(url).getHost().hashCode();
+        mDefaultTrafficStatsTag = findDefaultTrafficStatsTag(url);
     }
 
     /**
@@ -171,6 +171,22 @@
     }
 
     /**
+     * @return The hashcode of the URL's host component, or 0 if there is none.
+     */
+    private static int findDefaultTrafficStatsTag(String url) {
+        if (!TextUtils.isEmpty(url)) {
+            Uri uri = Uri.parse(url);
+            if (uri != null) {
+                String host = uri.getHost();
+                if (host != null) {
+                    return host.hashCode();
+                }
+            }
+        }
+        return 0;
+    }
+
+    /**
      * Sets the retry policy for this request.
      *
      * @return This Request object to allow for chaining.
diff --git a/tests/src/com/android/volley/RequestTest.java b/tests/src/com/android/volley/RequestTest.java
index 213e6cd..69f07e2 100644
--- a/tests/src/com/android/volley/RequestTest.java
+++ b/tests/src/com/android/volley/RequestTest.java
@@ -69,4 +69,32 @@
             return null;
         }
     }
+
+    public void testUrlParsing() {
+        UrlParseRequest nullUrl = new UrlParseRequest(null);
+        assertEquals(0, nullUrl.getTrafficStatsTag());
+        UrlParseRequest emptyUrl = new UrlParseRequest("");
+        assertEquals(0, emptyUrl.getTrafficStatsTag());
+        UrlParseRequest noHost = new UrlParseRequest("http:///");
+        assertEquals(0, noHost.getTrafficStatsTag());
+        UrlParseRequest badProtocol = new UrlParseRequest("bad:http://foo");
+        assertEquals(0, badProtocol.getTrafficStatsTag());
+        UrlParseRequest goodProtocol = new UrlParseRequest("http://foo");
+        assertFalse(0 == goodProtocol.getTrafficStatsTag());
+    }
+
+    private class UrlParseRequest extends Request<Object> {
+        public UrlParseRequest(String url) {
+            super(Request.Method.GET, url, null);
+        }
+
+        @Override
+        protected void deliverResponse(Object response) {
+        }
+
+        @Override
+        protected Response<Object> parseNetworkResponse(NetworkResponse response) {
+            return null;
+        }
+    }
 }