Android 异步任务套接字错误

Android 异步任务套接字错误,android,sockets,android-asynctask,Android,Sockets,Android Asynctask,我有一个AsyncTask,它连接到网络资源并尝试从该资源读取数据。 这个资源可以在任何时候发送数据,所以我需要在后台的套接字。 当我在初始连接上收到响应时,我会收到消息,它会显示在MainActivity的文本视图上,但是当服务器发送另一条消息时,我会收到消息 错误: 下面是我的代码: public class Client extends AsyncTask<Void, byte[], Boolean> { private String SvrAddr = sv

我有一个AsyncTask,它连接到网络资源并尝试从该资源读取数据。 这个资源可以在任何时候发送数据,所以我需要在后台的套接字。 当我在初始连接上收到响应时,我会收到消息,它会显示在MainActivity的文本视图上,但是当服务器发送另一条消息时,我会收到消息 错误:

下面是我的代码:

public class Client extends AsyncTask<Void, byte[], Boolean> {

        private String SvrAddr = svradr;
        private int Svrport = port;
        private InputStream input;
        private OutputStream output;    
        private Socket nSocket;


        @Override
        protected Boolean doInBackground(Void... params){
            boolean rslt = false;
            try{
                Log.i("AsyncTask", "doInBackground: Creating Socket");
                SocketAddress sockad = new InetSocketAddress(SvrAddr,Svrport);
                nSocket = new Socket();
                nSocket.connect(sockad,5000);
                if(nSocket.isConnected()){
                    input =  nSocket.getInputStream();
                    output = nSocket.getOutputStream();
                    Log.i("AsyncTask","doInBackground: Socket created.. Streams gotten");
                    byte[] buffer= new byte[4096];
                    int read = input.read(buffer,0,4096);
                    while(read!=-1){
                        byte[] data = new byte[read];
                        System.arraycopy(buffer, 0, data,0,read);
                        Log.i("AsyncTask","doInBackgroiund: got data :" + new String(data,"UTF-8"));
                        DisplayData(new String(data,"UTF-8"));
                        read =  input.read(buffer,0,4096);
                    }
                }
            }catch(IOException ioe){
                Log.i("AsyncTask","doInBackground: IO Exception");
                rslt=true;
            }catch(Exception e){
                Log.i("AsyncTask","doInBackground: Exception Details : " + e.getMessage());
                rslt = true;
            }finally{


                try{
                    input.close();
                    output.close(); 
                    nSocket.close();
                }catch(IOException ioe){

                }catch(Exception e){
                    Log.i("AsyncTask","doInBackground: Exception");
                    rslt = true;
                }
                Log.i("AsyncTask","doInBackground: Completed");
            }
            return rslt;
        }

        private void DisplayData(String msg){               
            TextView t = (TextView) findViewById(R.id.textView1);
            t.setText(msg);
        }

}
公共类客户端扩展异步任务{
私有字符串SvrAddr=svradr;
专用int Svrport=端口;
私有输入流输入;
私有输出流输出;
专用插座;
@凌驾
受保护的布尔doInBackground(Void…params){
布尔rslt=false;
试一试{
i(“AsyncTask”、“doInBackground:创建套接字”);
SocketAddress sockad=新的InetSocketAddress(SvrAddr,Svrport);
nSocket=新套接字();
nSocket.connect(sockad,5000);
如果(nSocket.isConnected()){
input=nSocket.getInputStream();
output=nSocket.getOutputStream();
i(“AsyncTask”、“doInBackground:socketcreated..Streams get”);
字节[]缓冲区=新字节[4096];
int read=input.read(缓冲区,04096);
while(读取!=-1){
字节[]数据=新字节[读取];
数组复制(缓冲区,0,数据,0,读取);
Log.i(“AsyncTask”、“doInBackgroiund:got data:”+新字符串(数据,“UTF-8”);
显示数据(新字符串(数据,“UTF-8”);
读取=输入。读取(缓冲区,04096);
}
}
}捕获(ioe异常ioe){
i(“AsyncTask”、“doInBackground:IO异常”);
rslt=真;
}捕获(例外e){
Log.i(“AsyncTask”,“doInBackground:异常详细信息:”+e.getMessage());
rslt=真;
}最后{
试一试{
input.close();
output.close();
nSocket.close();
}捕获(ioe异常ioe){
}捕获(例外e){
i(“AsyncTask”、“doInBackground:Exception”);
rslt=真;
}
Log.i(“AsyncTask”、“doInBackground:Completed”);
}
返回rslt;
}
私有void DisplayData(字符串msg){
TextView t=(TextView)findViewById(R.id.textView1);
t、 setText(msg);
}
}

只需移动
显示数据(新字符串(数据,“UTF-8”)到OnProgressUpdate(),如果仍然存在问题,请按照a进行操作。

可能重复。你甚至在线搜索过吗?你不应该从doInBackground()更新。使用publishProgress并实现onProgressUpdate()从后台任务更新UI。onProgressUpdate在UI线程的上下文中运行
public class Client extends AsyncTask<Void, byte[], Boolean> {

        private String SvrAddr = svradr;
        private int Svrport = port;
        private InputStream input;
        private OutputStream output;    
        private Socket nSocket;


        @Override
        protected Boolean doInBackground(Void... params){
            boolean rslt = false;
            try{
                Log.i("AsyncTask", "doInBackground: Creating Socket");
                SocketAddress sockad = new InetSocketAddress(SvrAddr,Svrport);
                nSocket = new Socket();
                nSocket.connect(sockad,5000);
                if(nSocket.isConnected()){
                    input =  nSocket.getInputStream();
                    output = nSocket.getOutputStream();
                    Log.i("AsyncTask","doInBackground: Socket created.. Streams gotten");
                    byte[] buffer= new byte[4096];
                    int read = input.read(buffer,0,4096);
                    while(read!=-1){
                        byte[] data = new byte[read];
                        System.arraycopy(buffer, 0, data,0,read);
                        Log.i("AsyncTask","doInBackgroiund: got data :" + new String(data,"UTF-8"));
                        DisplayData(new String(data,"UTF-8"));
                        read =  input.read(buffer,0,4096);
                    }
                }
            }catch(IOException ioe){
                Log.i("AsyncTask","doInBackground: IO Exception");
                rslt=true;
            }catch(Exception e){
                Log.i("AsyncTask","doInBackground: Exception Details : " + e.getMessage());
                rslt = true;
            }finally{


                try{
                    input.close();
                    output.close(); 
                    nSocket.close();
                }catch(IOException ioe){

                }catch(Exception e){
                    Log.i("AsyncTask","doInBackground: Exception");
                    rslt = true;
                }
                Log.i("AsyncTask","doInBackground: Completed");
            }
            return rslt;
        }

        private void DisplayData(String msg){               
            TextView t = (TextView) findViewById(R.id.textView1);
            t.setText(msg);
        }

}