'data is not emitted from android client to server using socket.io library
I am creating a chat app using socket.io library. The github link to library is com.github.nkzawa:socket.io-client:0.3.0 . I wanted to add the feature of adding channels by the users. For that I am emitting the channel name and description, but the problem is that data is not emittted to the server. I have done the debugging, but I can't detect where is the problem.The code is given below: 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 link to my code is - https://github.com/jadaungeetanjali/RoundTable
I am new to android, if anyone could help me out in this. Thanks in advance
Solution 1:[1]
In your RoundTableApplication.java
Replace:
private Socket mSocket;
with
public static Socket mSocket;
After that in your HomeActivity.java remove:
private Socket mSocket;
Replace mSocket in HomeActivity.java with:
RoundTableApplication.mSocket
e.g intead of
mSocket.connect();
use
RoundTableApplication.mSocket.connect();
And use emit method after the connection is established you are using it inside onConnect. Use once socket is connected and you require emitter listener interface for handling emitted events. Emit subscribes the event but to handle response you need emitter listener which i am unable to find in your code.
Replace:
mSocket.emit("add channel", channelName, channelDescription);
with:
mSocket.emit("add channel", listenerMethod);
In above statement listenerMethod is method in which you describe what to do when you get response.
Follow link below to get familiar with socket.io:
https://socket.io/blog/native-socket-io-and-android/
For full example visit:
Solution 2:[2]
Just check your socket.io-client version once, as my android application and nodejs was just connecting but not sending or receiving anything, then i have solved this issue by downgrading from
2.0.0 to 0.8.3 version in gradle file
implementation ('io.socket:socket.io-client:2.0.0') {
exclude group: 'org.json', module: 'json'
}
to
implementation ('io.socket:socket.io-client:0.8.3') {
exclude group: 'org.json', module: 'json'
}
and it worked for me
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Muhammad Saad Rafique |
Solution 2 | Osama Zafar |