我想为此任务使用相同的套接字地址进行多次调用。
我试图从不同的班级获得地址,但没有工作。
请帮助!!!
我来自不同班级的电话是:
new ConnectToServer().execute(ip, "2000", "br1");
我的任务代码是:
public class ConnectToServer extends AsyncTask<String, Void, Void> {
@Override
protected Void doInBackground(String... parms) {
// TODO Auto-generated method stub
try {
Socket s = new Socket(parms[0], Integer.parseInt(parms[1]));
// ******
OutputStream os = s.getOutputStream();
BufferedOutputStream out = new BufferedOutputStream(os);
if (parms[2] == "br1") {
out.write("rr1".getBytes());
out.write('\n');
out.flush();
} else if (parms[2] == "br2") {
out.write("rr2".getBytes());
out.write('\n');
out.flush();
} else if (parms[2] == "br3") {
out.write("rr3".getBytes());
out.write('\n');
out.flush();
} else if (parms[2] == "br4") {
out.write("rr4".getBytes());
out.write('\n');
out.flush();
} else if (parms[2] == "bb1") {
out.write("bb1".getBytes());
out.write('\n');
out.flush();
} else if (parms[2] == "bb2") {
out.write("bb2".getBytes());
out.write('\n');
out.flush();
} else if (parms[2] == "bb3") {
out.write("bb3".getBytes());
out.write('\n');
out.flush();
} else if (parms[2] == "bb4") {
out.write("bb4".getBytes());
out.write('\n');
out.flush();
}
// s.shutdownOutput();
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
答案 0 :(得分:0)
您无法将java String
与String == String
进行比较。您需要改为使用String.equals(String)
。
试试这段代码:
if (parms[2].equals("br1")) {
out.write("rr1".getBytes());
out.write('\n');
out.flush();
} else if (parms[2].equals("br2")) {
out.write("rr2".getBytes());
out.write('\n');
out.flush();
} else if (parms[2].equals("br3")) {
out.write("rr3".getBytes());
out.write('\n');
out.flush();
} else if (parms[2].equals("br4")) {
out.write("rr4".getBytes());
out.write('\n');
out.flush();
} else if (parms[2].equals("bb1")) {
out.write("bb1".getBytes());
out.write('\n');
out.flush();
} else if (parms[2].equals("bb2")) {
out.write("bb2".getBytes());
out.write('\n');
out.flush();
} else if (parms[2].equals("bb3")) {
out.write("bb3".getBytes());
out.write('\n');
out.flush();
} else if (parms[2].equals("bb4")) {
out.write("bb4".getBytes());
out.write('\n');
out.flush();
}