URL编码异常

时间:2013-12-18 09:04:12

标签: java android url android-intent encoding

我正在尝试使用 UTF-8 对网址进行编码,然后启动浏览器。这是我的代码

if(URLUtil.isValidUrl(url)){
                            //if(Patterns.WEB_URL.matcher(url).matches())   {

url = URLEncoder.encode(url,"UTF-8");
Intent launchUrl = new Intent(Intent.ACTION_VIEW, Uri.parse(url));

System.out.println("encoded new url is "+url);

System.out.println(" launurl is "+launchUrl);

mContext.startActivity(launchUrl);
}

我收到一个异常活动未找到。当我删除/评论编码行时,它对我来说很好。模式匹配也不起作用。

请帮助以下是输出

已编码的新网址为http%3A%2F%2Fwww.google.com%2F%23output%3Dsearch%26amp%3Bq%3Dnexus

opensurl是Intent { act=android.intent.action.VIEW dat=http://www.google.com/#output=search&q=nexus

输入网址为http://www.google.com/#output=search&q=nexus

1 个答案:

答案 0 :(得分:1)

而不是使用参数对整个网址进行编码,您应该对参数值进行编码,然后将其附加到网址中:

String str_url= "http://www.google.com/";

String str_parms="#output="+URLEncoder.encode("search","UTF-8")+
                                   "&q="+URLEncoder.encode("nexus","UTF-8");

String str_final_url=str_url+str_parms;