Asynctask从服务器

时间:2018-01-29 07:37:50

标签: android sockets android-asynctask

嗨,大家好我是Android编程的新手,需要一些asynctask的帮助,我已经可以发送数据,但我不知道如何从服务器接收消息。

到目前为止,这是我的代码:

 public void getIpandPort()
    {
        String iPandPort = txtAddress.getText().toString();
        Log.d("MY TEST","IP String:" + iPandPort);
        String temp[] = iPandPort.split(":");
        wifiModuleIp = temp[0];
        wifiModulePort = Integer.valueOf(temp[1]);
        Log.d("MY TEST","IP:" + wifiModuleIp);
        Log.d("MY TEST ","PORT:" + wifiModulePort);

    }

    public  class Socket_AsyncTask extends  AsyncTask<Void, Void, Void>
    {    
        Socket socket;
        @Override
        protected Void doInBackground(Void... params){
            try{
                InetAddress inetAddress = InetAddress.getByName(MainActivity.wifiModuleIp);
                socket = new java.net.Socket(inetAddress,MainActivity.wifiModulePort);
                DataOutputStream dataOutputStream = new DataOutputStream(socket.getOutputStream());
                dataOutputStream.writeBytes(CMD);
                dataOutputStream.close();    
                socket.close();
            }catch (UnknownHostException e){e.printStackTrace();}catch (IOException e){e.printStackTrace();}
            return null;

        }
    }

0 个答案:

没有答案