Skip to content

Commit

Permalink
fix network thread
Browse files Browse the repository at this point in the history
  • Loading branch information
adonese committed Aug 25, 2022
1 parent 03bb6a2 commit 3896b5f
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions lib/src/main/java/com/tuti/api/TutiApiClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -208,18 +208,20 @@ public void quickPayment(EBSRequest request, ResponseCallable<PaymentToken> onRe

public Thread sendRequest(RequestMethods method, String URL, Object requestToBeSent, Type ResponseType, Type ErrorType, ResponseCallable onResponse, ErrorCallable onError, Map<String, String> headers, String ...params) {

// // create a runnable to run it in a new thread (so main thread never hangs)
// Runnable runnable = () -> {
if (params != null && params.length > 1) {
URL += "?uuid="+params[1];
}
// create a runnable to run it in a new thread (so main thread never hangs)
String finalURL = URL;
Runnable runnable = () -> {

OkHttpClient okHttpClient = getOkHttpInstance();

Gson gson = getGsonInstance();
RequestBody requestBody = RequestBody.create(gson.toJson(requestToBeSent), JSON);

if (params != null && params.length > 1) {
URL += "?uuid="+params[1];
}
Request.Builder requestBuilder = new Request.Builder().url(URL);

Request.Builder requestBuilder = new Request.Builder().url(finalURL);
if (authToken != null) requestBuilder.header("Authorization", authToken);

//add additional headers set by the user
Expand Down Expand Up @@ -262,11 +264,11 @@ public Thread sendRequest(RequestMethods method, String URL, Object requestToBeS
exception.printStackTrace();
if (onError != null) onError.call(null, exception, null);
}
// };
};

// unit testing concurrent code on multiple threads is hard

Thread thread = new Thread((Runnable) null);
Thread thread = new Thread(runnable);

if (isSingleThreaded) {
thread.run();
Expand Down

0 comments on commit 3896b5f

Please sign in to comment.