gpt4 book ai didi

java - 如何在 Android 应用程序中解码 URL?

转载 作者:IT老高 更新时间:2023-10-28 20:55:09 27 4
gpt4 key购买 nike

我正在尝试编写一个 Android 应用程序,它应该检索 CDN URL 的内容,这些内容始终是 URLEncoded。例如,我使用的是 youtube URL,因为它们的格式相同。

    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 输出

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 进行解码。

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

请帮我解决这个问题。

最佳答案

你那里的字符串是 URL 编码的。

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

关于java - 如何在 Android 应用程序中解码 URL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8967253/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com