Search code examples
javaandroidexceptionconnectionserversocket

assign default value


        URL test = null;
        String inputLine = null;
        BufferedReader in = null;
        try {
            test = new URL("http://localhost/out.php"); // if connection down
        } catch (MalformedURLException e) {
            inputLine = "test_synntax";
        }
        try {
        in = new BufferedReader(new InputStreamReader(test.openStream()));
        ...

How assign an value for inputline if by default the URL resolution has failed In example context not wifi/3g connection disponible, thanks guys good day


Solution

  • You would need to call URL.openConnection first and then place your default value in the IOException block:

    try {
       test = new URL("http://localhost/out.php");
       URLConnection urlConn = test.openConnection();
       in = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
    } catch (MalformedURLException e) {
        inputLine = "test_synntax";
    } catch (IOException e) {
        inputLine = "test_synntax";
    }