2424import org .json .JSONException ;
2525import org .json .JSONObject ;
2626
27- import java .util .LinkedList ;
27+ import java .util .Iterator ;
2828import java .util .List ;
29- import java .util .ListIterator ;
29+ import java .util .concurrent . ConcurrentLinkedQueue ;
3030
3131import static com .proxerme .library .connection .ProxerException .ErrorCodes .PROXER ;
3232import static com .proxerme .library .connection .ProxerException .ErrorCodes .UNKNOWN ;
@@ -75,7 +75,8 @@ public String id() {
7575 }
7676 };
7777
78- private static LinkedList <ParseThread > parseThreads = new LinkedList <>();
78+ private static Handler handler = new Handler (Looper .getMainLooper ());
79+ private static ConcurrentLinkedQueue <ParseThread > parseThreads = new ConcurrentLinkedQueue <>();
7980
8081 /**
8182 * Entry point to load News of a specified page.
@@ -131,7 +132,7 @@ public static ConferencesRequest loadConferences(@IntRange(from = 1) int page) {
131132 * @see ProxerTag
132133 */
133134 public static void cancel (@ ConnectionTag int tag ) {
134- ListIterator <ParseThread > iterator = parseThreads .listIterator ();
135+ Iterator <ParseThread > iterator = parseThreads .iterator ();
135136
136137 while (iterator .hasNext ()) {
137138 ParseThread current = iterator .next ();
@@ -220,44 +221,46 @@ public final void execute(@NonNull final ResultCallback<T> callback) {
220221 public void response (Request request , final Response response ,
221222 BridgeException exception ) {
222223 if (exception == null ) {
223- ParseThread parseThread = new ParseThread (getTag (), new Runnable () {
224+ ParseThread parseThread = new ParseThread (getTag (), new ThreadedRunnable () {
224225 @ Override
225226 public void run () {
226227 try {
227- if (response == null ) {
228- new Handler (Looper .getMainLooper ()).post (new Runnable () {
228+ JSONObject json = response .asJsonObject ();
229+
230+ if (json == null ) {
231+ handler .post (new Runnable () {
229232 @ Override
230233 public void run () {
231234 callback .onError (new ProxerException (UNKNOWN ));
232235 }
233236 });
234237 } else {
235- final T result = parse (response . asJsonObject () );
238+ final T result = parse (json );
236239
237- new Handler ( Looper . getMainLooper ()) .post (new Runnable () {
240+ handler .post (new Runnable () {
238241 @ Override
239242 public void run () {
240243 callback .onResult (result );
241244 }
242245 });
243246 }
244247 } catch (final JSONException e ) {
245- new Handler ( Looper . getMainLooper ()) .post (new Runnable () {
248+ handler .post (new Runnable () {
246249 @ Override
247250 public void run () {
248251 callback .onError (ErrorHandler .handleException (e ));
249252 }
250253 });
251254 } catch (final BridgeException e ) {
252- new Handler ( Looper . getMainLooper ()) .post (new Runnable () {
255+ handler .post (new Runnable () {
253256 @ Override
254257 public void run () {
255258 callback .onError (ErrorHandler .handleException (e ));
256259 }
257260 });
258261 }
259262
260- parseThreads .remove (this );
263+ parseThreads .remove (getThread () );
261264 }
262265 });
263266
@@ -285,7 +288,11 @@ public final T executeSynchronized() throws ProxerException {
285288 try {
286289 JSONObject result = buildRequest ().tag (getTag () + 1 ).asJsonObject ();
287290
288- return parse (result );
291+ if (result == null ) {
292+ throw new ProxerException (UNKNOWN );
293+ } else {
294+ return parse (result );
295+ }
289296 } catch (JSONException e ) {
290297 throw ErrorHandler .handleException (e );
291298 } catch (BridgeException e ) {
@@ -424,15 +431,28 @@ private static class ParseThread extends Thread {
424431 @ ConnectionTag
425432 private int tag ;
426433
427- public ParseThread (@ ConnectionTag int tag , Runnable runnable ) {
434+ public ParseThread (@ ConnectionTag int tag , ThreadedRunnable runnable ) {
428435 super (runnable );
429436
430437 this .tag = tag ;
438+ runnable .setThread (this );
431439 }
432440
433441 @ ConnectionTag
434442 public int getTag () {
435443 return tag ;
436444 }
437445 }
446+
447+ private abstract static class ThreadedRunnable implements Runnable {
448+ private ParseThread thread ;
449+
450+ public ParseThread getThread () {
451+ return thread ;
452+ }
453+
454+ public void setThread (ParseThread thread ) {
455+ this .thread = thread ;
456+ }
457+ }
438458}
0 commit comments