Skip to content

Commit

Permalink
Merge pull request square#23 from square/serialize-on-main-thread
Browse files Browse the repository at this point in the history
Serialize HTTP request params on main thread
  • Loading branch information
pforhan committed Mar 26, 2012
2 parents bccb05d + c9f5ec0 commit eddf401
Showing 1 changed file with 19 additions and 13 deletions.
32 changes: 19 additions & 13 deletions modules/http/src/retrofit/http/RestAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,18 +82,6 @@ public static <T> Module service(final Class<T> type) {
private class RestHandler implements InvocationHandler {

@Override public Object invoke(Object proxy, final Method method, final Object[] args) {
// Execute HTTP request in the background.
executor.execute(new Runnable() {
@Override public void run() {
backgroundInvoke(method, args);
}
});

// Methods should return void.
return null;
}

private void backgroundInvoke(Method method, final Object[] args) {
// Construct HTTP request.
final UiCallback<?> callback =
UiCallback.create((Callback<?>) args[args.length - 1], mainThread);
Expand All @@ -115,9 +103,27 @@ private void backgroundInvoke(Method method, final Object[] args) {
GsonResponseHandler.create(gson, resultType, callback);

// Optionally wrap the response handler for server call profiling.
ResponseHandler<Void> rh = (profiler == null) ? gsonResponseHandler
final ResponseHandler<Void> rh = (profiler == null) ? gsonResponseHandler
: createProfiler(gsonResponseHandler, profiler, method, server.apiUrl());

// Execute HTTP request in the background.
executor.execute(new Runnable() {
@Override public void run() {
backgroundInvoke(request, rh, callback);
}
});

} catch (Throwable t) {
logger.log(Level.WARNING, t.getMessage() + " from " + server.apiUrl(), t);
callback.unexpectedError(t);
}

// Methods should return void.
return null;
}

private void backgroundInvoke(HttpUriRequest request, ResponseHandler<Void> rh, UiCallback<?> callback) {
try {
httpClientProvider.get().execute(request, rh);
} catch (IOException e) {
logger.log(Level.WARNING, e.getMessage() + " from " + server.apiUrl(), e);
Expand Down

0 comments on commit eddf401

Please sign in to comment.