diff --git a/src/main/java/cc/udp/httpjson/HttpJson.java b/src/main/java/cc/udp/httpjson/HttpJson.java index e93f591..4752ca5 100644 --- a/src/main/java/cc/udp/httpjson/HttpJson.java +++ b/src/main/java/cc/udp/httpjson/HttpJson.java @@ -12,6 +12,7 @@ public class HttpJson { private HttpTask httpTask; private HttpJsonTask taskHandler; + private HttpJsonTaskError taskHandlerError; public HttpJson(String url, HashMap params, HttpJsonTask func) { @@ -22,6 +23,13 @@ public void func(Object stringBuilder) { taskDone(stringBuilder.toString()); } }); + this.httpTask.setUncaughtExceptionHandler(new ThreadExceptionHandler(this)); + } + + public HttpJson setThreadExceptionHandler(HttpJsonTaskError funcCatch) + { + this.taskHandlerError = funcCatch; + return this; } public void post() @@ -36,6 +44,12 @@ public void get() this.httpTask.start(); } + public void threadErrorException() + { + if (this.taskHandlerError == null) return; + this.taskHandlerError.error(); + } + private void taskDone(String response) { // If just post call then return @@ -47,12 +61,12 @@ private void taskDone(String response) try { JSONObject root = (JSONObject) jsonParser.parse(response); - taskHandler.done(new HttpJsonObject(root)); + this.taskHandler.done(new HttpJsonObject(root)); } catch (ParseException e) { e.printStackTrace(); - taskHandler.done(new HttpJsonObject(response)); + this.taskHandler.done(new HttpJsonObject(response)); } } diff --git a/src/main/java/cc/udp/httpjson/HttpJsonTaskError.java b/src/main/java/cc/udp/httpjson/HttpJsonTaskError.java new file mode 100644 index 0000000..f20853e --- /dev/null +++ b/src/main/java/cc/udp/httpjson/HttpJsonTaskError.java @@ -0,0 +1,9 @@ +package cc.udp.httpjson; + +/** + * Created by DMDEV on 2018-07-02. + */ + +public interface HttpJsonTaskError { + void error(); +} diff --git a/src/main/java/cc/udp/httpjson/HttpTask.java b/src/main/java/cc/udp/httpjson/HttpTask.java index 6d2dc3a..da076f7 100644 --- a/src/main/java/cc/udp/httpjson/HttpTask.java +++ b/src/main/java/cc/udp/httpjson/HttpTask.java @@ -1,6 +1,7 @@ package cc.udp.httpjson; import java.io.BufferedReader; +import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.UnsupportedEncodingException; @@ -13,6 +14,8 @@ import java.util.Map; import java.util.stream.Collectors; +import javax.management.RuntimeErrorException; + /** * Created by DMDEV on 2017-11-23. */ @@ -89,6 +92,7 @@ public void run() catch (Exception e) { e.printStackTrace(); + throw new RuntimeException(); } } diff --git a/src/main/java/cc/udp/httpjson/ThreadExceptionHandler.java b/src/main/java/cc/udp/httpjson/ThreadExceptionHandler.java new file mode 100644 index 0000000..5c16f8d --- /dev/null +++ b/src/main/java/cc/udp/httpjson/ThreadExceptionHandler.java @@ -0,0 +1,21 @@ +package cc.udp.httpjson; + +/** + * Created by DMDEV on 2018-07-02. + */ + +public class ThreadExceptionHandler implements Thread.UncaughtExceptionHandler +{ + HttpJson httpJson; + + public ThreadExceptionHandler(HttpJson _httpJson) + { + this.httpJson = _httpJson; + } + + @Override + public void uncaughtException(Thread thread, Throwable e) + { + httpJson.threadErrorException(); + } +}