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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 22:43:32  来源:igfitidea点击:

How can I get parameters from URL in Android?

javaandroid

提问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 "