如何在Android应用程序中解码URL [英] How do I decode URL in Android Application

查看:167
本文介绍了如何在Android应用程序中解码URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
如何使用Java进行URL解码?

我正在尝试编写一个Android应用,应该检索CDN URL的内容,该内容始终是URLEncoded.举例来说,我使用的是YouTube URL,因为它们的格式相同.

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(
                                connection.getInputStream()));

    StringBuilder response = new StringBuilder();
    String inputLine;

    while ((inputLine = in.readLine()) != null) 
        response.append(inputLine);

    in.close();

    return response.toString();
}

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    try {
    String page = getText("http://www.youtube.com/watch?v=QH2-TGUlwu4");
    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"); **
    System.out.println(page);

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

被加注星标的代码可能是在给我带来麻烦的地方.system.out.println输出

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

http%3A%2F%2Fo-o.preferred.ord08s01.v3.lscache1.c.youtube.com%2Fvideoplayback%3Fsparams%3Did%252Cexpire%252Cip%252Cipbits%252Citag%252Csource%252Cratebypass%252Ccp%26fexp%3D909708%252C907322%252C908613%26itag%3D44%26ip%3D99.0.0.0%26signature%3D2889261E861AF0E724519652960C1BF31DA5BABD.A621580215B6416151470893E2D07BDE7AFF081E%26sver%3D3%26ratebypass%3Dyes%26source%3Dyoutube%26expire%3D1327316557%26key%3Dyt1%26ipbits%3D8%26cp%3DU0hRTFNMVF9KUUNOMV9LRlhGOlNaUzBvTG1tdEp2%26id%3D407dbe4c6525c2ee&quality=large&

我希望对该URL进行解码,以便可以将其包装在一种下载方法中.

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

我已经尝试过string.replaceAll()方法.简而言之,它不起作用

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

请对此提供帮助.

推荐答案

您所拥有的字符串经过URL编码.

The string you have there is URL encoded.

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

这篇关于如何在Android应用程序中解码URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆