这是我想出的,它应该适用于其他人。使用哈希映射是个好主意,我将修改我的代码。
public static String parseNamedParam(String name, String token) {
if (name == null || "".equals(name) || token == null || "".equals(token)) {
return null;
}
final String[] strNameValuePairs = token.split("&");
for (String entry : strNameValuePairs) {
final String[] strNameValuePair = entry.split("=");
final String decodedName = URL.decodeQueryString(strNameValuePair[0]);
if (decodedName.equals(name)) {
return URL.decodeQueryString(strNameValuePair[1]);
}
}
return "";
}
和
public static String generateTokenString(List<TokenParam> tokenParams) {
if (tokenParams == null || tokenParams.isEmpty()) {
return "";
}
final StringBuilder sb = new StringBuilder();
for (int i = 0; i < tokenParams.size(); i++) {
final TokenParam tokenParam = tokenParams.get(i);
final String nameEncoded = URL.encodeQueryString(tokenParam.getName());
final String valueEncoded = URL.encodeQueryString(tokenParam.getValue());
if (i != 0) {
sb.append("&");
}
sb.append(nameEncoded);
sb.append("=");
sb.append(valueEncoded);
}
return sb.toString();
}