我想创建Socket连接,但我有一些问题。我必须在新线程中创建它,但我不能。
public class SocketManager {
private static SocketManager instance;
private BufferedReader in;
private PrintWriter out;
private Socket mSocket = null;
public static SocketManager me() {
if (instance == null) {
instance = new SocketManager();
}
return instance;
}
public void connection() {
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
try {
mSocket = new Socket(Constants.CHAT_SERVER_URL, 4444);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}.execute();
}
public boolean isConnected() {
return mSocket.isConnected();
}
和
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
status = (TextView) findViewById(R.id.status);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(this);
SocketManager.me().connection();
if (SocketManager.me().isConnected()) {
status.setText("Connected");
} else {
status.setText("Disconnected");
}
我有错误:
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.net.Socket.isConnected()' on a null object reference
Becouse mSocket
在新线程中创建,当我调用它时== null;如何在新线程中创建mSocket并使用它?
答案 0 :(得分:1)
我认为方法&#34; isConnected()&#34;正在尝试在初始化之前访问mSocket。
尝试将其更改为:
public boolean isConnected() {
return mSocket == null ? false : mSocket.isConnected();
}
这将避免此方法的NullPointerException。
但是这里正确的方法是使用回调,这样子线程可以在主线程完成时通知主线程。
public class SocketTask extends AsyncTask<Void, Void, Void> {
public interface AsyncTaskListener<T> {
void onTaskCompleted(T t);
}
private final AsyncTaskListener<String> listener;
public SocketTask(AsyncTaskListener<String> listener) {
this.listener = listener;
}
@Override
protected Void doInBackground(Void... params) {
try {
mSocket = new Socket(Constants.CHAT_SERVER_URL, 4444);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute() {
listener.onTaskCompleted();
}
}
你的类SocketManager需要实现回调:
public class SocketManager implements SocketTask.AsyncTaskListener {
}
答案 1 :(得分:1)
最好的方法是使用界面。
在Socketmanager类中创建一个接口
public class SocketManager {
private static SocketManager instance;
private SocketListner listner;
public interface SocketListner {
void onConnectionSuccess();
void onConnectionFailed();
}
public void connection(SocketListner listner) {
this.listner = listner;
new ConnectionTask().execute();
}
并从doInBackGround()
方法返回一个布尔值,以检查连接是否成功
class ConnectionTask new AsyncTask<Void, Void, Boolean>() {
@Override
protected Void doInBackground(Void... params) {
//...your code
}
@Override
protected void onPostExecute(Boolean result) {
if(result) {
listner.onConnectionSuccess();
} else {
listner.onConnectionFailed();
}
}
}
在您的活动中实施界面
public class YourActivity imlpements SocketListner {
@Override
protected void onCreate(Bundle savedInstanceState) {
//your code
..
}
@Override
void onConnectionSuccess() {
//your socket is connected
status.setText("Connected");
}
@Override
void onConnectionFailed() {
status.setText("Disconnected");
}
}
答案 2 :(得分:0)
添加布尔值以了解您的asynctask是否完整。
boolean mIsSocketInstanceCreated = false;
public void connection() {
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
try {
mSocket = new Socket(Constants.CHAT_SERVER_URL, 4444);
mIsSocketInstanceCreated = true;
} catch (IOException e) {
e.printStackTrace();
mIsSocketInstanceCreated = false;
}
return null;
}
}.execute();
}
public boolean isConnected() {
if (mIsSocketInstanceCreated)
return mSocket.isConnected();
return false;
}