URI解析错误

时间:2012-10-08 15:39:59

标签: java android uri

我从Android手机将数据作为json发送到应用服务器时出现跟踪错误。但是如果我们从pc浏览器发送数据,应用程序可以正常运行。这是从Android浏览器发送数据时我们必须做的事情吗?

Caused by: java.net.URISyntaxException: Illegal character in query at index 271: http://oracleapps.veeralab.com:8080/emyed-whiteboard/pubsub/27c95d04-9783-447b-b333-bcc7b3bbb6b7/01815406-7699-4c2c-ae4b-4eefdbbcbdf0.topic?X-Atmosphere-tracking-id=0&X-Atmosphere-Framework=1.0&X-Atmosphere-Transport=polling&X-Cache-Date=0&X-Atmosphere-Post-Body=message={%22action%22:%22joinName%22,%22parameters%22:%22usersName%22:%22veera,%20job,%20ram%22,%22timestamp%22:1349724088883,%22user%22:%22job%22,%22whiteboardId%22:%2227c95d04-9783-447b-b333-bcc7b3bbb6b7%22}&jsonpTransport=jQuery180031574026358131124_1349709680844
        at java.net.URI$Parser.fail(URI.java:2829) [rt.jar:1.7.0_05]
        at java.net.URI$Parser.checkChars(URI.java:3002) [rt.jar:1.7.0_05]
        at java.net.URI$Parser.parseHierarchical(URI.java:3092) [rt.jar:1.7.0_05]
        at java.net.URI$Parser.parse(URI.java:3034) [rt.jar:1.7.0_05]
        at java.net.URI.<init>(URI.java:595) [rt.jar:1.7.0_05]
        at java.net.URI.create(URI.java:857) [rt.jar:1.7.0_05]

1 个答案:

答案 0 :(得分:0)

要删除无效字符,请使用URLEncoder或Uri Parsing,如下所示。

从以下信息开始

String BASE_URL = "http://oracleapps.veeralab.com:8080/emyed-whiteboard/pubsub/27c95d04-9783-447b-b333-bcc7b3bbb6b7/01815406-7699-4c2c-ae4b-4eefdbbcbdf0.topic"
String QUERY = "X-Atmosphere-tracking-id=0&X-Atmosphere-Framework=1.0&X-Atmosphere-Transport=polling&X-Cache-Date=0&X-Atmosphere-Post-Body=message={%22action%22:%22joinName%22,%22parameters%22:%22usersName%22:%22veera,%20job,%20ram%22,%22timestamp%22:1349724088883,%22user%22:%22job%22,%22whiteboardId%22:%2227c95d04-9783-447b-b333-bcc7b3bbb6b7%22}&jsonpTransport=jQuery180031574026358131124_1349709680844"

您可以使用URLEncoder

String url = BASE_URL + "?" + URLEncoder.encode(QUERY, "utf-8");

或者您可以使用Uri Parsing

String uri = Uri.parse(BASE_URL)
            .buildUpon()
            .appendQueryParameter("X-Atmosphere-tracking-id", "0")
            .appendQueryParameter("X-Atmosphere-Framework", "1")
            .appendQueryParameter("X-Atmosphere-Post-Body", "message={%22action%22:%22joinName%22,%22parameters%22:%22usersName%22:%22veera,%20job,%20ram%22,%22timestamp%22:1349724088883,%22user%22:%22job%22,%22whiteboardId%22:%2227c95d04-9783-447b-b333-bcc7b3bbb6b7%22}")
            ... //your other params
            .build().toString();

我的建议是使用Uri解析方法来确保不应该编码任何不应该编码的内容,但是你仍然以有效的uri结束。