0

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

sifokl
  • 3
  • 7
  • There are several ways to achieve this. Have a look at the answers of the following question: http://stackoverflow.com/questions/10728093/how-can-i-get-this-url-parameter-with-regex – Tobi May 19 '14 at 13:41

1 Answers1

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.

Manuel Allenspach
  • 12,467
  • 14
  • 54
  • 76