Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Volley MalformedURLException Bad URL

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. Volley always claims it is malformed.

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

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

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

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

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

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

like image 744
Eric Cochran Avatar asked Aug 05 '14 03:08

Eric Cochran


2 Answers

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.

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 Specification and make sure the URL you are providing is valid.

like image 167
King of Masses Avatar answered Nov 15 '22 00:11

King of Masses


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.

JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Method.POST, URL, null, ResponseListener, ErrorListener);
like image 4
RevanthKrishnaKumar V. Avatar answered Nov 15 '22 02:11

RevanthKrishnaKumar V.