-1

JSOUP

when the device internet does not work (or is turned off) exits the program.

but it works without problems (without app crash) while the internet is running.

if the program enters this coded part of the program without the Internet, the program will crash. the program should not crash when I access this coded part without internet. just a toast message that the internet is not available is enough. how can i do?

EDIT:

the problem is when I don't add a toast message.

NONAME
  • 37
  • 7
  • 2
    You are correct. What is the question? – luk2302 Dec 13 '21 at 16:23
  • if the program enters this coded part of the program without the Internet, the program will crash. – NONAME Dec 13 '21 at 16:25
  • Yes, what is the question? – luk2302 Dec 13 '21 at 16:25
  • the program should not crash when I access this coded part without internet. just a toast message that the internet is not available is enough. how can i do? – NONAME Dec 13 '21 at 16:28
  • I updated my question. please help to me this is very important. – NONAME Dec 13 '21 at 16:34
  • Please post the entire stack trace when complaining about a crash. But here it's almost certainly because text1 is null when the network call fails, and you can't toast a null value. Add an if statement to check and don't toast if its null (or toast an error message) – Gabe Sechan Dec 13 '21 at 16:43

1 Answers1

0

If what you're looking for is just a message that the request couldn't be completed normally, it should be enough to change the catch block a bit:

        try {

            Document doc = (Document) Jsoup.connect("(warn: here my web)").get();
            test1 = doc.text();
        } catch (Exception e) {
            // TODO your error message here
            test1 = "An exception occurred and it wasn't possible to get the document";
            // TODO use a logging framework!
            e.printStackTrace();
        }
geco17
  • 5,152
  • 3
  • 21
  • 38