TheGraduateGuy
TheGraduateGuy

Reputation: 1520

URL encoding exception

I am trying to encode a URL using UTF-8 and then launch the browser.Below is my code

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

I am getting an exception Activity Not Found.When i remove/comment the encoding line it works fine for me.Pattern matching also not working.

Please help below is the ouput

encoded new url is http%3A%2F%2Fwww.google.com%2F%23output%3Dsearch%26amp%3Bq%3Dnexus

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

Input URL is http://www.google.com/#output=search&q=nexus

Upvotes: 0

Views: 1328

Answers (1)

ρяσѕρєя K
ρяσѕρєя K

Reputation: 132992

instead of encoding whole url with parameters you should encode parameters values then append in url as:

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;

Upvotes: 1

Related Questions