如果我有网址。
https://graph.facebook.com/me/home?limit=25&since=1374196005
我可以获取(或拆分)参数(避免硬编码)吗?
喜欢这个
https /// graph.facebook.com /// me / home /// {limit = 25, sincse = 1374196005}
答案 0 :(得分:43)
使用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");
答案 1 :(得分:7)
如上所述,您可以使用.split()。
像这样:
String url = "http://stackoverflow.com/questions/thispost/";
String[] separated = newurl.split("/");
separated[0]; // = http:
separated[1]; // = (nothing, i.e, empty string, `""`)
separated[2]; // = stackoverflow.com
separated[3]; // = questions
separated[4]; // = thispost
答案 2 :(得分:1)
对于纯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();
}
}
}