【问题标题】:Using AsyncTask for android network connection使用 AsyncTask 进行安卓网络连接
【发布时间】:2012-11-20 19:07:52
【问题描述】:

我在使用 AsyncTask 时遇到了一些问题,因为我以前从未遇到过它,也不知道我在用它做什么。

基本上我正在强制关闭,因为我正在尝试在主类上运行连接。有人可以帮我在代码中添加AsyncTask

package com.smarte.smartipcontrol;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class IPControl extends Activity {

  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);

        // Get the message from the intent
        Intent intent = getIntent();
        serverIpAddress = intent.getStringExtra(IPEntry.ACTUALSMARTIP);
        createConnection();

      }

  public void getModel(View view) {
    try {
      out.println("[m\r\n");
      //System.out.print("root\r\n");
      while (! in .ready());
      String textStatus = readBuffer();

    } catch (IOException e) {}
  }

  public void createConnection() {
    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());
      readBuffer();
    } catch (IOException e) {}


    //R.id.textStatus
  }

  private String readBuffer() throws IOException {
    String msg = "";

    while ( in .ready()) {
      msg = msg + (char) in .read();
    }
    //System.out.print(msg);
    if (msg.indexOf("SNX_COM> ") != -1) return msg.substring(0, msg.indexOf("SNX_COM> "));
    else return msg;
  }

}

【问题讨论】:

    标签: java android tcp android-asynctask telnet


    【解决方案1】:

    通过查看文档here..

    您需要将创建连接部分放在doInBackground.. 然后在onPostExecute 中执行任何您想要更新ui 的操作。

    【讨论】:

      【解决方案2】:

      只是一个简单的例子来说明它的外观:

      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();
              }
          } 
      }
      

      【讨论】:

        【解决方案3】:

        通过这个简单的教程,了解什么是 AsyncTask 以及如何使用它:

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

        然后尝试修改您的代码以使用异步任务。如果遇到问题,请回到这里 :-)

        【讨论】:

        • 谢谢阿努普,我会全力以赴的 :)
        猜你喜欢
        • 2012-11-20
        • 1970-01-01
        • 1970-01-01
        • 2013-02-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-05-05
        相关资源
        最近更新 更多