21

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.com /// me/home /// {limit=25, sincse=1374196005}

6
  • do you want to parse parameters (limit,since) and their value in a map?
    – kvh
    Jul 19, 2013 at 1:34
  • You can use .split() to split them based on various delimiters like / or ? . Is that what you want? Jul 19, 2013 at 1:37
  • @kevinhoo Map is good for me. or getParmKeys(), and getParmVal(parmKey)?
    – ChangUZ
    Jul 19, 2013 at 1:37
  • you may be you are looking for this:stackoverflow.com/questions/1667278/…
    – kvh
    Jul 19, 2013 at 1:46
  • @kevinhoo Yes, Before asking, I could not find good answer.
    – ChangUZ
    Jul 19, 2013 at 2:14

4 Answers 4

59

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");
4
  • this is what I want. To use getQueryParameterNames in low api level(<11), we can get method in android open source.
    – ChangUZ
    Jul 19, 2013 at 1:49
  • (4.2.2 adroid.net.Uri.getQueryParameterNames() method) grepcode.com/file/repository.grepcode.com/java/ext/…
    – ChangUZ
    Jul 19, 2013 at 1:50
  • This is the right answer. Everyone who said anything about split(), myself included, read the question too fast.
    – Doodad
    Jul 19, 2013 at 1:50
  • 1
    @ChangUZ fyi, the grepcode link does not appear to be working/live.
    – AJW
    Jul 22, 2020 at 13:59
3

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;

public class UrlTest {
    public static void main(String[] args) {
        try {
            String s = "https://graph.facebook.com/me/home?limit=25&since=1374196005";
            URL url = new URL(s);
            String query = url.getQuery();
            Map<String, String> data = new HashMap<String, String>();
            for (String q : query.split("&")) {
                String[] qa = q.split("=");
                String name = URLDecoder.decode(qa[0]);
                String value = "";
                if (qa.length == 2) {
                    value = URLDecoder.decode(qa[1]);
                }

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

    }

}
0

In kotlin

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

import java.net.URI
import android.net.Uri
val uri = URI(viewEvent.qr)
val guid = Uri.parse(uri.fragment).getQueryParameter("c")
0

Built-in java.net.URL should serve.

URL url = new URL("https://graph.facebook.com/me/home?limit=25&since=1374196005");

String[] parts = {
   url.getProtocol(), 
   url.getUserInfo(), 
   url.getHost(), 
   url.getPort(), 
   url.getPath(),
   url.getQuery(), 
   url.getRef() 
};

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.