20

如果我有 url 地址。

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

我可以获取(或拆分)参数(避免硬编码)吗?

像这样

https /// graph.facebook.com /// me/home /// {limit=25, sincse=1374196005}

4

3 回答 3

56

使用 Android 的Uri类。 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");
于 2013-07-19T01:44:22.513 回答
3

对于纯 Java ,我认为这段代码应该可以工作:

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

    }

}
于 2013-07-19T02:11:42.823 回答
0

在科特林

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")
于 2021-08-26T18:49:06.567 回答