I am trying to create a XMPP chat client using aSmack library for android.I am using openfire server that runs on localhost.
I am creating a XMPPConnection and logging in the user successfully but when I try to create a new roster entry for that connection it throws an internal server error
Presence presence = new Presence(Presence.Type.subscribe);
presence.setTo(requestedUser);
connection.sendPacket(presence);
try {
roster.createEntry(requestedUser+"#123", "nickname", null);
} catch (XMPPException e) {
Log.e("exception", e.getMessage().toString());
}
Through this piece of code I am trying to send a friend request to another user.
When you call roster.createEntry(), it will send request to your openfire server,and then the server will insert an new record into your ofRoster table in openfire database.
So insure that ofRoster table exists.
Related
I am trying to connect with my Node.js Socket.IO server via an Android app. The console log shows that the phone connects with the server but when I try to emit a message from Android, the server does not give any output on the log console. I have read through the sample Socket.IO Android app but I am not able to figure out what's the problem...
Below lies my code...
Client Android
mSocket.connect();
private void attemptSend() {
JSONObject jsonObject1 = new JSONObject();
try {
jsonObject1 = new JSONObject();
jsonObject1.put("message","This is a test");
} catch (JSONException e) {
e.printStackTrace();
}
mSocket.emit("test",jsonObject1);
Log.d("TAG", "Sent");
}
Server Node.js
io.on('connection', function(socket) {
console.log('User Connected');
socket.on('test', function(data) {
console.log(data.message)
}
});
As stated in the comments under the question, the URL that I used while connecting to the server was erroneous. After fixing the URL, the app started working as expected.
I am developing an Android application, and I need to send a message from the application to the Java Server.
Java Server works like this:
thread = new Thread(){
public void run(){
System.out.println("Server is running...");
try {
ServerSocket socket = new ServerSocket(7000);
while(true){
Socket s = socket.accept();
DataInputStream dis = new DataInputStream(s.getInputStream());
System.out.println("Received from client: " + dis.readUTF());
dis.close();
s.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
};
thread.start();
In my application I send the message in this way:
mt = new Thread() {
public void run() {
try {
Socket socket = new Socket("192.168.1.100", 7000);
DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
dos.writeUTF(song_field.getText().toString());
dos.flush();
dos.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
};
mt.start();
Toast.makeText(context, "Your Message is sent. Thank you!", Toast.LENGTH_SHORT).show();
I can send the message with emulator and my phone successfully, since they are connected to the same wifi connection, but if the device is not connected to the same network, message is not sent to the server. I want everybody to be able to send message to my computer server regardless of their internet connection.
How can I fix this problem?
In general you'll need to use something like Web Sockets to achieve what you're trying to do where, as would typically be the case, client/server are on different networks. There are a few different Web Socket implementations e.g. https://medium.com/square-corner-blog/web-sockets-now-shipping-in-okhttp-3-5-463a9eec82d1#.w9hrc1icw
EDIT
I initially misread question and thought you were trying to asynchronously send message from server to client (which would require something like Web Sockets). If you are just making requests from client to server then a typical solution would be to expose REST API from your server (and using something like Retrofit to make requests from client).
Now I am working with XMPP-chat for Android using ejabberd server.
When I am trying to connect to the server, it shows an error. But it works fine in openfire server.
I am using smack library.
Error log is given below:
04-21 20:34:16.824: I/XMPPChatDemoActivity(1929): [SettingsDialog] Connected to 10.0.2.2
04-21 20:34:21.932: E/XMPPChatDemoActivity(1929): Failed to log in as test3#eworks.com
04-21 20:34:21.932: E/XMPPChatDemoActivity(1929): No response from the server.
I found solution how to connect to gtalk and jabber.org with Smack 3.1.0:
Code for GTalk:
ConnectionConfiguration cc = new ConnectionConfiguration("talk.google.com", 5222, "gmail.com");
XMPPConnection connection = new XMPPConnection(cc);
try {
connection.connect();
// You have to put this code before you login
SASLAuthentication.supportSASLMechanism("PLAIN", 0);
// You have to specify your gmail addres WITH #gmail.com at the end
connection.login("some.account#gmail.com", "password", "resource");
// See if you are authenticated
System.out.println(connection.isAuthenticated());
} catch (XMPPException e1) {
e1.printStackTrace();
}
For jabber.org here is the code:
ConnectionConfiguration cc = new ConnectionConfiguration("jabber.org", 5222, "jabber.org");
XMPPConnection connection = new XMPPConnection(cc);
try {
connection.connect();
// You have to put this code before you login
SASLAuthentication.supportSASLMechanism("PLAIN", 0);
// You have to specify your Jabber ID addres WITHOUT #jabber.org at the end
connection.login("your.jabber", "password", "resource");
// See if you are authenticated
System.out.println(connection.isAuthenticated());
} catch (XMPPException e1) {
e1.printStackTrace();
}
With this code i can now connect to my local ejabberd and openfire server. I hope this will solve your problems.
I'm using Android 1.6. In my office there is some application which sends some data via HTTP post method.
I want to add a module send the data via socket.I send the data via socket (output stream) correctly.It receives in server side (c# socket application) socket application correctly.
But in HTTP post method some data are passed as parameter. I'm not able to find any methods in socket to send the some data as parameter and some data in streams.
The following are the code which i done for send data via socket through outpustream .
socket=new Socket(this.ipAddress,this.port_number);
//socket.setSocketImplFactory(fac)
Log.i(tagName, "after creating sokcet");
os=socket.getOutputStream();
is=socket.getInputStream();
dos=new DataOutputStream(os);
Log.i(tagName, "after creating ouput streams");
dis=new DataInputStream(is);
Log.i(tagName, "after creating input streams");
//dos.writeUTF(msg[i].trim());
//dos.write(msg[i].trim().getBytes());
//dos.writeUTF(msg[i].trim());
dos.write(msg[i].trim().getBytes()); //data written as bytes
//dos.writeUTF(str)
dos.flush();
Log.i(tagName, "after writing data to os");
StringBuilder sbuilder=new StringBuilder();
///*
int ch;
byte bt=1;
while((bt=(byte) dis.read())!=-1)
{
Log.i(tagName, "ch="+bt);
byte temp[]=new byte[1];
//temp[0]=(byte)ch;
temp[0]=(byte)bt;
String tempStr1=new String(temp);
Log.i(tagName, "tempstr:"+tempStr1);
sbuilder.append(tempStr1);
Log.i(tagName, "Data fro server : "+sbuilder.toString());
tempStr1=null;
}
//*/
//byte tt[]=new byte[dis.readLine()]
//resultStr=dis.readLine();resultStr=resultStr.trim();
resultStr=sbuilder.toString();
Log.i(tagName, "server res :"+resultStr);
if(dos!=null)
{
try
{
dos.close();
}
catch(Exception ex)
{
}
}
if(dis!=null)
{
try
{
dis.close();
}
catch(Exception ex){}
}
if(socket!=null)
{
try
{
socket.close();
}
catch(Exception ex)
{
}
}
The above coding snippet works correctly.
But in HTTP post method the user name and password as parameter and the actual data in streams. Like that I want to send the username and password in parameter and actual in streams.
I find the solution.
We cannot send the data as parameter like http parameter in socket connection.
We can send the data only in streams.
In server side we can write to parse the data from the streams.
Thanks
I am using the XMPP Connection(using smack) for chat in android application.I have made the connection with openfire and also i can send and receive the message.But the problem is that when i go in the XMPPClient.java activity then it made the connection.So i cant get any message till not go in that activity.So how can made the connection at the starting and then reuse at other activity.Code is in this 2 links ConnectionSettings file and the chatscreen in which we can do chat.In this link the comment line is also my questions so please also see that comment.
Create global XMPPConnection object and Use below funciton and store in global XMPPConnection object and use that connection object everywhere. This is a sample gtalk example.
public XMPPConnection login() throws XMPPException {
ConnectionConfiguration config = new
ConnectionConfiguration("talk.google.com",5222,"gmail.com");
config.setSecurityMode(SecurityMode.required);
config.setTruststoreType("BKS");
config.setTruststorePath("/system/etc/security/cacerts.bks");
XMPPConnection connection = new XMPPConnection(config);
connection.connect();
connection.login(username, password);
Presence presence = new Presence(Presence.Type.available);
presence.setMode(Presence.Mode.available);
connection.sendPacket(presence);
try {
Thread.sleep(3000);
} catch (Exception ex) {
ex.printStackTrace();
}
return connection;
}