I have this url:
http://myhost.com/Request?to=s%3A73746647+d%3Afalse+f%3A-1.0+x%3A-74.454383+y%3A40.843021+r%3A-1.0+cd%3A-1.0+fn%3A-1+tn%3A-1+bd%3Atrue+st%3ACampus%7EDr&returnGeometries=true&nPaths=1&returnClientIds=true&returnInstructions=true&hour=12+00&from=s%3A-1+d%3Afalse+f%3A-1.0+x%3A-74.241765+y%3A40.830182+r%3A-1.0+cd%3A-1.0+fn%3A56481485+tn%3A26459042+bd%3Afalse+st%3AClaremont%7EAve&sameResultType=true
how can I extract the from
and to
arguments in a readable manner?
I have tried the following:
String patternString1 = "(&to=) (.+?) (&returnGeometries) (.+?) (&hour=)"
+" (.+?) (&from=) (.+?) (&sameResultType=)";
Pattern pattern = Pattern.compile(patternString1);
Matcher matcher = pattern.matcher(freshResponse.regression_requestUrl);
while(matcher.find()) {
System.out.println("found: "+matcher.group(1)+" "+matcher.group(3)+matcher.group(4));
}
Pattern pattern = Pattern.compile(patternString1);
Matcher matcher = pattern.matcher(url);
but even if I succeed fetching the correct substrings, how can I convert them to coordinates which I can use to find this place? (in other words: ..such that the coordinates are clean and ready to be used)
This should do what you want:
public class DecodeURL {
public static void main(String[] args) throws UnsupportedEncodingException {
String input = "http://myhost.com/Request?to=s%3A73746647+d%3Afalse+f%3A-1.0+"
+"x%3A-74.454383+y%3A40.843021+r%3A-1.0+cd%3A-1.0+fn%3A-1+tn%3A-1+bd%3A"
+"true+st%3ACampus%7EDr&returnGeometries=true&nPaths=1&returnClientIds="
+"true&returnInstructions=true &hour=12+00&from=s%3A-1+d%3Afalse+f%3A-"
+"1.0+x%3A-74.241765+y%3A40.830182+r%3A-1.0+cd%3A-1.0+fn%3A56481485+tn"
+"%3A26459042+bd%3Afalse+st%3AClaremont%7EAve&sameResultType=true";
String decoded = java.net.URLDecoder.decode(input, "UTF-8").replace("&", " & ");
String[] params = {"s","d","f","x","y","r","cd","fn","tn","bd","st"};
System.out.println("Decoded input URL: \n"+decoded);
// Output all FROM arguments
System.out.println("\nFROM:");
for (int i = 0; i < params.length; i++) {
System.out.println(params[i]+" = \t"+findInstance(decoded, "from", params[i]));
}
// Output all TO arguments
System.out.println("\nTO:");
for (int i = 0; i < params.length; i++) {
System.out.println(params[i]+" = \t"+findInstance(decoded, "to", params[i]));
}
}
public static String findInstance(String input, String type, String match) {
int start = input.indexOf(match+":", input.indexOf(type))+match.length()+1;
return input.substring(start, input.indexOf(" ", start));
}
}
Decoded input URL:
http://myhost.com/Request?to=s:73746647 d:false f:-1.0 x:-74.454383 y:40.843021 r:-1.0 cd:-1.0 fn:-1 tn:-1 bd:true st:Campus~Dr & returnGeometries=true & nPaths=1 & returnClientIds=true & returnInstructions=true & hour=12 00 & from=s:-1 d:false f:-1.0 x:-74.241765 y:40.830182 r:-1.0 cd:-1.0 fn:56481485 tn:26459042 bd:false st:Claremont~Ave & sameResultType=true
FROM:
s = -1
d = false
f = -1.0
x = -74.241765
y = 40.830182
r = -1.0
cd = -1.0
fn = 56481485
tn = 26459042
bd = false
st = Claremont~Ave
TO:
s = 73746647
d = false
f = -1.0
x = -74.454383
y = 40.843021
r = -1.0
cd = -1.0
fn = -1
tn = -1
bd = true
st = Campus~Dr
To change the number of output parameters, simply edit the params
array. For example, if you have String[] params = {"x","y"}
, the program will output the coordinates (x,y) only
I hope that helps you out. Good luck!