URISyntaxError当我打电话request.setURI(新的URI(URL)),而URL是在所有的浏览器工作正常 [英] URISyntaxError when I called request.setURI(new URI(url)) while url is working fine on all browsers

查看:154
本文介绍了URISyntaxError当我打电话request.setURI(新的URI(URL)),而URL是在所有的浏览器工作正常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,当我拨打一个网址为JSON响应许多参数,那么它显示了一个URISyntaxError但同样的网址出现在所有的浏览器工作正常。我无法理解到底是怎么回事了?

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

code:

  HttpClient的客户端=新DefaultHttpClient();
            HTTPGET请求=新HTTPGET();
            request.setURI(新的URI(api_url));            HTT presponse响应= client.execute(请求);
            InputStream的IPS = response.getEntity()的getContent()。
            BUF的BufferedReader =新的BufferedReader(新的InputStreamReader(IPS,
                    UTF-8));            StringBuilder的SB =新的StringBuilder();
            字符串s;
            而(真){
                S = buf.readLine();
                如果(S == NULL || s.length()== 0)
                    打破;
                sb.append(多个);            }
            buf.close();
            ips.close();
            返回sb.toString();

错误:

  05-10 23:03:45.326:W / System.err的(2227):java.net.URISyntaxException:索引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):在java.net.URI.validateQuery(URI.java:434)
05-10 23:03:45.326:W / System.err的(2227):在java.net.URI.parseURI(URI.java:340)
05-10 23:03:45.335:W / System.err的(2227):在java.net.URI中的<&初始化GT;(URI.java:72)
05-10 23:03:45.335:W / System.err的(2227):在com.TCC.android.ResearchList.getJsonSring(ResearchList.java:3892)
05-10 23:03:45.335:W / System.err的(2227):在com.TCC.android.ResearchList $ 67.run(ResearchList.java:4077)


解决方案

下面是解决办法,如果你的问题,那将会从URL中删除所有无效字符.Pass您的网址此功能,您将得到一个新的URL与功能EN codeD字符串。

 公共静态字符串convertURL(字符串str){    URL = NULL;
    尝试{
    。URL =新的String(str.trim()代替(,%20)取代(与&,%26)
            .replace(,,%2C)。替换((,%28)。替换(),%29)
            。.replace(!,%21)代替(=,%3D)取代。(&下;,%3C)
            .replace(>中,%3E)取代(#,%23)取代($,%24)。
            。.replace(',%27)代替(*,%2A)代替( - ,%2D)
            。.replace(。,%2E)替换(/,%2F)取代。(:,%3A)
            .replace(;,%3B)替换替换(@,%40)。(%3F,?)。
            .replace([,%5B)。替换(\\\\,5C%)。替换(],%5D)
            .replace(_,%5F)。替换(`,%60)。替换({,%7B)
            。.replace(|,%7C)取代(},%7D));
    }赶上(例外五){
        e.printStackTrace();
    }
    返回URL;
}

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)

解决方案

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;
}

这篇关于URISyntaxError当我打电话request.setURI(新的URI(URL)),而URL是在所有的浏览器工作正常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆