Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.6k views
in Technique[技术] by (71.8m points)

java 占位符的问题

package main;
import com.company.MingLuSpider;
import java.io.IOException;
public class Main {
    public static void main(String[] args) throws IOException {
        for (int i = 0; i < 11; i++) {
            if (i == 0) {
                new MingLuSpider().GetRequestData("https://gongshang.mingluji.com/beijing/diqu/%E4%B8%9C%E5%9F%8E", i);
            } else {
                System.out.println(i);
                new MingLuSpider().GetRequestData("https://gongshang.mingluji.com/beijing/diqu/%E4%B8%9C%E5%9F%8E?page=%d",i);
            }
        }
    }
}

请问这是为啥?这个%d占位符 报错
报错如下,数字都打印出来了,但是这个占位符貌似报错了

1
2
3
4
5
6
7
8
9
java.lang.IllegalArgumentException: Malformed escape pair at index 68: https://gongshang.mingluji.com/beijing/diqu/%E4%B8%9C%E5%9F%8E?page=%d
    at java.net.URI.create(URI.java:852)
    at org.apache.http.client.methods.HttpGet.<init>(HttpGet.java:66)
    at com.company.MingLuSpider.GetRequestData(MingLuSpider.java:28)
    at main.Main.main(Main.java:14)
Caused by: java.net.URISyntaxException: Malformed escape pair at index 68: https://gongshang.mingluji.com/beijing/diqu/%E4%B8%9C%E5%9F%8E?page=%d
    at java.net.URI$Parser.fail(URI.java:2848)
    at java.net.URI$Parser.scanEscape(URI.java:2978)
    at java.net.URI$Parser.scan(URI.java:3001)
    at java.net.URI$Parser.checkChars(URI.java:3019)
    at java.net.URI$Parser.parseHierarchical(URI.java:3111)
    at java.net.URI$Parser.parse(URI.java:3053)
    at java.net.URI.<init>(URI.java:588)
    at java.net.URI.create(URI.java:850)
    ... 3 more
java.lang.IllegalArgumentException: Malformed escape pair at index 68: https://gongshang.mingluji.com/beijing/diqu/%E4%B8%9C%E5%9F%8E?page=%d
    at java.net.URI.create(URI.java:852)
    at org.apache.http.client.methods.HttpGet.<init>(HttpGet.java:66)
    at com.company.MingLuSpider.GetRequestData(MingLuSpider.java:28)
    at main.Main.main(Main.java:14)
Caused by: java.net.URISyntaxException: Malformed escape pair at index 68: https://gongshang.mingluji.com/beijing/diqu/%E4%B8%9C%E5%9F%8E?page=%d
    at java.net.URI$Parser.fail(URI.java:2848)
    at java.net.URI$Parser.scanEscape(URI.java:2978)
    at java.net.URI$Parser.scan(URI.java:3001)
    at java.net.URI$Parser.checkChars(URI.java:3019)
    at java.net.URI$Parser.parseHierarchical(URI.java:3111)
    at java.net.URI$Parser.parse(URI.java:3053)
    at java.net.URI.<init>(URI.java:588)
    at java.net.URI.create(URI.java:850)
    ... 3 more
java.lang.IllegalArgumentException: Malformed escape pair at index 68: https://gongshang.mingluji.com/beijing/diqu/%E4%B8%9C%E5%9F%8E?page=%d
    at java.net.URI.create(URI.java:852)
    at org.apache.http.client.methods.HttpGet.<init>(HttpGet.java:66)
    at com.company.MingLuSpider.GetRequestData(MingLuSpider.java:28)
    at main.Main.main(Main.java:14)
Caused by: java.net.URISyntaxException: Malformed escape pair at index 68: https://gongshang.mingluji.com/beijing/diqu/%E4%B8%9C%E5%9F%8E?page=%d
    at java.net.URI$Parser.fail(URI.java:2848)
    at java.net.URI$Parser.scanEscape(URI.java:2978)
    at java.net.URI$Parser.scan(URI.java:3001)
    at java.net.URI$Parser.checkChars(URI.java:3019)
    at java.net.URI$Parser.parseHierarchical(URI.java:3111)
    at java.net.URI$Parser.parse(URI.java:3053)
    at java.net.URI.<init>(URI.java:588)
    at java.net.URI.create(URI.java:850)
    ... 3 more
java.lang.IllegalArgumentException: Malformed escape pair at index 68: https://gongshang.mingluji.com/beijing/diqu/%E4%B8%9C%E5%9F%8E?page=%d
    at java.net.URI.create(URI.java:852)
    at org.apache.http.client.methods.HttpGet.<init>(HttpGet.java:66)
    at com.company.MingLuSpider.GetRequestData(MingLuSpider.java:28)
    at main.Main.main(Main.java:14)
Caused by: java.net.URISyntaxException: Malformed escape pair at index 68: https://gongshang.mingluji.com/beijing/diqu/%E4%B8%9C%E5%9F%8E?page=%d
    at java.net.URI$Parser.fail(URI.java:2848)
    at java.net.URI$Parser.scanEscape(URI.java:2978)
    at java.net.URI$Parser.scan(URI.java:3001)
    at java.net.URI$Parser.checkChars(URI.java:3019)
    at java.net.URI$Parser.parseHierarchical(URI.java:3111)
    at java.net.URI$Parser.parse(URI.java:3053)
    at java.net.URI.<init>(URI.java:588)
    at java.net.URI.create(URI.java:850)
    ... 3 more
java.lang.IllegalArgumentException: Malformed escape pair at index 68: https://gongshang.mingluji.com/beijing/diqu/%E4%B8%9C%E5%9F%8E?page=%d
    at java.net.URI.create(URI.java:852)
    at org.apache.http.client.methods.HttpGet.<init>(HttpGet.java:66)
    at com.company.MingLuSpider.GetRequestData(MingLuSpider.java:28)
    at main.Main.main(Main.java:14)
Caused by: java.net.URISyntaxException: Malformed escape pair at index 68: https://gongshang.mingluji.com/beijing/diqu/%E4%B8%9C%E5%9F%8E?page=%d
    at java.net.URI$Parser.fail(URI.java:2848)
    at java.net.URI$Parser.scanEscape(URI.java:2978)
    at java.net.URI$Parser.scan(URI.java:3001)
    at java.net.URI$Parser.checkChars(URI.java:3019)
    at java.net.URI$Parser.parseHierarchical(URI.java:3111)
    at java.net.URI$Parser.parse(URI.java:3053)
    at java.net.URI.<init>(URI.java:588)
    at java.net.URI.create(URI.java:850)
    ... 3 more
java.lang.IllegalArgumentException: Malformed escape pair at index 68: https://gongshang.mingluji.com/beijing/diqu/%E4%B8%9C%E5%9F%8E?page=%d
    at java.net.URI.create(URI.java:852)
    at org.apache.http.client.methods.HttpGet.<init>(HttpGet.java:66)
    at com.company.MingLuSpider.GetRequestData(MingLuSpider.java:28)
    at main.Main.main(Main.java:14)
Caused by: java.net.URISyntaxException: Malformed escape pair at index 68: https://gongshang.mingluji.com/beijing/diqu/%E4%B8%9C%E5%9F%8E?page=%d
    at java.net.URI$Parser.fail(URI.java:2848)
    at java.net.URI$Parser.scanEscape(URI.java:2978)
    at java.net.URI$Parser.scan(URI.java:3001)
    at java.net.URI$Parser.checkChars(URI.java:3019)
    at java.net.URI$Parser.parseHierarchical(URI.java:3111)
    at java.net.URI$Parser.parse(URI.java:3053)
    at java.net.URI.<init>(URI.java:588)
    at java.net.URI.create(URI.java:850)
    ... 3 more
java.lang.IllegalArgumentException: Malformed escape pair at index 68: https://gongshang.mingluji.com/beijing/diqu/%E4%B8%9C%E5%9F%8E?page=%d
    at java.net.URI.create(URI.java:852)
    at org.apache.http.client.methods.HttpGet.<init>(HttpGet.java:66)
    at com.company.MingLuSpider.GetRequestData(MingLuSpider.java:28)
    at main.Main.main(Main.java:14)
Caused by: java.net.URISyntaxException: Malformed escape pair at index 68: https://gongshang.mingluji.com/beijing/diqu/%E4%B8%9C%E5%9F%8E?page=%d
    at java.net.URI$Parser.fail(URI.java:2848)
    at java.net.URI$Parser.scanEscape(URI.java:2978)
    at java.net.URI$Parser.scan(URI.java:3001)
    at java.net.URI$Parser.checkChars(URI.java:3019)
    at java.net.URI$Parser.parseHierarchical(URI.java:3111)
    at java.net.URI$Parser.parse(URI.java:3053)
    at java.net.URI.<init>(URI.java:588)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

%是URL是URL的转义字符,%d不能直接做为URL的一部分,如果是占位符,那在发起网络请求之前需要替换成真实的参数。


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...