Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/382.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/214.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用AsyncTask进行android网络连接_Java_Android_Tcp_Android Asynctask_Telnet - Fatal编程技术网

Java 使用AsyncTask进行android网络连接

Java 使用AsyncTask进行android网络连接,java,android,tcp,android-asynctask,telnet,Java,Android,Tcp,Android Asynctask,Telnet,我在使用AsyncTask时遇到了一些问题,因为我以前从未遇到过它,也不知道我在用它做什么 基本上,我得到了一个力接近,因为我试图运行一个主类上的连接。是否有人可以帮助我将AsyncTask添加到代码中: package com.smarte.smartipcontrol; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputS

我在使用
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;
  }

}

通过查阅文件


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

这只是一个简单的例子,说明它可能是什么样子:

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();
        }
    } 
}
公共类IPControl扩展活动{
private ProgressDialog pd=null;
私有字符串数据=null;
专用插座;
私有字符串serverIpAddress;
私有静态最终int重定向_SERVERPORT=32;
公共打印输出;
公共缓冲区中的读卡器;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
试试{
this.pd=ProgressDialog.show(这是“正在加载…”、请稍候…”、true、false);
新建AsyncAction().execute();
}捕获(例外e){
e、 printStackTrace();
}
}
私有类AsyncAction扩展了AsyncTask{
受保护的Void doInBackground(字符串…args){
试一试{
InetAddress serverAddr=InetAddress.getByName(serverIpAddress);
套接字=新套接字(serverAddr,重定向的\u SERVERPORT);
}捕获(未知后异常e1){
e1.printStackTrace();
}捕获(IOE1异常){
e1.printStackTrace();
}
试一试{
out=new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()),true);
in=新的BufferedReader(新的InputStreamReader(socket.getInputStream());
而(!in.ready());
readBuffer();
out.println(“root\r\n”);
//System.out.print(“root\r\n”);
而(!in.ready());
readBuffer();
out.println(“root\r\n”);
//System.out.print(“root\r\n”);
而(!in.ready());
字符串msg=“”;
while(在.ready()中){
msg=msg+(char)in.read();
}
}捕获(IOE){}
return null;//返回要传递给onPostExecute()的内容
}
受保护的void onPostExecute(字符串结果){
//结果是从doInbackground返回的数据
IPControl.this.data=结果;
if(IPControl.this.pd!=null){
IPControl.this.pd.discouse();
}
} 
}

阅读本简单教程,了解什么是异步任务以及如何使用它:


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

谢谢,我会尽力解决:)