NetworkOnMainThreadException and the Web Component

14 views
Skip to first unread message

Steve Marcus

unread,
Jun 23, 2013, 5:00:28 PM6/23/13
to alternate-java-bridg...@googlegroups.com
Hi,

Having trouble with this exception. It was introduced for Android 3.0+ and it means it will not allow any network activity using Web.Get or Got on the main thread. So the Web component activity needs to be on another thread.

How to get around this? If I put Web.Got in another thread how can I then register the despatchEvent:

 if (component.equals(web1) && eventName.equals(Events.GOT_TEXT)) {
             gotText(args[0].toString(), Convert.Int(args[1]), args[2].toString(), args[3].toString());
             return true;
             }  


It will not work if I try to register the above despatchEvent in another Thread, e.g.

Thread thread = new Thread(new Runnable(){
   @Override
   public void run() {
       try {
           //Your code goes here
                   //How to register a GOT_TEXT event here??
       } catch (Exception e) {
           e.printStackTrace();
       }
   }
});

I tried ThreadTimer too, but if the timer does not go off at exactly the same time as the GOT_TEXT despatchEvent then it will not work, or so it would appear.

Any ideas?

(Potential feature request: Could the Web component automatically be moved to another thread in the bridge jar files?)


S



Brian Cohen

unread,
Jun 24, 2013, 8:16:26 AM6/24/13
to alternate-java-bridg...@googlegroups.com
Hey Steve,
    I am not quite sure I understand what you mean by "It will not work if I try to register the above despatchEvent in another Thread".  You only need to run the code in another thread. 

You should be registering your events like you always do, not in a separate thread.  You simply run the web.get in a separate thread.  When it finishes, it will trigger the dispatch event.

If that doesn't make sense to you (because either i didn't understand what you said, or you don't understand my response), post or send me your full java file and I'll show you what needs to be changed.

Imp Inc

unread,
Jun 24, 2013, 12:39:51 PM6/24/13
to alternate-java-bridg...@googlegroups.com
Or you could try the AltWeb component. That already runs it's requests in a separate thread, then posts the Event in the UI thread.

What Brian suggested will work fine, however it won't post the event in the UI thread, so if you try to touch anything on screen, like a Label for instance, you will get another exception because that code is running in a separate thread. You'd have to call the post method to run it in the UI thrread ex:
post(new Runnable() {
    @Override
    public run() {
        mylabel.Text(webResponse);
    }
});

Ryan

Steve Marcus

unread,
Jun 24, 2013, 3:05:02 PM6/24/13
to alternate-java-bridg...@googlegroups.com
Ah ha! Found the problem! It wasn't the Web component at all. It's an Image component.

In my Web.Got method I was trying to set an image Picture to a URL. This is what is throwing the exception.

In fact, if you create a brand new app and just try to set an Image to a URL in Screen Initialized the exception gets thrown, e.g.

private void screenInitialized() {
    // This method is run when the screen is initialized, and reports a valid width/height. 

    }


Is this expected behaviour?

Steve Marcus

unread,
Jun 24, 2013, 3:07:13 PM6/24/13
to alternate-java-bridg...@googlegroups.com
Oh I didn't even know about the AltWeb component. Will check it out thanks for letting me know.

Imp Inc

unread,
Jun 24, 2013, 3:34:46 PM6/24/13
to alternate-java-bridg...@googlegroups.com
No, it most certainly is not expected behaviour. It's older code from before 3.0 was out. Thanks for finding that one. Would you mind entering an issue for it?

Ryan

Steve Marcus

unread,
Jun 24, 2013, 4:10:33 PM6/24/13
to alternate-java-bridg...@googlegroups.com
Excellent thanks for that, ok issue 177 created!
Reply all
Reply to author
Forward
0 new messages