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

  • Home
  • SEARCH
  • 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 8667997
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:06:23+00:00 2026-06-12T18:06:23+00:00

I am trying to make a simple app. Where user sees one edittext ..

  • 0

I am trying to make a simple app. Where user sees one edittext .. enters some text in it.. and then press send… then the server on laptop receives that message.

Now the NetworkOnMainThread exception is giving me Headaches……the app works perfectly for 2.3.3 because there was no such thing as NetworkOnMainThread Exception that time.

Having searched a lot .. Two solutions are

  1. Making new thread for networking OR
  2. AsyncTask.

I tried both without any results.

Try 1: With Separate Thread:

Now what I could understand is that I had to start a separate thread. Ok. I did.

Following is my client side code.

EditText e ;
TextView tv;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    e= (EditText) findViewById(R.id.editText1);
    tv = (TextView) findViewById(R.id.textView1);
    Thread startNetworking = new Thread(new NetworkThread());
    startNetworking.start();
}

public void sendMessage(View v){
        if(NetworkThread.sendToClient(e.getText().toString()))
            tv.setText("Status : Successful");
        else
            tv.setText("Status : Unsuccessful");
}

sendMessage is onClick function for my send button. I have another JAVA file NetworkThread.java….

Here is a code for that :

public class NetworkThread implements Runnable{

static DatagramSocket socket;
static InetAddress add;
public void run() {
    try {
        socket = new DatagramSocket();
    } catch (SocketException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    try {
        add = InetAddress.getByName("192.168.1.12");
    } catch (UnknownHostException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
public static boolean sendToClient(String message){
    DatagramPacket p = new DatagramPacket(message.getBytes(),message.getBytes().length,add,4444);
    try {
        socket.send(p);
        return true;
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return false;
    }
}
}

This is still doesn’t work. I first want to exhaust this first try then I will move onto asking here about AsyncTask and what I have tried. So for time being please help me achieve this simple task of sending and receiving a string.

  • 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-12T18:06:24+00:00Added an answer on June 12, 2026 at 6:06 pm

    Unfortunately, having sendToClient() defined in the same class as NetworkThread doesn’t mean that it will run on your network-specific thread. The sendToClient() method will still run on your main (UI) thread because it’s being called from your onClick() method. UI callbacks, such as onClick(), are always processed on the UI thread.

    I would recommend using an AsyncTask as it enables you to send arbitrary data (such as your message parameter) to the background/network thread before it executes. Continuing to use Runnable and Thread will require extra machinery to synchronize the execution of your UI and network threads, and these challenges are handled behind the scenes by AsyncTask.

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

Sidebar

Related Questions

This one is killing me. I'm trying to make a simple console app that
I am trying to make a simple Facebook app that takes some data from
I am trying to make a simple web app. I have a User table
I am fairly new to iOS development and trying make a simple app which
I'm trying to make a simple app that sits on the Mac OSX menu
I'm trying to make a simple drawing app in c++, but i'm having trouble
I'm trying to make a very simple updater app that reads current version.txt file
Trying to make simple minesweeper game in python, but have one problem. I have
I'm trying to make a simple username/password authentication in a Spring Security web app.
So I'm trying to make a simple application that quizzes the user. It asks

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.