Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/342.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何防止Jsoup在http错误上引发异常?_Java_Android_Jsoup - Fatal编程技术网

Java 如何防止Jsoup在http错误上引发异常?

Java 如何防止Jsoup在http错误上引发异常?,java,android,jsoup,Java,Android,Jsoup,我正在使用Jsoup制作一个应用程序,它将获得有关公共交通卡现金余额的信息。但若用户输入了不正确的卡ID,我的应用程序将其发送到网站,服务器将发送http错误409,并将输出该ID不存在的卡。我成功地编写了这个简单的代码: if (res1.statusCode() == 200) { doc1 = res1.parse(); answer = doc1.body(); title = answer.text(); } else if (res1.statusCode() ==

我正在使用Jsoup制作一个应用程序,它将获得有关公共交通卡现金余额的信息。但若用户输入了不正确的卡ID,我的应用程序将其发送到网站,服务器将发送http错误409,并将输出该ID不存在的卡。我成功地编写了这个简单的代码:

if (res1.statusCode() == 200) {
   doc1 = res1.parse();
   answer = doc1.body();
   title = answer.text();
} else if (res1.statusCode() == 409) {
  title = "Neteisingas kortelės numeris arba nepavyko patikrinti";
}
它将某些内容写入
字符串标题
,因为如果没有此解决方案,它会将
标题
保留为空,从而导致代码的其他部分出现字符串标记器异常。 在所有这些之后,我提出了一个问题:如何防止Jsoup抛出异常

我的例外:

07-27 23:30:12.831 22409-22427/com.bjobs.vvtcards W/System.err: org.jsoup.HttpStatusException: HTTP error fetching URL. Status=409, URL=https://mano.vilniustransport.lt/card/card-check?number=1231234566&_csrf_token=ke_q5dOIIzHCIB5OsuS-N6MlSLXh-im78brCfYn631c
07-27 23:30:12.831 22409-22427/com.bjobs.vvtcards W/System.err:     at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:682)
07-27 23:30:12.831 22409-22427/com.bjobs.vvtcards W/System.err:     at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:629)
07-27 23:30:12.831 22409-22427/com.bjobs.vvtcards W/System.err:     at org.jsoup.helper.HttpConnection.execute(HttpConnection.java:261)
07-27 23:30:12.832 22409-22427/com.bjobs.vvtcards W/System.err:     at com.bjobs.vvtcards.MainActivity$2.run(MainActivity.java:183)
07-27 23:30:12.832 22409-22427/com.bjobs.vvtcards W/System.err:     at java.lang.Thread.run(Thread.java:761)
以及完整的Jsoup代码(如果需要):

 try {
                res = Jsoup.connect("https://mano.vilniustransport.lt/login")
                        .userAgent("Mozilla/5.0 (Linux; Android 7.1.1; LG-D855 Build/NOF26W) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.83 Mobile Safari/537.36")
                        .ignoreHttpErrors(true)
                        .method(Connection.Method.GET)
                        .execute();
                doc = res.parse();
                Map welcomeCookies = res.cookies();
                Element inputHidden = doc.select("input").last();
                //String securityTokenKey = inputHidden.attr("name");
                String securityTokenValue = inputHidden.attr("value");
                for (int i = 0; i < cycleCounter; i++) {
                    String fullData = cardsNids.get(i);
                    String[] split = fullData.split("\n");
                    String cardNu = split[1];
                    Connection.Response res1 = Jsoup.connect("https://mano.vilniustransport.lt/card/card-check?number=" + cardNu + "&_csrf_token=" + securityTokenValue)
                            .header("Content-Type", "text/html; charset=UTF-8")
                            .userAgent("Mozilla/5.0 (Linux; Android 7.1.1; LG-D855 Build/NOF26W) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.83 Mobile Safari/537.36")
                            .cookies(welcomeCookies)
                            .execute();
                    if (res1.statusCode() == 200) {
                        doc1 = res1.parse();
                        answer = doc1.body();
                        title = answer.text();
                    } else if (res1.statusCode() == 409) {
                        title = "Neteisingas kortelės numeris arba nepavyko patikrinti";
                    }
                    String presentData = cardsNids.get(i);
                    cardsNids.set(i, presentData + "\n" + title);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

当Jsoup失败时,它抛出一个您没有捕获的异常-您的代码只捕获一个
IOException
,但在堆栈跟踪中,您有一个
HttpStatusException
。在第一个
catch
子句之后添加第二个
catch
子句:

catch (HttpStatusException e) {
    if (res1.statusCode == 409) {
        //handle the exception
    }
}
试试这个:

catch (HttpStatusException e) {
    if (res1 != null && res1.statusCode == 409) {
        //handle the exception
    }
}

仍然无法运行我的代码,我发布了更新的代码和问题中的新错误。你的解决方案没有解决我的问题。此外,我还需要
try
中的循环来继续运行,即使出现http错误
catch (HttpStatusException e) {
    if (res1.statusCode == 409) {
        //handle the exception
    }
}
catch (HttpStatusException e) {
    if (res1 != null && res1.statusCode == 409) {
        //handle the exception
    }
}