Sanat Pandey
Sanat Pandey

Reputation: 4103

URISyntaxError when I called request.setURI(new URI(url)) while url is working fine on all browsers

I have a problem that when I call a url with many parameters for JSON response, then it shows an URISyntaxError but the same url is working fine on all browsers. I unable to understand what is going wrong?

The URL is: http://apibeta.highgearmedia.com/v1/vehicles/get-make-models.json?sort=mpg&filter=category&client-id=10030812&from=convertible&signature=QOwiWhG2T47KaQoyUztbag==

Code:

HttpClient client = new DefaultHttpClient();
            HttpGet request = new HttpGet();
            request.setURI(new URI(api_url));

            HttpResponse response = client.execute(request);
            InputStream ips = response.getEntity().getContent();
            BufferedReader buf = new BufferedReader(new InputStreamReader(ips,
                    "UTF-8"));

            StringBuilder sb = new StringBuilder();
            String s;
            while (true) {
                s = buf.readLine();
                if (s == null || s.length() == 0)
                    break;
                sb.append(s);

            }
            buf.close();
            ips.close();
            return sb.toString();

Error:

05-10 23:03:45.326: W/System.err(2227): java.net.URISyntaxException: Illegal character in query at index 161: http://apibeta.highgearmedia.com/v1/vehicles/get-make-models.json?sort=mpg&filter=category&client-id=10030812&from=convertible&signature=QOwiWhG2T47KaQoyUztbag==
05-10 23:03:45.326: W/System.err(2227):     at java.net.URI.validateQuery(URI.java:434)
05-10 23:03:45.326: W/System.err(2227):     at java.net.URI.parseURI(URI.java:340)
05-10 23:03:45.335: W/System.err(2227):     at java.net.URI.<init>(URI.java:72)
05-10 23:03:45.335: W/System.err(2227):     at com.TCC.android.ResearchList.getJsonSring(ResearchList.java:3892)
05-10 23:03:45.335: W/System.err(2227):     at com.TCC.android.ResearchList$67.run(ResearchList.java:4077)

Upvotes: 0

Views: 519

Answers (2)

Akram
Akram

Reputation: 7526

Here is solution if your problem, a function that will remove all invalid characters from the url .Pass your url in this function and you will get a new url with encoded strings.

public static String convertURL(String str) {

    url = null;
    try{
    url = new String(str.trim().replace(" ", "%20").replace("&", "%26")
            .replace(",", "%2c").replace("(", "%28").replace(")", "%29")
            .replace("!", "%21").replace("=", "%3D").replace("<", "%3C")
            .replace(">", "%3E").replace("#", "%23").replace("$", "%24")
            .replace("'", "%27").replace("*", "%2A").replace("-", "%2D")
            .replace(".", "%2E").replace("/", "%2F").replace(":", "%3A")
            .replace(";", "%3B").replace("?", "%3F").replace("@", "%40")
            .replace("[", "%5B").replace("\\", "%5C").replace("]", "%5D")
            .replace("_", "%5F").replace("`", "%60").replace("{", "%7B")
            .replace("|", "%7C").replace("}", "%7D"));
    }catch(Exception e){
        e.printStackTrace();
    }
    return url;
}

Upvotes: 2

user180100
user180100

Reputation:

According to java URI javadoc, and to the underlying RFC (§2.2 and 3.4), = is a reserved char and should be escaped (%3D for =)

By the way, you should use android.net.URI

Upvotes: 0

Related Questions