如何使用Asynctask建立socket连接?
How to use Asynctask establish socket connections?
最近研究利用Asynctask建立socket连接,遇到一些情况
现在我可以使用按下按钮建立连接了,但是我不知道如何让按钮文本从"unconnected"变成"Connected "。编译没问题但是在模拟器上执行不了,按下按钮,我可以看到从"unconnected"到"Connected"的文字,但下一秒就强制关闭了APP。
以下是我的代码片段:
public class MainActivity extends Activity {
public static Button Btn_Wifi,Btn_Power,Btn_Flame;
public static Boolean connected=false;
public static DataOutputStream dataOutputStream = null;
public static DataInputStream dataInputStream = null ;
public static Socket socket;
AsyncTask:
static class SocketTask extends AsyncTask<Void, Void, Void > {
@Override
protected Void doInBackground(Void ... parms) {
try {
socket = new Socket("ip", port);//
dataOutputStream = new DataOutputStream(socket.getOutputStream());//and stream
changeConnectionStatus(true);//change the connection status
}catch (UnknownHostException e) {
}catch (IOException e) {
}finally {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
};
Button.OnClickListener:
Button.OnClickListener BtnWifiOnClickListener = new Button.OnClickListener(){
@Override
public void onClick(View view) {
//SocketTask sockettask = new SocketTask();
new SocketTask().execute();
}
};
更改连接状态:
public static void changeConnectionStatus(Boolean isConnected) {
connected=isConnected;//change variable
if(isConnected){//if connection established
Btn_Wifi.setText("connected");
Btn_Power.setEnabled(true);
}else{
Btn_Wifi.setText("unconnected");
Btn_Power.setText("POWER OFF");
Btn_Power.setEnabled(false);
PowerStatus(false);
}
}
正解
@Override
protected void onPostExecute(Void result) {
changeConnectionStatus(true);
}
你不能触摸来自 doInBackground()
的 UI
你需要使用onPostExecute()
将 changeConnectionStatus(true)
的调用从 doInBackground
() 移动如下:
@Override
protected void onPostExecute() {
changeConnectionStatus(true);
}
最近研究利用Asynctask建立socket连接,遇到一些情况 现在我可以使用按下按钮建立连接了,但是我不知道如何让按钮文本从"unconnected"变成"Connected "。编译没问题但是在模拟器上执行不了,按下按钮,我可以看到从"unconnected"到"Connected"的文字,但下一秒就强制关闭了APP。
以下是我的代码片段:
public class MainActivity extends Activity {
public static Button Btn_Wifi,Btn_Power,Btn_Flame;
public static Boolean connected=false;
public static DataOutputStream dataOutputStream = null;
public static DataInputStream dataInputStream = null ;
public static Socket socket;
AsyncTask:
static class SocketTask extends AsyncTask<Void, Void, Void > {
@Override
protected Void doInBackground(Void ... parms) {
try {
socket = new Socket("ip", port);//
dataOutputStream = new DataOutputStream(socket.getOutputStream());//and stream
changeConnectionStatus(true);//change the connection status
}catch (UnknownHostException e) {
}catch (IOException e) {
}finally {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
};
Button.OnClickListener:
Button.OnClickListener BtnWifiOnClickListener = new Button.OnClickListener(){
@Override
public void onClick(View view) {
//SocketTask sockettask = new SocketTask();
new SocketTask().execute();
}
};
更改连接状态:
public static void changeConnectionStatus(Boolean isConnected) {
connected=isConnected;//change variable
if(isConnected){//if connection established
Btn_Wifi.setText("connected");
Btn_Power.setEnabled(true);
}else{
Btn_Wifi.setText("unconnected");
Btn_Power.setText("POWER OFF");
Btn_Power.setEnabled(false);
PowerStatus(false);
}
}
正解
@Override
protected void onPostExecute(Void result) {
changeConnectionStatus(true);
}
你不能触摸来自 doInBackground()
你需要使用onPostExecute()
将 changeConnectionStatus(true)
的调用从 doInBackground
() 移动如下:
@Override
protected void onPostExecute() {
changeConnectionStatus(true);
}