Using AsyncTask for android network connection

后端 未结 3 1381
时光说笑
时光说笑 2020-12-03 16:32

I am having some trouble using AsyncTask as I have never come across it before and have no clue what I am doing with it.

Basically I am getting a force

相关标签:
3条回答
  • 2020-12-03 17:00

    By checking the documentaion here..

    You need to put the create connection part in the doInBackground.. then in the onPostExecute do whatever you want to update the ui.

    0 讨论(0)
  • 2020-12-03 17:11

    Go through this simple tutorial to get a good grounding on what an AsyncTask is and how to use it:

    https://androidresearch.wordpress.com/2012/03/17/understanding-asynctask-once-and-forever/

    Then try modifying your code to use async tasks. If you get problems, Just come back here :-)

    0 讨论(0)
  • 2020-12-03 17:14

    Just one quick example of how it migtht look like:

    public class IPControl extends Activity {
        private ProgressDialog pd = null;
        private String data = null;
        private Socket socket;
        private String serverIpAddress;
        private static final int REDIRECTED_SERVERPORT = 32;
        public PrintWriter out;
        public BufferedReader in ;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            setContentView(R.layout.main);
    
            try{   
                this.pd = ProgressDialog.show(this, "Loading..", "Please Wait...", true, false);
                new AsyncAction().execute();
    
            }catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        private class AsyncAction extends AsyncTask<String, Void, String> {
            protected Void doInBackground(String... args) { 
                try {
                    InetAddress serverAddr = InetAddress.getByName(serverIpAddress);
                    socket = new Socket(serverAddr, REDIRECTED_SERVERPORT);
                } catch (UnknownHostException e1) {
                    e1.printStackTrace();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
                try {
                    out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
                    in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                    while (! in.ready());
                    readBuffer();
                    out.println("root\r\n");
                    //System.out.print("root\r\n");
                    while (! in .ready());
                    readBuffer();
                    out.println("root\r\n");
                    //System.out.print("root\r\n");
                    while (! in .ready());
                    String msg = "";
    
                    while (in.ready()) {
                        msg = msg + (char) in .read();
                    }
                } catch (IOException e) {}
    
            return null;//returns what you want to pass to the onPostExecute()
        }
    
        protected void onPostExecute(String result) {
            //resultis the data returned from doInbackground
            IPControl.this.data = result;
    
            if (IPControl.this.pd != null) {
                IPControl.this.pd.dismiss();
            }
        } 
    }
    
    0 讨论(0)
提交回复
热议问题