简体   繁体   English

如何从URL查询中提取变量为Java中的可读格式?

[英]How to extract variables from url query to readable format in java?

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? 如何以一种可读的方式提取fromto参数?

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));       
    }
}

Output 产量

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. 要更改输出参数的数量,只需编辑params数组。 For example, if you have String[] params = {"x","y"} , the program will output the coordinates (x,y) only 例如,如果您有String[] params = {"x","y"} ,则程序将仅输出坐标(x,y)

I hope that helps you out. 希望对您有所帮助。 Good luck! 祝好运!

Try this: 尝试这个:

    String 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";
    URL urlObject = new URL(url);
    for (String s : urlObject.getQuery().split("&")) {
        String d = URLDecoder.decode(s, "UTF-8");
        if (d.startsWith("from=") || d.startsWith("to=")) {
            int index = d.indexOf('=') + 1;
            System.out.println(d.substring(0, index));
            for (String t : d.substring(index).split(" "))
                System.out.println("  " + t);
        } else
            System.out.println(s);
    }

URLDecoder.decode() is useful. URLDecoder.decode()很有用。 But be careful in case of query string may contain %26 . 但是要小心查询字符串可能包含%26 It is & but not a delimiter. 它是&而不是分隔符。 So you should split("&") and then decode() . 因此,您应该split("&") ,然后decode()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM