Skip to content

Commit

Permalink
ThreadExceptionHandler Added
Browse files Browse the repository at this point in the history
  • Loading branch information
setreuid committed Jul 2, 2018
1 parent d4b224a commit ec27cef
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/main/java/cc/udp/httpjson/HttpJson.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class HttpJson
{
private HttpTask httpTask;
private HttpJsonTask taskHandler;
private HttpJsonTaskError taskHandlerError;

public HttpJson(String url, HashMap<String, String> params, HttpJsonTask func)
{
Expand All @@ -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()
Expand All @@ -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
Expand All @@ -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));
}
}

Expand Down
9 changes: 9 additions & 0 deletions src/main/java/cc/udp/httpjson/HttpJsonTaskError.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package cc.udp.httpjson;

/**
* Created by DMDEV on 2018-07-02.
*/

public interface HttpJsonTaskError {
void error();
}
4 changes: 4 additions & 0 deletions src/main/java/cc/udp/httpjson/HttpTask.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -13,6 +14,8 @@
import java.util.Map;
import java.util.stream.Collectors;

import javax.management.RuntimeErrorException;

/**
* Created by DMDEV on 2017-11-23.
*/
Expand Down Expand Up @@ -89,6 +92,7 @@ public void run()
catch (Exception e)
{
e.printStackTrace();
throw new RuntimeException();
}
}

Expand Down
21 changes: 21 additions & 0 deletions src/main/java/cc/udp/httpjson/ThreadExceptionHandler.java
Original file line number Diff line number Diff line change
@@ -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();
}
}

0 comments on commit ec27cef

Please sign in to comment.