JAVA : how to extract youtube URL from facebook.com/l.php?u=

余生颓废 提交于 2019-12-25 06:32:20

问题


I m developing an Android app , and , in this step , I want to feed a database with a "Facebook user youtube video request", when a user click in a Youtube video from facebook , an intent catch the URL , but i found that URL is like that : https://m.facebook.com/l.php?u=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DD148dv1G3E4&h=KAQGHuQwG&s=1&enc=AZNGSlqUPqFIDzzjZHddvdQlAkwGkAHJy7YxLMEX7Bfi7-1PE97FOtxHPq73XJ_mKf_Dh50D_YHBxrIiIJ1HnWCbesQO4f19EVtaV-ovXqHnXw

For this original one : https://www.youtube.com/watch?v=D148dv1G3E4

How can I transform with JAVA (regex or other tool) the facebook.com/l.php?u= URL to the www.youtube.com/watch?v= one ? Thanks


回答1:


In this simple example, you could to something like this:

String oldUrl = "https://m.facebook.com/l.php?u=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DD148dv1G3E4&h=KAQGHuQwG&s=1&enc=AZNGSlqUPqFIDzzjZHddvdQlAkwGkAHJy7YxLMEX7Bfi7-1PE97FOtxHPq73XJ_mKf_Dh50D_YHBxrIiIJ1HnWCbesQO4f19EVtaV-ovXqHnXw";
String newUrl;
List args= URLEncodedUtils.parse(oldUrl , Charset.defaultCharset());
for (NameValuePair arg:args) {
    if (arg.getName().equals("u")) {
        newUrl = URLDecoder.decode(arg.getValue(), "UTF-8");
    }
}

First, get the encoded youtube url, then decode it and store it in newUrl.



来源:https://stackoverflow.com/questions/23739237/java-how-to-extract-youtube-url-from-facebook-com-l-phpu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!