How to customize java.io.Ioexception error message on Android?

Asked

Viewed 1,887 times

0

When Android makes an external connection and this url is not available it returns an error.

How to customize an error as in the example below:

java.io.IOException: Error response: 401 Service Unavailable

for something like:

Erro: Serviço não dospinível

I hope I was clear on the question.

  • 1

    How is this message being displayed on the screen? Could you post a snippet of the code in which it displays it?

  • The error is at the time of the aacencoder connection. When streaming is not available the above error appears on the screen. multiplayer.playAsync("http://url-do-streaming:8000"); My code looks similar to this: https://code.google.com/p/aacdecoder-android/source/browse/trunk/player/src/com/spoledge/aacplay/ACPlayerActivity.java

1 answer

2


The error is already being customized by the library/method that is producing the IOException. In that case the solution would be to capture this Exception in a block catch() and launch a new IOException with custom text. If you want to customize this text only when the IOException the original contains exactly the text "Error Response: 401 Service Unavailable" you will need to make a comparison of this string with the one that is returned by exception.getMessage() (using equals()).

For example:

try {
    ...
    player.playAsync(url);
    ...
} catch (IOException e) {
    if ("Error response: 401 Service Unavailable".equals(e.getMessage()) {
        throw new IOException("Erro: Serviço não disponível");
    } else {
        throw e;
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.