Skip to content Skip to sidebar Skip to footer

How To Parse Or Split Url Address In Java?

If I have url address. https://graph.facebook.com/me/home?limit=25&since=1374196005 Can I get(or split) parameters (avoiding hard coding)? Like this https /// graph.facebook.

Solution 1:

Use Android's Uri class. http://developer.android.com/reference/android/net/Uri.html

Uri uri = Uri.parse("https://graph.facebook.com/me/home?limit=25&since=1374196005");
String protocol = uri.getScheme();
String server = uri.getAuthority();
String path = uri.getPath();
Set<String> args = uri.getQueryParameterNames();
String limit = uri.getQueryParameter("limit");

Solution 2:

For pure Java , I think this code should work:

import java.net.URL;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;

publicclassUrlTest {
    publicstaticvoidmain(String[] args) {
        try {
            Strings="https://graph.facebook.com/me/home?limit=25&since=1374196005";
            URLurl=newURL(s);
            Stringquery= url.getQuery();
            Map<String, String> data = newHashMap<String, String>();
            for (String q : query.split("&")) {
                String[] qa = q.split("=");
                Stringname= URLDecoder.decode(qa[0]);
                Stringvalue="";
                if (qa.length == 2) {
                    value = URLDecoder.decode(qa[1]);
                }

                data.put(name, value);
            }
            System.out.println(data);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}

Solution 3:

In kotlin

https://graph.facebook.com/me/home?limit=25&since=1374196005

import java.net.URI
import android.net.Uri
valuri= URI(viewEvent.qr)
valguid= Uri.parse(uri.fragment).getQueryParameter("c")

Post a Comment for "How To Parse Or Split Url Address In Java?"