Skip to content

Instantly share code, notes, and snippets.

@ZakTaccardi
Last active August 29, 2015 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ZakTaccardi/d79594f22d6b4171d220 to your computer and use it in GitHub Desktop.
Save ZakTaccardi/d79594f22d6b4171d220 to your computer and use it in GitHub Desktop.
Encapsulating Google App Engine AsyncTask logic
@Override
protected AsyncTaskResult<String> doInBackground(User... users) {
int userCount = users.length;
if(apiService == null) { // Only do this once
UserApi.Builder builder = new UserApi.Builder(AndroidHttp.newCompatibleTransport(),
new AndroidJsonFactory(), null)
// options for running against local devappserver
.setRootUrl("http://" + ServerUtil.getHost() + ":8080/_ah/api/");
//turn off compression when running against local devappserver
Utility.compressTrafficIfNecessary(builder);
// end options for devappserver
apiService = builder.build();
}
try {
for (User u : users)
apiService.insert(u).execute();
return new AsyncTaskResult<String>("success");
} catch (IOException e) {
return new AsyncTaskResult<String>(e);
}
}
import com.google.api.client.googleapis.services.AbstractGoogleClientRequest;
import com.google.api.client.googleapis.services.GoogleClientRequestInitializer;
import com.google.api.client.googleapis.services.json.AbstractGoogleJsonClient.Builder
import java.io.IOException;
/**
* Created by zak on 12/21/14.
*/
public class Utility {
/**
* When working with the local app engine sdk, compression should be disabled. When using production, it should be enabled.
* @param builder the builder of the API object
*/
public static void compressTrafficIfNecessary(Builder builder){
if (BuildConfig.DEBUG)
builder.setGoogleClientRequestInitializer(new GoogleClientRequestInitializer() {
@Override
public void initialize(AbstractGoogleClientRequest<?> abstractGoogleClientRequest) throws IOException {
abstractGoogleClientRequest.setDisableGZipContent(true);
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment