AsynkThread但仍然是NetworkOnMainThreadException

时间:2017-05-22 19:24:50

标签: android android-asynctask networkonmainthread

我尝试在我的Android设备和arduino nano之间建立连接。

我测试了覆盆子pi零和我的arduino之间的TCP连接,它可以工作并完成应该做的事情。

现在我尝试编写Android应用程序,但它会抛出

android.io.NetworkOnMainThreadException

我发现了一些使用AsynkTask的提示,但它仍然会抛出此异常。我找到了一些解决方案,但在我的情况下也没有帮助(例如:将minAPI设置为11)。

public class MainActivity extends AppCompatActivity {

    [...]

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        [...]

        bt1.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v) {
                onBtnClick();
            }
        }
    }

    public void onBtnClick() {
        new Client().execute("0101010101");
    }



    private class Client extends AsyncTask<String, Void, String> {
        protected String doInBackground(String[] params) {
            // do above Server call here
            return "null";
        }

        protected void onPostExecute(String msg) {
            String reply ="";
            try {
                reply=sendData();
            } catch(IOException e) {
                e.printStackTrace();
                reply = "IOException: "+e;
            }
            txt1.setText(reply);
        }
        public String sendData() throws IOException{

            try {
                // send message to Arduino
                InetAddress serverAddr = InetAddress.getByName(ipAddress);
                DatagramSocket clientSocket = new DatagramSocket();

                send_data = str.getBytes();
                DatagramPacket sendPacket = new DatagramPacket(send_data, send_data.length, serverAddr, portNumber);
                clientSocket.send(sendPacket);

                // get reply
                byte[] receiveData1 = new byte[512];
                DatagramPacket receivePacket = new DatagramPacket(receiveData1, receiveData1.length);
                clientSocket.receive(receivePacket);

                String reply = new String(receiveData1);
                clientSocket.close();
                return reply;

            } catch (Exception e) {
                e.printStackTrace();
                return("Exception: "+ e);
            }
        }
    }
}

我做错了什么?谢谢你的帮助。

1 个答案:

答案 0 :(得分:2)

onPreExecute和onPostExecute方法在主线程上运行,因此,方法sendData应该在doInBackground上执行

private class Client extends AsyncTask<String, Void, String> {
        protected String doInBackground(String[] params) {
            String reply ="";
            try {
                reply=sendData();
            } catch(IOException e) {
                e.printStackTrace();
                reply = "IOException: "+e;
            }
        }

        protected void onPostExecute(String msg) {
            txt1.setText(reply);
        }
        public String sendData() throws IOException{

            try {
                // send message to Arduino
                InetAddress serverAddr = InetAddress.getByName(ipAddress);
                DatagramSocket clientSocket = new DatagramSocket();

                send_data = str.getBytes();
                DatagramPacket sendPacket = new DatagramPacket(send_data, send_data.length, serverAddr, portNumber);
                clientSocket.send(sendPacket);

                // get reply
                byte[] receiveData1 = new byte[512];
                DatagramPacket receivePacket = new DatagramPacket(receiveData1, receiveData1.length);
                clientSocket.receive(receivePacket);

                String reply = new String(receiveData1);
                clientSocket.close();
                return reply;

            } catch (Exception e) {
                e.printStackTrace();
                return("Exception: "+ e);
            }
        }
    }