0

I'm trying to send date from an Android app as the client side to a NetBeans app on the desktop as the server side, the connection works just fine, even sending data but that was before i updated my Android IDE to the version 3!

Now, when i try sending data i get the following exception :

android.os.NetworkOnMainThreadException

Now i saw a post here is Stackoverflow... this one, and i got that because sending the data is happening on the main thread, but i'm not using the main thread to send the data, and i'm still getting this error every time i try to send data.


Code Explanation : I have two methods that sends data, one to send data only once after the connection has been made, and the second once is to send data at any time, now my fist one is on the main thread, but the second one is on another thread, the exception well never occur on the first one, but it will always occur on the second one, i heard that one solution is to use AsyncTask and i'v used it on the second one, and now i get the excpention most of the time but not always.


Here is my code for better understanding :

This is the first method that runs only once and on the main thread, and it will always work :

private void SendInformation(){
    try{
        OutPut = new BufferedWriter(new OutputStreamWriter(ConnectionSocket.getOutputStream(), "UTF-8"));
        OutPut.write(android.os.Build.MODEL);
        OutPut.newLine();
        OutPut.flush();
    }catch(Exception e){
        e.printStackTrace();
    }
}

This is my second one with a thread, and this always give the exception :

public static void SendDetails(String S1, String S2){
    Handler handler = new Handler();

    handler.postDelayed(new Runnable(){

        public void run(){
            try{
                OutPut = new BufferedWriter(new OutputStreamWriter(ConnectionSocket.getOutputStream(), "UTF-8"));
                OutPut.write(S1 + "," + S2);
                OutPut.newLine();
                OutPut.flush();
            }catch(Exception e){
                e.printStackTrace();
                MainActivity.ErrorMessage.setText(e.toString());
            }
        }
    }, Delay * 1000);
}

And this is the same second once but using AsyncTask :

private static SendInfoTask SIT = new SendInfoTask();

public static void SendDetails(String S1, String S2){
    Handler handler = new Handler();

    handler.postDelayed(new Runnable(){

        public void run(){
            SIT.execute();
        }
    }, Delay * 1000);
}

static class SendInfoTask extends AsyncTask<Void, Void, Void> {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected Void doInBackground(Void... params) {
        try{
            OutPut = new BufferedWriter(new OutputStreamWriter(ConnectionSocket.getOutputStream(), "UTF-8"));
            OutPut.write(Client.AppName + "," + Client.NotificationText);
            OutPut.newLine();
            OutPut.flush();
        }catch(Exception e){
            e.printStackTrace();
            MainActivity.ErrorMessage.setText(e.toString());
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
    }
}

Sometimes it works and some times it does not, and when it does work, after trying to send like 5 time the whole application Crashes!!

Can someone please help me? What is the best practice to do what i'm trying to do? (appreciate any help)

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Mousa Alfhaily
  • 1,260
  • 3
  • 20
  • 38
  • 1
    `This is the first method that runs only once and on the main thread, and it will always work :`. No. It cannot work as it runs on the main thread and hence will cause a NetworkOnMainThreadException. – greenapps Jun 27 '18 at 11:40
  • 1
    `public void run(){ SIT.execute(); }`Pretty wrong code. You should not start an asynctask in the run of a runnable but from the main thread directly. Do awsy with thst handler. – greenapps Jun 27 '18 at 11:43

0 Answers0