数据不会使用socket.io库从android客户端发送到服务器
我正在使用socket.io库创建聊天应用程序。指向库的github链接是com.github.nkzawa:socket.io客户端:0.3.0。我想添加用户添加频道的功能。为此,我将发送通道名称和描述,但问题是数据不会发送到服务器。我已完成调试,但无法检测问题出在哪里。代码如下所示: HomeActivity.java数据不会使用socket.io库从android客户端发送到服务器,android,socket.io,Android,Socket.io,我正在使用socket.io库创建聊天应用程序。指向库的github链接是com.github.nkzawa:socket.io客户端:0.3.0。我想添加用户添加频道的功能。为此,我将发送通道名称和描述,但问题是数据不会发送到服务器。我已完成调试,但无法检测问题出在哪里。代码如下所示: HomeActivity.java public class HomeActivity extends AppCompatActivity implements AlertDialogueBoxInterfac
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);
}
}
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;
}
}
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;
}
}
AlertDialogueBox.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);
}
}
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;
}
}
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;
}
}
公共类警报对话框{
EditText etAlertDialogueChannelName、etAlertDialogueChannelDescription;
私有字符串inputChannelName,inputChannelDescription;
私有最终ArrayList通道列表=新ArrayList();
专用AlertDialogueBoxInterface dialogueBoxInterface;
私人活动;
公共警报对话框(活动){
这个。活动=活动;
this.dialogueBoxInterface=(AlertDialogueBoxInterface)this.activity;
}
公共布尔getAlertDialogueBox(){
//充气警报对话框xml
LayoutInflater li=LayoutInflater.from(活动);
视图对话框视图=li.充气(R.layout.custom\u dialogue\u add\u channel,null);
AlertDialog.Builder alertDialogBuilder=新建AlertDialog.Builder(
活动);
//定名
//alertDialogBuilder.setTitle(“添加频道”);
//将custom_dialog.xml设置为alertdialog builder
alertDialogBuilder.setView(dialogView);
etAlertDialogueChannelName=(编辑文本)对话框视图
.findviewbyd(R.id.custom\u dialogue\u channel\u name);
etAlertDialogueChannelDescription=(编辑文本)对话框视图
.findViewById(R.id.自定义对话\频道\描述);
//设置对话框消息
alertDialogBuilder
.setCancelable(错误)
.setPositiveButton(“添加频道”,
新建DialogInterface.OnClickListener(){
公共void onClick(对话框接口对话框,
int id){
inputChannelName=etAlertDialogueChannelName.getText().toString();
inputChannelDescription=etAlertDialogueChannelDescription.getText().toString();
channelList.add(输入channelname);
channelList.add(输入ChannelDescription);
dialogueBoxInterface.sendChannel(通道列表);
//Toast.makeText(活动,channelList.toString(),Toast.LENGTH_SHORT.show();
dialog.cancel();
}
})
.setNegativeButton(“取消”,
新建DialogInterface.OnClickListener(){
公共void onClick(对话框接口对话框,
int id){
dialog.cancel();
}
});
//创建警报对话框
AlertDialog AlertDialog=alertDialogBuilder.create();
//表现出来
alertDialog.show();
返回true;
}
}
指向我的代码的Github链接是-
如果有人能在这方面帮我的话,我对安卓系统还是新手。提前感谢您的RoundTableApplication.java 替换:
private Socket mSocket;
mSocket.emit("add channel", channelName, channelDescription);
与
之后,在HomeActivity.java中删除:
private Socket mSocket;
将HomeActivity.java中的mSocket替换为:
RoundTableApplication.mSocket
e、 国际贸易
mSocket.connect();
使用
并在建立连接后使用emit方法—您正在onConnect中使用它。连接套接字后使用,并且需要发射器侦听器接口来处理发出的事件。Emit订阅事件,但要处理响应,您需要我在代码中找不到的发射器侦听器
替换:
private Socket mSocket;
mSocket.emit("add channel", channelName, channelDescription);
与:
在上面的语句中,listenerMethod是一种描述当您得到响应时要做什么的方法
按照以下链接熟悉socket.io:
有关完整示例,请访问:
您的发射器正在连接到服务器。请检查您的服务器端代码。该代码是否正在从客户端获取数据。您可以通过此