Parsing query strings on Android

前端 未结 25 1305
时光说笑
时光说笑 2020-11-22 17:41

Java EE has ServletRequest.getParameterValues().

On non-EE platforms, URL.getQuery() simply returns a string.

What\'s the normal way to properly parse the qu

25条回答
  •  误落风尘
    2020-11-22 17:54

    On Android, I tried using @diyism answer but I encountered the space character issue raised by @rpetrich, for example: I fill out a form where username = "us+us" and password = "pw pw" causing a URL string to look like:

    http://somewhere?username=us%2Bus&password=pw+pw
    

    However, @diyism code returns "us+us" and "pw+pw", i.e. it doesn't detect the space character. If the URL was rewritten with %20 the space character gets identified:

    http://somewhere?username=us%2Bus&password=pw%20pw
    

    This leads to the following fix:

    Uri uri = Uri.parse(url_string.replace("+", "%20"));
    uri.getQueryParameter("para1");
    

提交回复
热议问题