jsoup例外

时间:2012-01-20 19:36:38

标签: java parsing jsoup

我的循环看起来像这样:

//Pseudo code

subUrl = divs.get(n).select("a[href].eDL");

for(int o=0; o<subUrl.size(); o++) {

    subUrlW = subUrl.get(o).select("a[href].eDL").attr("abs:href");

    httpget = new HttpGet(subUrlW);
    HttpResponse httpRes2 = httpclient1.execute(httpget, httpContext);
    html = EntityUtils.toString(httpRes2.getEntity());
    doc = Jsoup.parse(html, subUrlW);
    divs = doc.select("div#pTU");

    for(int p=0; p<divs.size();p++) {

        subUrl = divs.get(p).select("a[href].pd");
        for(int q=0; q<subUrl.size(); q++) {  

             subUrlW = subUrl.get(q).select("a[href].pd").attr("abs:href"); 

        }
    }                                              
}

我看到subUrlW为null但是如果我执行它来调试,则subUrlW不为null。

subUrl = divs.get(n).select("a[href].eDL");

for(int o=0; o<subUrl.size(); o++) {

    subUrlW = subUrl.get(o).select("a[href].eDL").attr("abs:href");
    System.out.println("subUrlW"+subUrlW);

}

当这个循环执行时,我得到以下异常,

java.lang.IllegalStateException: Target host must not be null, or set in parameters.
    at org.apache.http.impl.client.DefaultRequestDirector.determineRoute(DefaultRequestDirector.java:717)
    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:358)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:820)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:754)

然而,他指出了他的路线,

HttpResponse httpRes2 = httpclient1.execute(httpget, httpContext);

任何非常感激的投入。

1 个答案:

答案 0 :(得分:0)

subUrl = divs.get(n).select("a[href].eDL");

Document doc=null;

for(int o=0; o< subUrl.size(); o++) 
{

    doc = JSoup.connect(subUrl[o].toString());
    subUrlW = doc.select("a[href].eDL").attr("abs:href");
    System.out.println("subUrlW"+subUrlW);
    doc=null;

}