Last active
August 29, 2015 14:13
-
-
Save ZakTaccardi/d79594f22d6b4171d220 to your computer and use it in GitHub Desktop.
Encapsulating Google App Engine AsyncTask logic
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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