Android Volley MalformedURLException 错误的 URL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25130745/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:06:55  来源:igfitidea点击:

Android Volley MalformedURLException Bad URL

androidandroid-volleyandroid-networkingokhttp

提问by Eric Cochran

After making a second network request using Volley, I always get this error. It doesn't seem to matter what the url I put in is. Volleyalways claims it is malformed.

使用 发出第二个网络请求后Volley,我总是收到此错误。我输入的网址似乎并不重要。 Volley总是声称它是畸形的。

08-04 20:16:26.885  14453-14470/com.thredup.android E/Volley﹕ [994] NetworkDispatcher.run: Unhandled exception java.lang.RuntimeException: Bad URL
java.lang.RuntimeException: Bad URL
        at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:127)
        at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:110)
 Caused by: java.net.MalformedURLException: Protocol not found:
        at java.net.URL.<init>(URL.java:176)
        at java.net.URL.<init>(URL.java:125)
        at com.android.volley.toolbox.HurlStack.performRequest(HurlStack.java:101)
        at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:93)

Investigating further, I put a couple logs in HurlStack. In

进一步调查后,我在 HurlStack 中放入了几个日志。在

public HttpResponse performRequest(Request<?> request, Map<String, String> additionalHeaders),

the request that fails is REQUEST [ ] 0x0 LOW 26."

失败的请求是REQUEST [ ] 0x0 LOW 26.

Thus, line 101 of HurlStack : URL parsedUrl = new URL(url);

因此,HurlStack 的第 101 行: URL parsedUrl = new URL(url);

fails with an empty url (request.getUrl()is empty).

失败,网址request.getUrl()为空(为空)。

I am using OkHttpStack(extending HurlStack). Any ideas on what could be causing this?

我正在使用OkHttpStack(扩展 HurlStack)。关于可能导致这种情况的任何想法?

回答by King of Masses

actually the problem is with your url not with the volley. Your Url is not a URI. There is no protocol component in it. It needs http://or whatever other protocol you intend. If you have the http in your url make sure where it is correctly formed or not.

实际上问题在于你的网址而不是凌空抽射。您的网址不是 URI。其中没有协议组件。它需要http://或您想要的任何其他协议。如果您的 url 中有 http,请确保它的格式正确与否。

For example your url formation should be like this

例如你的网址格式应该是这样的

public String URL = "http://www.w3schools.com/webservices/tempconvert.asmx";

Don't forget to read the URL Specificationand make sure the URL you are providing is valid.

不要忘记阅读URL 规范并确保您提供的 URL 有效。

回答by RevanthKrishnaKumar V.

Make Sure that you have passed the URL as the second parameter in JsonObjectRequest or StringRequest. I made the same mistake which produced the same error like what you faced.

确保您已将 URL 作为 JsonObjectRequest 或 StringRequest 中的第二个参数传递。我犯了同样的错误,产生了与您面临的相同的错误。

JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Method.POST, URL, null, ResponseListener, ErrorListener);

回答by Dildarkhan Pathan

Use http:// OR https://

使用 http:// 或 https://

prefix to your URL example: example.com/information.json write it as http://example.com/information.json

URL 示例的前缀:example.com/information.json 将其写为 http://example.com/information.json

回答by John smith

this Exception occur while you are hitting an Urlthat is not prefixed withhttp//or https//.so check there is therehttp//is with your URL. you can get more information hereand seethese links

当您点击Url未以http//或为前缀的https//.so时会发生此异常,因此请检查http//您的 URL是否存在。您可以在此处获取更多信息并查看这些链接