我正在使用socket.io库创建聊天应用。库的github链接是com.github.nkzawa:socket.io-client:0.3.0。我想添加用户添加频道的功能。为此,我发出了频道名称和说明,但问题是数据没有发射到服务器。我已完成调试,但我无法检测到问题出在哪里。代码如下: 的 HomeActivity.java
public class HomeActivity extends AppCompatActivity implements AlertDialogueBoxInterface {
private Socket mSocket;
private Boolean isConnected = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
RoundTableApplication app = (RoundTableApplication) this.getApplication();
mSocket = app.getSocket();
mSocket.on(Socket.EVENT_CONNECT,onConnect);
mSocket.on(Socket.EVENT_DISCONNECT,onDisconnect);
mSocket.on(Socket.EVENT_CONNECT_ERROR, onConnectError);
mSocket.on(Socket.EVENT_CONNECT_TIMEOUT, onConnectError);
mSocket.connect();
private Emitter.Listener onConnect = new Emitter.Listener() {
@Override
public void call(Object... args) {
runOnUiThread(new Runnable() {
@Override
public void run() {
if(!isConnected) {
if(null!=channelName)
mSocket.emit("add channel", channelName, channelDescription);
Toast.makeText(getApplicationContext(),
"Connected", Toast.LENGTH_LONG).show();
isConnected = true;
}
}
});
}
};
private Emitter.Listener onDisconnect = new Emitter.Listener() {
@Override
public void call(Object... args) {
runOnUiThread(new Runnable() {
@Override
public void run() {
isConnected = false;
Toast.makeText(getApplicationContext(),
"Disconnected. Please check your internet connection", Toast.LENGTH_LONG).show();
}
});
}
};
private Emitter.Listener onConnectError = new Emitter.Listener() {
@Override
public void call(Object... args) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
"Failed to connect", Toast.LENGTH_LONG).show();
}
});
}
};
@Override
public void sendChannel(ArrayList<String> channelList) {
channelName = channelList.get(0);
channelDescription = channelList.get(1);
if (channelName != null && channelDescription != null) {
navItemIndex = 0;
CURRENT_TAG = TAG_CHANNEL;
Bundle bundle = new Bundle();
bundle.putString("channelName", channelName);
bundle.putString("channelDescription", channelDescription);
AddChannelModel addChannelModel = new AddChannelModel(channelName, channelDescription);
mSocket.emit("newChannel", addChannelModel);
Toast.makeText(this, "new channel", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onDestroy() {
super.onDestroy();
mSocket.disconnect();
mSocket.off(Socket.EVENT_CONNECT, onConnect);
mSocket.off(Socket.EVENT_DISCONNECT, onDisconnect);
mSocket.off(Socket.EVENT_CONNECT_ERROR, onConnectError);
mSocket.off(Socket.EVENT_CONNECT_TIMEOUT, onConnectError);
}
}
RoundTableApplication.java
public class RoundTableApplication extends Application {
private Socket mSocket;
{
try {
mSocket = IO.socket("http://chattymac.herokuapp.com/v1/");
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
}
public Socket getSocket() {
return mSocket;
}
}
AlertDialogueBox.java
public class AlertDialogueBox {
EditText etAlertDialogueChannelName, etAlertDialogueChannelDescription;
private String inputChannelName, inputChannelDescription;
private final ArrayList<String> channelList = new ArrayList<String>();
private AlertDialogueBoxInterface dialogueBoxInterface;
private Activity activity;
public AlertDialogueBox(Activity activity){
this.activity = activity;
this.dialogueBoxInterface = (AlertDialogueBoxInterface) this.activity;
}
public boolean getAlertDialogueBox(){
// inflate alert dialog xml
LayoutInflater li = LayoutInflater.from(activity);
View dialogView = li.inflate(R.layout.custom_dialogue_add_channel, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
activity);
// set title
//alertDialogBuilder.setTitle("Add Channel");
// set custom_dialog.xml to alertdialog builder
alertDialogBuilder.setView(dialogView);
etAlertDialogueChannelName = (EditText) dialogView
.findViewById(R.id.custom_dialogue_channel_name);
etAlertDialogueChannelDescription = (EditText) dialogView
.findViewById(R.id.custom_dialogue_channel_description);
// set dialog message
alertDialogBuilder
.setCancelable(false)
.setPositiveButton("Add Channel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
inputChannelName = etAlertDialogueChannelName.getText().toString();
inputChannelDescription = etAlertDialogueChannelDescription.getText().toString();
channelList.add(inputChannelName);
channelList.add(inputChannelDescription);
dialogueBoxInterface.sendChannel(channelList);
//Toast.makeText(activity, channelList.toString(), Toast.LENGTH_SHORT).show();
dialog.cancel();
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
return true;
}
}
我的代码的Github链接是 - https://github.com/jadaungeetanjali/RoundTable
我是Android的新手,如果有人可以帮我解决这个问题。提前致谢
答案 0 :(得分:0)
在RoundTableApplication.java中
替换:
private Socket mSocket;
与
public static Socket mSocket;
之后在HomeActivity.java中删除:
private Socket mSocket;
将HomeActivity.java中的mSocket替换为:
RoundTableApplication.mSocket
例如
的内容mSocket.connect();
使用
RoundTableApplication.mSocket.connect();
在建立连接后使用emit方法,你在onConnect中使用它。使用一次套接字连接后,您需要使用发射器侦听器接口来处理发出的事件。 Emit订阅事件但是要处理响应,你需要在我的代码中找不到的探测器监听器。
替换:
mSocket.emit("add channel", channelName, channelDescription);
使用:
mSocket.emit("add channel", listenerMethod);
在上面的语句中,listenerMethod是一种方法,您可以在其中描述获得响应时要执行的操作。
按照以下链接熟悉socket.io:
https://socket.io/blog/native-socket-io-and-android/
如需完整示例,请访问: