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? Commented Jul 19, 2013 at 1:34
  • You can use .split() to split them based on various delimiters like / or ? . Is that what you want? Commented Jul 19, 2013 at 1:37
  • @kevinhoo Map is good for me. or getParmKeys(), and getParmVal(parmKey)? Commented Jul 19, 2013 at 1:37
  • you may be you are looking for this:stackoverflow.com/questions/1667278/… Commented Jul 19, 2013 at 1:46
  • @kevinhoo Yes, Before asking, I could not find good answer. Commented 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");
Sign up to request clarification or add additional context in comments.

4 Comments

this is what I want. To use getQueryParameterNames in low api level(<11), we can get method in android open source.
(4.2.2 adroid.net.Uri.getQueryParameterNames() method) grepcode.com/file/repository.grepcode.com/java/ext/…
This is the right answer. Everyone who said anything about split(), myself included, read the question too fast.
@ChangUZ fyi, the grepcode link does not appear to be working/live.
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();
        }

    }

}

Comments

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")

Comments

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() 
};

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.