0

I'm sending some packets by UDP protocol but app crashes by NetworkOnMainThreadException error at this part of UDP class(in below):

client_socket.send(send_packet);

by searching the error i find out that should use AsyncTask in this case(I'm not sure about this,any idea). So how can i implement AsynkTask to this code in order to solve the error? My UDP class below:

public class CheckIP {

    private String CheckState;
    private String IP;

    //UDP IP Seraching
    private byte[] send_data = new byte[1024];
    private byte[] receiveData = new byte[1024];

    public CheckIP(final String BN, final Context context) throws IOException {

        DatagramSocket client_socket = new DatagramSocket(2222);
        InetAddress IPAddress = InetAddress.getByName("255.255.255.255");

        String str = String.valueOf(BN.charAt(7) + BN.charAt(5) + BN.charAt(8) + BN.charAt(3));
        send_data = str.getBytes();

        DatagramPacket send_packet = new DatagramPacket(send_data, str.length(), IPAddress, 2222);
        client_socket.send(send_packet);

        DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
        client_socket.receive(receivePacket);

        CheckState = "true";
        IP = receivePacket.getAddress().toString();
     }

    public String getIP() {
        return IP;
    }

    public String getCheckState() {
        return CheckState;
    }
}

and the related code in my home.class :

additembtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View v) {
                  try {
                        Buildnumber = "123456789";
                        CheckIP checkIP = new CheckIP(Buildnumber,home.this);
                        if (checkIP.getCheckState().equals("true")){
                            //show Toast
                        }else{
                            //show Toast
                        }

                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
Markus Kauppinen
  • 3,025
  • 4
  • 20
  • 30
Alireza Ma
  • 23
  • 6

1 Answers1

0

make another async task class

 private class UDPTask extends AsyncTask<URL, Integer, CheckIP> 
 {
 protected CheckIPdoInBackground(URL... urls) {

      Buildnumber = "123456789";
      CheckIP checkIP = null;
        try {
            checkIP = new CheckIP(Buildnumber, home.this);
        } catch (IOException e) {
            e.printStackTrace();
        }

     return checkIP;
 }

 protected void onProgressUpdate(Integer... progress) {
 }

 protected void onPostExecute(CheckIP checkIP) {

   if (checkIP.getCheckState().equals("true")){
                        //show Toast
                    }else{
                        //show Toast
                    }
 }
 }

and then call it in on click by using this Code :

new UDPTask().execute();
Alireza Ma
  • 23
  • 6
  • Cannot Resolve setProgressPercent... checkIP should change to result in onPostExecute and CheckIP checkIP = new CheckIP(Buildnumber,home.this); should be i try/catch and the Long paramets should be CheckIP... Thanks to answer overall – Alireza Ma Oct 03 '19 at 14:23