Java 如何从Android中的URL获取参数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40466217/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I get parameters from URL in Android?
提问by Shubham Chauhan
I have a URL like this:
我有一个这样的网址:
http://www.chalklit.in/post.html?chapter=V-Maths-Addition%20&%20Subtraction&post=394
http://www.chalklit.in/post.html?chapter=V-Maths-Addition%20&%20Subtraction&post=394
How to get the value of parameter of chapter and post?
如何获取章节和帖子的参数值?
My URL contains '&' in the value of chapter parameter.
我的 URL 在章节参数的值中包含“&”。
采纳答案by jason.kaisersmith
You can use the Uri class in Android to do this; https://developer.android.com/reference/android/net/Uri.html
您可以使用 Android 中的 Uri 类来执行此操作; https://developer.android.com/reference/android/net/Uri.html
Uri uri = Uri.parse("http://www.chalklit.in/post.html?chapter=V-Maths-Addition%20&%20Subtraction&post=394");
String server = uri.getAuthority();
String path = uri.getPath();
String protocol = uri.getScheme();
Set<String> args = uri.getQueryParameterNames();
Then you can even get a specific element from the query parameters as such;
然后您甚至可以从查询参数中获取特定元素;
String chapter = uri.getQueryParameter("chapter"); //will return "V-Maths-Addition "