您好我正在构建一个需要用户按下按钮的应用程序(共8个按钮)。这些按钮用于将字符串发送到服务器onclick。我遇到的问题是,当我连接到服务器后按任何按钮时,它会发送字符串,但第二次没有任何反应。我被建议使用来自AsyncTask的doInBackground(),运行keep运行套接字并在每次按下按钮时写入它。但我无法这样做。我该怎么办 ?我不知道问题出在哪里。我在这里放我的代码。
这是我的活动
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);
}
请帮助我,这对我来说非常重要。提前谢谢。
答案 0 :(得分:1)
您无法重复使用AsyncTask。您必须创建一个新对象并调用execute。
替换
start.execute();
与
start = new MyThread(); start.execute(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个单位。