java - How to prevent Jsoup from throwing exception on http error? -


i using jsoup make app info public transport card cash balance. if user input incorrect card id , app send website, server send http error 409 , output card such id doesn't exist. managed write simple peace of code:

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

which writes string title, because without solution, leave title empty, leads string tokenizer exception in other part of code. after have come question how prevent jsoup throwing exception?

my exception:

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:     @ org.jsoup.helper.httpconnection$response.execute(httpconnection.java:682) 07-27 23:30:12.831 22409-22427/com.bjobs.vvtcards w/system.err:     @ org.jsoup.helper.httpconnection$response.execute(httpconnection.java:629) 07-27 23:30:12.831 22409-22427/com.bjobs.vvtcards w/system.err:     @ org.jsoup.helper.httpconnection.execute(httpconnection.java:261) 07-27 23:30:12.832 22409-22427/com.bjobs.vvtcards w/system.err:     @ com.bjobs.vvtcards.mainactivity$2.run(mainactivity.java:183) 07-27 23:30:12.832 22409-22427/com.bjobs.vvtcards w/system.err:     @ java.lang.thread.run(thread.java:761) 

and full jsoup code, if need it:

 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, 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");                 (int = 0; < 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, 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();             } 

updated code:

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, 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");                 (int = 0; < 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, 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 progress bar             mhandler.post(new runnable() {                 public void run() {                     adapter = new recycleradapter(createlist(cardsnids.size()));                     recyclerview.setadapter(adapter);                 }             });         }     }).start(); } 

and im getting error:

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 invoke interface method 'int org.jsoup.connection$response.statuscode()' on null object reference                                                                         @ com.bjobs.vvtcards.mainactivity$2.run(mainactivity.java:194)                                                                         @ java.lang.thread.run(thread.java:761) 

try this:

catch (httpstatusexception e) {     if (res1 != null && res1.statuscode == 409) {         //handle exception     } } 

Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -