0
我想运行我的ServerSocket,我知道如何使用线程,但我想在Ui中启动它,所以我想用AsyncTask启动这个服务器。运行服务器不在UiThread
我有代码:
,但它不工作,谁能告诉我该怎么办?
我使用的AsyncTask ServerClient运行,我想也跑我的服务器liek的是, 这里是我的客户端代码:
public class MyClientTask extends AsyncTask<Void, Void, Void> {
String dstAddress="..."
int dstPort=8080;
String response = "";
MyClientTask(String addr, int port){
dstAddress = addr;
dstPort = port;
}
@Override
protected Void doInBackground(Void... arg0) {
Socket socket = null;
try {
socket = new Socket(dstAddress, dstPort);
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
response = "UnknownHostException: " + e.toString();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
response = "IOException: " + e.toString();
}finally{
if(socket != null){
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return null;
}
@Override
protected void onPostExecute(Void result)
{
super.onPostExecute(result);
}
}
}
即使我把toast放在onPostExecute()它不工作,我不认为我使用AsyncTask权利,我只需要在后台运行服务器。代码在帖子中是不正确的。 – user3465277
那是你所有的代码吗? – codeMagic
ServerSocket serverSocket; – user3465277