Decoding URI query string in Java
I need to decode a URI that contains a query string; expected input/output behavior is something like the following:
abstract class URIParser
{
/** example input:
* something?alias=pos&FirstName=Foo+A%26B%3DC&LastName=Bar */
URIParser(String input) { ... }
/** should return "something" for t开发者_StackOverflowhe example input */
public String getPath();
/** should return a map
* {alias: "pos", FirstName: "Foo+A&B=C", LastName: "Bar"} */
public Map<String,String> getQuery();
}
I've tried using java.net.URI, but it seems to decode the query string so in the above example I'm left with "alias=pos&FirstName=Foo+A&B=C&LastName=Bar" so there is ambiguity whether a "&" is a query separator or is a character in a query component.
Edit: I just tried URI.getRawQuery() and it doesn't do the encoding, so I can split the query string with a &
, but then what do I do? Javascript has decodeURIComponent, I can't seem to find the corresponding method in Java.
Any suggestions? I would prefer not to use any new libraries.
Use
URLDecoder.decode(proxyRequestParam.replace("+", "%2B"), "UTF-8")
.replace("%2B", "+")
to simulate decodeURIComponent
. Java's URLDecoder
decodes the plus sign to a space, which is not what you want, therefore you need the replace statements.
Warning: the
.replace("%2B", "+")
at the end will corrupt your data if the original (pre-x-www-form-urlencoded) contained that string, as @xehpuk pointed out.
See class URLDecoder
var reqParam = URLDecoder.decode(reqParam, "UTF-8")
Regarding the issue with the + sign :
I made a helper class that wraps the URLDecoder function based on the answer of @janb
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;
}
}
new java.net.URI(proxyRequestParam).getPath()
The string encoded by js encodeURIComponent should just be a path, without schema and other things. However it still a valid input for java.net.URI. So java.net.URI will do everything for us and then the path of it is what we want.
精彩评论