2013-12-18 48 views
0

我試圖用UTF-8,然後啓動browser.Below是我的代碼URL編碼例外

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

我得到一個異常活動不Found.When我刪除/註釋編碼網址編碼行對我來說工作得很好。模式匹配也不起作用。

請在下面幫忙是輸出中

編碼新的URL http%3A%2F%2Fwww.google.com%2F%23output%3Dsearch%26amp%3Bq%3Dnexus

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

輸入網址是http://www.google.com/#output=search&q=nexus

+0

問題是我的網址不固定。我只是舉了一個例子的URL.URL可以屬於搜索引擎或任何其他webite.Any有效的網站。 –

回答

1

,而不是與參數,你應該編碼參數值編碼整個URL然後附加在URL爲:

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; 
+0

另外,請參閱此答案以供參考:http://stackoverflow.com/questions/10786042/java-url-encoding/10786112#10786112 – user1277546

+0

問題是我的URL不固定。我只是舉了一個例子URL.URL可以屬於搜索引擎或任何其他webite.Any有效的網站。 –

+0

@AuujKumarJha:好的,然後首先從url中提取基礎URL和參數名稱 - 值對,然後編碼並傳遞給Intent。 –