java – How do I decode URL in Android Application-ThrowExceptions

Exception or error:

Possible Duplicate:
How to do URL decoding in Java?

I am trying to write an Android app, and it’s supposed to retrieve the content of a CDN URL, which are always URLEncoded. For the sake of example, I am using a youtube URL, because they are formatted the same way.

    public static String getText(String url) throws Exception {
    URL website = new URL(url);
    URLConnection connection = website.openConnection();
    BufferedReader in = new BufferedReader(
                            new InputStreamReader(

    StringBuilder response = new StringBuilder();
    String inputLine;

    while ((inputLine = in.readLine()) != null) 


    return response.toString();

 * @param args the command line arguments
public static void main(String[] args) {
    try {
    String page = getText("");
    String[] temper = page.split("flashvars=\"");
    page = temper[1];
    temper = page.split("\"     allowscriptaccess=\"always\"");
    page = temper[0];
    temper = page.split("url_encoded_fmt_stream_map=");
    page = temper[1];
    temper = page.split("fallback_host");
    page = temper[0];
    page = page.replaceAll("url%3D", ""); **
    page = URLDecoder.decode(page, "UTF-8"); **
    page = page.replaceAll("hd720&", "hd720"); **

        // TODO code application logic here
    } catch (Exception ex) {
        Logger.getLogger(app.class.getName()).log(Level.SEVERE, null, ex);

The code that is starred is (probably) where it is giving me trouble. The system.out.println outputs

I want that URL decoded in a way that I can wrap it in a method to download it.

I have already tried the string.replaceAll() method. In short, It doesn’t work

Please help me regarding this.

How to solve:

The string you have there is URL encoded.

String afterDecode = URLDecoder.decode(stingvalue, "UTF-8");


using URLDecoder

Leave a Reply

Your email address will not be published. Required fields are marked *