Volley给出运行时异常和没有协议的错误URL

时间:2018-08-21 08:06:57

标签: android android-volley

当我使用凌空运行ImageRequest时,它给出了运行时异常,并且没有协议错误。以下是我使用的代码和网址。

ImageRequest ir = new ImageRequest(URL, new Response.Listener<Bitmap>() {
            @Override
            public void onResponse(Bitmap response) {

                musicMetadata.trackArt.add(response);
                if (musicMetadata.trackArt.size() == 10) {
                    extract.setNotify(true);
                }

            }
        }, 0, 0, ImageView.ScaleType.CENTER_CROP, null, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });

URL:

  

https://lastfm-img2.akamaized.net/i/u/64s/4cc18b154dc94f4acf5a1a480d73ab29.png

错误:

08-21 12:40:58.231 6364-6382/com.example.kapiljack.interesting E/Volley: [274] NetworkDispatcher.processRequest: Unhandled exception java.lang.RuntimeException: Bad URL 
java.lang.RuntimeException: Bad URL 
    at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:175)
    at com.android.volley.NetworkDispatcher.processRequest(NetworkDispatcher.java:131)
    at com.android.volley.NetworkDispatcher.processRequest(NetworkDispatcher.java:111)
    at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:90)
 Caused by: java.net.MalformedURLException: no protocol: 
    at java.net.URL.<init>(URL.java:589)
    at java.net.URL.<init>(URL.java:486)
    at java.net.URL.<init>(URL.java:435)
    at com.android.volley.toolbox.HurlStack.executeRequest(HurlStack.java:87)
    at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:123)
    at com.android.volley.NetworkDispatcher.processRequest(NetworkDispatcher.java:131) 
    at com.android.volley.NetworkDispatcher.processRequest(NetworkDispatcher.java:111) 
    at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:90) 
    08-21 12:41:06.807 6364-6384/com.example.kapiljack.interesting D/Volley: [276] BasicNetwork.logSlowRequests: HTTP response for request=<[ ] https://lastfm-img2.akamaized.net/i/u/64s/4cc18b154dc94f4acf5a1a480d73ab29.png 0xe7f9399b LOW 3> [lifetime=9166], [size=9667], [rc=200], [retryCount=1]
    08-21 12:41:06.811 6364-6385/com.example.kapiljack.interesting D/Volley: [277] BasicNetwork.logSlowRequests: HTTP response for request=<[ ] https://lastfm-img2.akamaized.net/i/u/64s/9758f93dd7104585c537e53cfaa084a4.png 0xe7f9399b LOW 4> [lifetime=9168], [size=8999], [rc=200], [retryCount=1]
    08-21 12:41:06.815 6364-6382/com.example.kapiljack.interesting D/Volley: [274] BasicNetwork.logSlowRequests: HTTP response for request=<[ ] https://lastfm-img2.akamaized.net/i/u/64s/4ad5eaab149f4c42af957658b5294998.png 0xe7f9399b LOW 7> [lifetime=8573], [size=9313], [rc=200], [retryCount=1]
    08-21 12:41:07.389 6364-6383/com.example.kapiljack.interesting D/Volley: [275] BasicNetwork.logSlowRequests: HTTP response for request=<[ ] https://lastfm-img2.akamaized.net/i/u/64s/cba44589a043424ab11579be7af8bf39.png 0xe7f9399b LOW 2> [lifetime=9749], [size=9363], [rc=200], [retryCount=1]

PS:我在浏览器中检查了网址,效果很好。

1 个答案:

答案 0 :(得分:0)

问题在于您提供给ImageRequest的URL。如果网址是这样的:

   "https://lastfm-img2.akamaized.net/i/u/64s/4cc18b154dc94f4acf5a1a480d73ab29.png"
   ^                                                                              ^

然后它将找不到任何协议,例如http或https,因此抱怨网址格式错误。 确保您传递的URL的开头和结尾处没有双引号的有效URL。