如何创建的AsyncTask至prevent networkOnMainThreadException [英] How to create asyncTask to prevent networkOnMainThreadException

查看:201
本文介绍了如何创建的AsyncTask至prevent networkOnMainThreadException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的Andr​​oid应用程序的开发。我试图开发一个Android客户端服务器聊天

我的第一个项目。这是code为客户端。当客户端preSS btnJoin

它将连接到服务器和发送一个字符串。我看过很多例子,许多人

看起来是这样的。我有一个 networkOnMainThreadException 。如何让我的的AsyncTask 来prevent

这个问题?任何帮助将是非常美联社preciated。

  btnJoin =(按钮)findViewById(R.id.buttonJoin);
btnJoin.setOnClickListener(新OnClickListener(){
    @覆盖
    公共无效的onClick(视图v){
        Socket套接字= NULL;
        DataOutputStream类DataOutputStream类= NULL;
        DataInputStream以DataInputStream以= NULL;
        尝试{
            插座=新的Socket(192.168.1.4,9092);
            DataOutputStream类=新的DataOutputStream类(socket.getOutputStream());
            DataInputStream以=新DataInputStream所(socket.getInputStream());
            dataOutputStream.writeUTF(Hello服务器!);
            txtIP.append(dataInputStream.readUTF()+\\ n);
        }赶上(UnknownHostException异常五){
            e.printStackTrace();
        }赶上(IOException异常五){
            e.printStackTrace();
        } {最后
            如果(插座!= NULL){
                尝试{
                    socket.close();
                }赶上(IOException异常五){
                    e.printStackTrace();
                }
            }
            如果(DataOutputStream类!= NULL){
                尝试{
                    dataOutputStream.close();
                }赶上(IOException异常五){
                    e.printStackTrace();
                }
            }
            如果(DataInputStream以!= NULL){
                尝试{
                    dataInputStream.close();
                }赶上(IOException异常五){
                    e.printStackTrace();
                }
            }
        }
    }
});


解决方案

更改code为:

  btnJoin.setOnClickListener(新OnClickListener(){
    @覆盖
   公共无效的onClick(查看视图){              新LongOperation()执行();    }
}私有类LongOperation扩展的AsyncTask<弦乐,太虚,字符串> {
     Socket套接字= NULL;
     串strresult =;
        DataOutputStream类DataOutputStream类= NULL;
        DataInputStream以DataInputStream以= NULL;
          @覆盖
          保护字符串doInBackground(字符串... PARAMS){        尝试{
            插座=新的Socket(192.168.1.4,9092);
            DataOutputStream类=新的DataOutputStream类(socket.getOutputStream());
            DataInputStream以=新DataInputStream所(socket.getInputStream());
            dataOutputStream.writeUTF(Hello服务器!);
            strresult.append(dataInputStream.readUTF()+\\ n);
         // txtIP.append(dataInputStream.readUTF()+\\ n);
        }赶上(UnknownHostException异常五){
            e.printStackTrace();
        }赶上(IOException异常五){
            e.printStackTrace();
        } {最后
            如果(插座!= NULL){
                尝试{
                    socket.close();
                }赶上(IOException异常五){
                    e.printStackTrace();
                }
            }
            如果(DataOutputStream类!= NULL){
                尝试{
                    dataOutputStream.close();
                }赶上(IOException异常五){
                    e.printStackTrace();
                }
            }
            如果(DataInputStream以!= NULL){
                尝试{
                    dataInputStream.close();
                }赶上(IOException异常五){
                    e.printStackTrace();
                }
            }
        }                返回strresult;
          }          @覆盖
          保护无效onPostExecute(字符串结果){
                TextView的txtIP =(的TextView)findViewById(R.id.txtIP);
              // txtIP.append(结果+\\ n);
                txtIP.setText(结果+\\ n);          }          @覆盖
          在preExecute保护无效(){
          }    }

I'm new to android application development. I tried to develop an android server client chat

for my first project. This is the code for the client side. When the client press btnJoin,

it will connect to the server and send a string. I've read many example and many of them

looks like this. I got a networkOnMainThreadException. How do I make an asyncTask to prevent

this problem? Any help would be much appreciated.

btnJoin = (Button) findViewById(R.id.buttonJoin);
btnJoin.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        Socket socket = null;
        DataOutputStream dataOutputStream = null;
        DataInputStream dataInputStream = null;
        try {
            socket = new Socket("192.168.1.4", 9092);
            dataOutputStream = new DataOutputStream(socket.getOutputStream());
            dataInputStream = new DataInputStream(socket.getInputStream());
            dataOutputStream.writeUTF("Hello server!");
            txtIP.append(dataInputStream.readUTF() + "\n");
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (socket != null) {
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (dataOutputStream != null) {
                try {
                    dataOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (dataInputStream != null) {
                try {
                    dataInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
});

解决方案

Change your code as:

   btnJoin.setOnClickListener(new OnClickListener() {
    @Override
   public void onClick(View view){

              new LongOperation().execute("");

    }
}

private class LongOperation extends AsyncTask<String, Void, String> {
     Socket socket = null;
     String strresult="";
        DataOutputStream dataOutputStream = null;
        DataInputStream dataInputStream = null;
          @Override
          protected String doInBackground(String... params) {

        try {
            socket = new Socket("192.168.1.4", 9092);
            dataOutputStream = new DataOutputStream(socket.getOutputStream());
            dataInputStream = new DataInputStream(socket.getInputStream());
            dataOutputStream.writeUTF("Hello server!");
            strresult.append(dataInputStream.readUTF() + "\n");
         //   txtIP.append(dataInputStream.readUTF() + "\n");
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (socket != null) {
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (dataOutputStream != null) {
                try {
                    dataOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (dataInputStream != null) {
                try {
                    dataInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

                return strresult;
          }      

          @Override
          protected void onPostExecute(String result) {
                TextView txtIP= (TextView) findViewById(R.id.txtIP);
              //  txtIP.append(result + "\n");
                txtIP.setText(result + "\n");

          }

          @Override
          protected void onPreExecute() {
          }

    }  

这篇关于如何创建的AsyncTask至prevent networkOnMainThreadException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆