1

嗨,我正在构建一个需要用户按下按钮(总共 8 个按钮)的应用程序。这些按钮用于将字符串发送到服务器 onclick 。我遇到的问题是,当我在连接到服务器后按下任何按钮时,它会按原样发送字符串,但第二次没有任何反应。有人建议我使用 AsyncTask 中的 doInBackground() 来运行,继续运行套接字并在每次按下按钮时写入它。但我无法这样做。我该怎么办 ?我不知道问题出在哪里。我在这里放我的代码。

这是我的活动

public class Acontroller extends Activity {

Button bForward;
Button bBackward;
Button bRight;
Button bLeft;
Button bSelect;
Button bStart;
Button bB;
Button bA;
Socket s;
DataOutputStream os;
String ip;

// MyAppActivity ip = new MyAppActivity();
MyThread start = new MyThread();

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub

    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);

    super.onCreate(savedInstanceState);

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);

    setContentView(R.layout.nesskin);

    bForward = (Button) findViewById(R.id.bForward);
    bBackward = (Button) findViewById(R.id.bBackward);
    bRight = (Button) findViewById(R.id.bRight);
    bLeft = (Button) findViewById(R.id.bLeft);
    bSelect = (Button) findViewById(R.id.bSelect);
    bStart = (Button) findViewById(R.id.bStart);
    bA = (Button) findViewById(R.id.bA);
    bB = (Button) findViewById(R.id.bB);

    Bundle gotIP = getIntent().getExtras();
    ip = gotIP.getString("ipAddress");


//  start.doInBackground(ip);
    //start.execute(ip);
    // sock.start();

}

protected void onStart() {
    // TODO Auto-generated method stub
    super.onStart();

    bForward.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub

            start.execute(ip);
        }
    });

并从线程。

public class MyThread extends AsyncTask <String, Void, String>{


Socket s;
DataOutputStream os;
String ip;
String cmd;


  @Override
  protected void onPreExecute() {
      Log.i("AsyncTask", "onPreExecute");
  }



@Override
protected String doInBackground(String... params) {
    int port = 2222;


    // TODO Auto-generated method stub

    if(s.isConnected()){
        try {
            os.writeUTF("forward");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }else{



    try {
    s= new Socket(ip, port);
    os = new DataOutputStream(s.getOutputStream());
    os.writeUTF("forward");

    }catch(Exception e){

        e.printStackTrace();
    }


    finally{
          if (s!= null){
           try {
            s.close();
           } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
           }
          }

          if (os != null){
           try {
            os.close();
           } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
           }
          }
    }


}


    return null;
}

我的目标是用户点击按钮的次数,相应的字符串必须发送到服务器。这是我使用 PC 作为服务器的服务器代码。

public class Server


public static void main(String[] args) throws AWTException {
    String msg_received = null;
    String fw = "forward";
    String bw = "backward";
    String l = "left";
    String r = "right";
    String se = "select";
    String st = "start";
    String a = "a";
    String b = "b";
    String fin = "finish";
//  Boolean finish = (msg_received.equal(fin));
    Robot robot = new Robot();

    try {
        ServerSocket ss = new ServerSocket(2222);
        System.out.println("Server Started...");

        while (true) {
            Socket s = ss.accept();

            System.out.println("Connection Request Received");

            DataInputStream DIS = new DataInputStream(s.getInputStream());
            msg_received = DIS.readUTF();
            System.out.println(msg_received);
            // s.close();
            // ss.close();

            if (msg_received.equals(fw)) {
                // tu yeh kerna

                robot.keyPress(KeyEvent.VK_UP);
                robot.keyRelease(KeyEvent.VK_UP);

            }

请帮助我,这对我来说真的很重要。提前致谢。

4

1 回答 1

1

您不能重复使用 AsyncTask。您必须创建一个新对象并调用执行。

代替

start.execute();

开始 = 新的 MyThread(); 开始.执行(IP);

或者如果您可以在完成后删除实例,那么您可以这样做:

(new MyThread()).start();

编辑:

要将参数发送到 AsyncTask,请执行以下操作:

start.execute(ip, cmd, param3, param4, param5);

doInBackground这样收集参数:

protected Void doInBackground(String... params)
{
   String ipParam = params[0];
   String cmdParam = params[1];
   String thirdParam = params[2];
   String fourthParam = params[3];
   String fifthParam = params[4];
}

您可以传递任意数量的字符串params,它会在运行时自动创建一个包含所有参数的新数组。请注意,如果您只传入两个,那么params大小将只有两个。如果传入 100 个参数,则params大小为 100 个单位。

于 2012-05-28T16:39:11.337 回答