用Java解码URI查询字符串

我需要解码包含查询字符串的URI; 预期的输入/输出行为类似于以下内容:

abstract class URIParser { /** example input: * something?alias=pos&FirstName=Foo+A%26B%3DC&LastName=Bar */ URIParser(String input) { ... } /** should return "something" for the example input */ public String getPath(); /** should return a map * {alias: "pos", FirstName: "Foo+A&B=C", LastName: "Bar"} */ public Map getQuery(); } 

我已经尝试过使用java.net.URI ,但它似乎解码了查询字符串所以在上面的例子中我留下了“alias = pos&FirstName = Foo + A&B = C&LastName = Bar”所以有一个歧义是否“& “是查询分隔符,或者是查询组件中的字符。

编辑:我刚刚尝试了URI.getRawQuery()并且它没有进行编码,所以我可以用&拆分查询字符串,但是我该怎么办? Javascript有decodeURIComponent ,我似乎无法在Java中找到相应的方法。

有什么建议么? 我宁愿不使用任何新库。

请参阅类URLDecoder

使用

 URLDecoder.decode(proxyRequestParam.replace("+", "%2B"), "UTF-8") .replace("%2B", "+") 

模拟decodeURIComponent 。 Java的URLDecoder将加号解码为空格,这不是您想要的,因此您需要替换语句。

警告: 如果原始(pre-x-www-form-urlencoded)包含该字符串,则最后的.replace("%2B", "+") 损坏您的数据,如@xehpuk指出的那样。

 var reqParam = URLDecoder.decode(reqParam, "UTF-8") 

关于+号的问题:

我根据@janb的答案创建了一个包装URLDecoder函数的帮助器类

 import android.net.Uri; import android.support.annotation.Nullable; import android.text.TextUtils; import java.io.UnsupportedEncodingException; import java.net.URLDecoder; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Locale; public class DateDecoder { private static final String KEY_DATE = "datekey"; private static final SimpleDateFormat SIMPLE_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZZZZZ", Locale.US); public static void main(String[] args) throws UnsupportedEncodingException { try { Uri uri = Uri.parse("http://asdf.com?something=12345&" + KEY_DATE +"=2016-12-24T12:00:00+01:00"); System.out.println("parsed date: " + DateDecoder.createDate(uri)); // parsed date: Sat Dec 24 12:00:00 GMT+01:00 2016 } catch (Exception e) { e.printStackTrace(); } } @Nullable public static Date createDate(@Nullable Uri data) { if (data != null) { try { String withPlus = decodeButKeepPlus(KEY_DATE, data.getEncodedQuery()); if (!TextUtils.isEmpty(withPlus)) { return SIMPLE_DATE_FORMAT.parse(withPlus); } } catch (Exception e) { e.printStackTrace(); } } return null; } /** * copied from android.net.Uri.java */ @Nullable public static String decodeButKeepPlus(String encodedKey, String completeEncodedQuery) throws UnsupportedEncodingException { final int length = completeEncodedQuery.length(); int start = 0; do { int nextAmpersand = completeEncodedQuery.indexOf('&', start); int end = nextAmpersand != -1 ? nextAmpersand : length; int separator = completeEncodedQuery.indexOf('=', start); if (separator > end || separator == -1) { separator = end; } if (separator - start == encodedKey.length() && completeEncodedQuery.regionMatches(start, encodedKey, 0, encodedKey.length())) { if (separator == end) { return ""; } else { String encodedValue = completeEncodedQuery.substring(separator + 1, end); if (!TextUtils.isEmpty(encodedValue)) { return URLDecoder.decode(encodedValue.replace("+", "%2B"), "UTF-8").replace("%2B", "+"); } } } // Move start to end of name. if (nextAmpersand != -1) { start = nextAmpersand + 1; } else { break; } } while (true); return null; } }