如何在安卓系统中从网址获取参数?

2022-08-31 16:48:28

我有一个这样的网址:

http://www.chalklit.in/post.html?chapter=V-Maths-Addition%20&%20Subtraction&post=394

如何获取章节和帖子的参数值?

我的 URL 在章节参数的值中包含“&”。


答案 1

您可以使用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();

然后,您甚至可以从查询参数中获取特定元素;

String chapter = uri.getQueryParameter("chapter");  //will return "V-Maths-Addition "

答案 2

推荐