I have written some code to do a httpGet and then return the JSON back to the main thread. Sometimes though the server is down and I want to report back to the main thread that the server is down but don’t know how to do it properly using the handler.
My code looks like this:
public class httpGet implements Runnable {
private final Handler replyTo;
private final String url;
public httpGet(Handler replyTo, String url, String path, String params) {
this.replyTo = replyTo;
this.url = url;
}
@Override
public void run() {
try {
// do http stuff //
} catch (ClientProtocolException e) {
Log.e("Uh oh", e);
//how can I report back with the handler about the
//error so I can update the UI
}
}
}
Send a message to the handler, with some error code, for example:
In the handler’s
handleMessageimplementation handle this message.The handler should look like this: