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 7928655
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T19:48:07+00:00 2026-06-03T19:48:07+00:00

I am trying to code a simple chat app for Android so I have

  • 0

I am trying to code a simple chat app for Android so I have a chat server and I am trying to connect to it. So in the main class I have

final EditText etHost = (EditText) findViewById(R.id.entry1);
final EditText etPort = (EditText) findViewById(R.id.entry2);
final EditText messageBoard = (EditText) findViewById(R.id.editText1);
final EditText etSend = (EditText) findViewById(R.id.editText2);

soEd = new SocketAndEditText(etHost, etPort, messageBoard, etSend);
SetNetworking net = new SetNetworking();
SocketAndEditText net.execute(soEd);
final Button sendButton = (Button) findViewById(R.id.button2);

sendButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Messager mes = new Messager();
                mes.execute(soEd);
            }
        });

SocketAndEditText is a class where I simply keep the socket and four EditText components. The idea is that SetNetworking will establish the network connection and Messager will be sending messages. Both classes implement AsyncTask:

public class SetNetworking extends AsyncTask<SocketAndEditText, Void, Void> {
...

public class Messager extends AsyncTask<SocketAndEditText, Void, Void> {
...

But strangely in onPostExecute(Void result) in Messager when I try to do even a quite simple thing as

try {
    InetAddress address1 = InetAddress.getByName("130.237.161.23");
    boolean reachable = address1.isReachable(4456);
    messageBoard.setText("Is host reachable?" + reachable);
} catch (Exception e) {
    messageBoard.setText("Test not working! " + e.toString());
}

I get this:

Test not working! android.os.NetworkOnMainThreadException

Why since it is in the AsyncTask and not in the main thread?

  • 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-06-03T19:48:09+00:00Added an answer on June 3, 2026 at 7:48 pm

    You cannot do network I/O in onPostExecute of an AsyncTask, since that method runs on the UI thread. You need to do all network activity in doInBackground.

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

Sidebar

Related Questions

I'm trying to create my first server, a simple chat server. I have a
I am trying to run the following simple code, public abstract class Shape{ abstract
I have been trying to work on a simple code for two days now.
I am trying to design a simple chat application for android. The concept is,
I am trying to code simple membership class. First class name is Customer and
Trying to code a simple Live wallpaper for android. I'm having trouble finding out
I'm trying to learn android and for my app i have few questions. If
I'm trying to make a simple text chat for example. Server start and work.
I’m trying to make a simple chat server. I had a siisue where the
I'm trying to write a simple client/server chat application in 2 languages - Java

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.