我正在使用Jsoup來製作應用程序,它將獲取有關公共交通卡現金餘額的信息。但是如果用戶輸入錯誤的卡號和我的應用程序發送給網站,服務器將發送http錯誤409,並輸出該卡號不存在。我設法寫的代碼這個簡單的和平:如何防止Jsoup拋出http錯誤的異常?
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 title
,因爲如果沒有這個解決方案,它留給title
空,從而導致字符串標記異常的代碼另一部分。 畢竟這個問題來了如何防止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();
}
更新代碼:
private void checkBalance() {
new Thread(new Runnable() {
@Override
public void run() {
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];
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();
doc1 = res1.parse();
answer = doc1.body();
title = answer.text();
String presentData = cardsNids.get(i);
cardsNids.set(i, presentData + "\n" + title);
}
} catch (HttpStatusException e) {
if (res1.statusCode() == 409) {
title = "Neteisingas kortelės numeris arba nepavyko patikrinti";
}
} catch (IOException e) {
e.printStackTrace();
}
// Update the progress bar
mHandler.post(new Runnable() {
public void run() {
adapter = new RecyclerAdapter(createList(cardsNids.size()));
recyclerView.setAdapter(adapter);
}
});
}
}).start();
}
而現在即時得到另一錯誤:
07-28 20:56:51.328 21205-21242/com.bjobs.vvtcards E/AndroidRuntime: FATAL EXCEPTION: Thread-4
Process: com.bjobs.vvtcards, PID: 21205
java.lang.NullPointerException: Attempt to invoke interface method 'int org.jsoup.Connection$Response.statusCode()' on a null object reference
at com.bjobs.vvtcards.MainActivity$2.run(MainActivity.java:194)
at java.lang.Thread.run(Thread.java:761)
仍然無法運行我的代碼,我張貼在我的問題我更新的代碼和新的錯誤。您的解決方案沒有解決我的問題。即使發生http錯誤,我也需要'try'中的循環來繼續運行 – konor