Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6529377
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T09:37:42+00:00 2026-05-25T09:37:42+00:00

I have set up a socket connection between two droids, and I can send

  • 0

I have set up a socket connection between two droids, and I can send data back and forth with no problem. However, in the server side I have set up a ContentObserver to be notified when there is a change to the contacts.

I get the notification, but when I try to send a message to the client that the contacts have changed, I get a fatal exception. The content observer code is

public class ContactWatcher extends ContentObserver {
   public ContactWatcher( Handler h ) 
   {
      super( h );
   }
   public void onChange(boolean selfChange) 
   { 
   if(!ConnectionManager.devices[0].matches("Null"))
   {
     ConnectionManager.sendMessage(MessageNames.CONTACT_UPDATE, 0);
   }
}

I get to the sendMessage code, but when it tries to write to the socket the program crashes with the LogCat output

09-04 16:00:50.880: ERROR/AndroidRuntime(958): FATAL EXCEPTION: main
09-04 16:00:50.880: ERROR/AndroidRuntime(958): android.os.NetworkOnMainThreadException
09-04 16:00:50.880: ERROR/AndroidRuntime(958):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1077)
09-04 16:00:50.880: ERROR/AndroidRuntime(958):     at dalvik.system.BlockGuard$WrappedNetworkSystem.write(BlockGuard.java:290)
09-04 16:00:50.880: ERROR/AndroidRuntime(958):     at org.apache.harmony.luni.net.PlainSocketImpl.write(PlainSocketImpl.java:462)
09-04 16:00:50.880: ERROR/AndroidRuntime(958):     at org.apache.harmony.luni.net.SocketOutputStream.write(SocketOutputStream.java:55)
09-04 16:00:50.880: ERROR/AndroidRuntime(958):     at java.io.OutputStreamWriter.flush(OutputStreamWriter.java:165)
09-04 16:00:50.880: ERROR/AndroidRuntime(958):     at java.io.BufferedWriter.flush(BufferedWriter.java:129)
09-04 16:00:50.880: ERROR/AndroidRuntime(958):     at java.io.PrintWriter.flush(PrintWriter.java:270)
09-04 16:00:50.880: ERROR/AndroidRuntime(958):     at java.io.PrintWriter.println(PrintWriter.java:491)
09-04 16:00:50.880: ERROR/AndroidRuntime(958):     at java.io.PrintWriter.println(PrintWriter.java:603)
09-04 16:00:50.880: ERROR/AndroidRuntime(958):     at com.gigaset.homepanel.ConnectionManager.sendMessage(ConnectionManager.java:151)
09-04 16:00:50.880: ERROR/AndroidRuntime(958):     at com.gigaset.homepanel.ContactWatcher.onChange(ContactWatcher.java:19)
09-04 16:00:50.880: ERROR/AndroidRuntime(958):     at android.database.ContentObserver$NotificationRunnable.run(ContentObserver.java:43)
09-04 16:00:50.880: ERROR/AndroidRuntime(958):     at android.os.Handler.handleCallback(Handler.java:587)
09-04 16:00:50.880: ERROR/AndroidRuntime(958):     at android.os.Handler.dispatchMessage(Handler.java:92)
09-04 16:00:50.880: ERROR/AndroidRuntime(958):     at android.os.Looper.loop(Looper.java:132)
09-04 16:00:50.880: ERROR/AndroidRuntime(958):     at android.app.ActivityThread.main(ActivityThread.java:4025)
09-04 16:00:50.880: ERROR/AndroidRuntime(958):     at java.lang.reflect.Method.invokeNative(Native Method)
09-04 16:00:50.880: ERROR/AndroidRuntime(958):     at java.lang.reflect.Method.invoke(Method.java:491)
09-04 16:00:50.880: ERROR/AndroidRuntime(958):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
09-04 16:00:50.880: ERROR/AndroidRuntime(958):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
09-04 16:00:50.880: ERROR/AndroidRuntime(958):     at dalvik.system.NativeStart.main(Native Method)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T09:37:43+00:00Added an answer on May 25, 2026 at 9:37 am

    The Android SDK documentation for the NetworkOnMainThreadException class is pretty clear and easy to understand. It states that it is thrown on Honeycomb when you try to perform a network operation on the main UI thread.

    The fix is of course to run the network operation in a background thread. You can associate the ContentObserver with a different thread by passing a handler for that thread when you construct the ContentObserver.

    Alternately, you could directly spawn a new thread, or post a runnable to a handler on a different thread.

    If you really want to run the network operation on the main thread, you should be able to disable that exception via the StrictMode apis (not recommended)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have set up transactional replication between two SQL Servers on different ends of
I have a problem making the link between the underlying socket (in this case,
I have a problem with socket send (or write) function on android. There is
I am trying have non-blocking I/O between a server and client. After the two
I have set of scripts for doing scripted installs. You can use the scripts
I need to develop a client server system where I can have multiple clients
I have this piece of code where a server socket is created and is
I have created socket program in Blackberry. and also set socket to keep alive
Suppose I have two servers and I have set up DNS round robin as
I've got code as follows: var smtpClient = new SmtpClient(my.mail.server); smtpClient.Send(mailmessage); however the send

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.