Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EGit » JGit/Android: prevent NetworkOnMainThreadException during commit
JGit/Android: prevent NetworkOnMainThreadException during commit [message #1212893] Tue, 26 November 2013 22:11 Go to next message
Andrew F is currently offline Andrew F
Messages: 2
Registered: November 2013
Junior Member
If I try to do a api.CommitCommand.call() on the main thread, JGit appears to do a network call to get the hostname, so it can figure out the default email address. Android is a stickler about doing network operations on the UI thread, so it throws a hissy fit (aka NetworkOnMainThreadException) and crashes the app. JGit persists in doing this even after I set the committer and author before calling the command.

So, is it possible to prevent JGit from doing network access while committing? I know I can do the commit in a background task and avoid the issue (and maybe should do that anyway) but right now I'm not sure that's the right solution. It seems silly to have to do a background task just to commit.

My current code. This is in a button handler in the main activity, basically just for playing around:
// create git repository
// the `repo` variable is a related concept specific to my app
// there are already some files in the mentioned RepositoryRoot
FileRepositoryBuilder builder = new FileRepositoryBuilder();
org.eclipse.jgit.lib.Repository gitrepository = builder.setGitDir(new File(repo.getConfig().getRepositoryRoot(), ".git"))
  .readEnvironment() // scan environment GIT_* variables
  .findGitDir() // scan up the file system tree
  .build();
gitrepository.create(false);
// add files and make a commit
Git git = new Git(gitrepository);
AddCommand adder = git.add();
adder.addFilepattern("*");
try {
    adder.call();
} catch (NoFilepatternException e) {
    // this can literally never happen
} catch (GitAPIException e) {
    DialogUtils.QuickToast(this, "failed to add files? the heck?");
}
CommitCommand comm = git.commit();
try {
    comm.setMessage("Initial commit for LynxNotes repository")
        .setAuthor("LynxNotes", "lynxnotes@example.com?")
        .setCommitter("LynxNotes", "lynxnotes@example.com?").call();
} catch (GitAPIException e1) {
    DialogUtils.QuickToast(this, "failed to commit");
}

Stack trace:
FATAL EXCEPTION: main
java.lang.IllegalStateException: Could not execute method of the activity
...snip...
Caused by: android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1133)
at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
at java.net.InetAddress.getLocalHost(InetAddress.java:365)
at org.eclipse.jgit.util.SystemReader$1.getHostname(SystemReader.java:109)
at org.eclipse.jgit.lib.UserConfig.getDefaultEmail(UserConfig.java:217)
at org.eclipse.jgit.lib.UserConfig.<init>(UserConfig.java:84)
at org.eclipse.jgit.lib.UserConfig.<init>(UserConfig.java:52)
at org.eclipse.jgit.lib.UserConfig$1.parse(UserConfig.java:56)
at org.eclipse.jgit.lib.UserConfig$1.parse(UserConfig.java:54)
at org.eclipse.jgit.lib.Config.get(Config.java:547)
at org.eclipse.jgit.lib.PersonIdent.<init>(PersonIdent.java:81)
at org.eclipse.jgit.internal.storage.file.ReflogWriter.log(ReflogWriter.java:217)
at org.eclipse.jgit.internal.storage.file.RefDirectory.log(RefDirectory.java:704)
at org.eclipse.jgit.internal.storage.file.RefDirectoryUpdate.doUpdate(RefDirectoryUpdate.java:120)
at org.eclipse.jgit.lib.RefUpdate$1.execute(RefUpdate.java:491)
at org.eclipse.jgit.lib.RefUpdate.updateImpl(RefUpdate.java:616)
at org.eclipse.jgit.lib.RefUpdate.update(RefUpdate.java:486)
at org.eclipse.jgit.lib.RefUpdate.update(RefUpdate.java:465)
at org.eclipse.jgit.lib.RefUpdate.forceUpdate(RefUpdate.java:444)
at org.eclipse.jgit.api.CommitCommand.call(CommitCommand.java:242)
at com.scrivulet.lynxnotes.MainActivity.onCreateRepoButton(MainActivity.java:69)
... 14 more

Re: JGit/Android: prevent NetworkOnMainThreadException during commit [message #1216700 is a reply to message #1212893] Thu, 28 November 2013 10:43 Go to previous messageGo to next message
Matthias Sohn is currently offline Matthias Sohn
Messages: 556
Registered: July 2009
Senior Member
Looks like we need a way to suppress this lookup and we shouldn't do this lookup when emails are provided.
Fixes welcome (http://wiki.eclipse.org/EGit/Contributor_Guide Smile

Re: JGit/Android: prevent NetworkOnMainThreadException during commit [message #1217391 is a reply to message #1216700] Thu, 28 November 2013 17:33 Go to previous message
Andrew F is currently offline Andrew F
Messages: 2
Registered: November 2013
Junior Member
Thanks for clearing it up. I probably won't be able to fix it myself for a while, at least. Confused
Previous Topic:import an existing project from remote repository
Next Topic:Compare with working tree
Goto Forum:
  


Current Time: Fri Aug 29 08:31:07 EDT 2014

Powered by FUDForum. Page generated in 0.02240 seconds