how to connect ejabbered server to android java code - android

how to connect ejabbered server to android
ConnectionConfiguration config = new ConnectionConfiguration("http://localhost:5280/admin");
XMPPConnection connection = new XMPPConnection(config);
connection.connect();
connection.login("Test", "Test");// Log into the server

You may use Smack Android Clientfor that.
By using Smack creating a connection is as simple like
// Create the configuration for this new connection
XMPPTCPConnectionConfiguration.Builder configBuilder = XMPPTCPConnectionConfiguration.builder();
configBuilder.setUsernameAndPassword("username", "password");
configBuilder.setResource("SomeResource");
configBuilder.setXmppDomain("jabber.org");
AbstractXMPPConnection connection = new XMPPTCPConnection(configBuilder.build());
// Connect to the server
connection.connect();
// Log into the server
connection.login();
...
// Disconnect from the server
connection.disconnect();
Smack Documention

Smak 4.1.3 ServerName is the name of your server. ServerIp is the ip address of your server and mPort is port for xmpp server which is usually 5222.
And Make sure you have registered users for example in your case user "Test" should be registered.
XMPPTCPConnectionConfiguration config = XMPPTCPConnectionConfiguration.builder()
.setServiceName(serverName).setHost(serverIp)
.setPort(mport)
.setCompressionEnabled(false).build();
XMPPTCPConnectionconn conn= new XMPPTCPConnection(config);
conn.connect();
conn.login(username,password);

Related

Connect to Ejabberd server using Smack on Android emulator

I've started to develop an android chat application using Smack. The server I've been using is Ejabberd 4.2 which I want to connect it on the local machine using the emulator. This is the code to make a connection and print some log text:
try {
InetAddress address = InetAddress.getByName("10.0.2.2");
DomainBareJid serviceName = JidCreate.domainBareFrom("localhost");
XMPPTCPConnectionConfiguration config = XMPPTCPConnectionConfiguration.builder()
.setUsernameAndPassword(user,pass)
.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled)
.setXmppDomain(serviceName)
.setHostAddress(address)
.setPort(5222)
.setDebuggerEnabled(true)
.build();
AbstractXMPPConnection conn = new XMPPTCPConnection(config);
conn.connect();
if (conn.isConnected()){
Log.d(TAG, "connection established");
}
conn.login();
if (conn.isAuthenticated()){
Log.d(TAG, "connection authorized");
}
} catch (Exception e) {
e.printStackTrace();
}
I've used 10.0.2.2 as the localhost domain name for emulator, but it throws ConnectionException.
SmackException$ConnectionException: The following addresses failed:
'null:5222' failed because: /10.0.2.2 exception:
java.net.SocketTimeoutException: failed to connect

Connecting to a host address with Android

I have a server address that I want to connect my app to.
This is his address: "http://54.148.194.246:8080/".
I try to connect to it by this code:
clientSocket = new Socket("http://54.148.194.246/", 8080);
But my app gives me this error :
java.net.UnknownHostException: Unable to resolve host "http://54.148.194.246/": No address associated with hostname.
I added Internet permission and my wireless is on (those were the answers that I saw for this problem).
Any ideas?
Thanks.
You need to remove http://from the IP/hostname when passing it to the Socket constructor:
clientSocket = new Socket("54.148.194.246", 8080);
Alternatively, use the URL class for sending HTTP requests specifically:
URL url = new URL("http://54.148.194.246:8080/");
InputStream strm = (InputStream) url.getContent();
// use strm as needed...
Or:
URL url = new URL("http://54.148.194.246:8080/");
URLConnection conn = url.openConnection();
// use conn as needed...

Android tcp connection from client

I'm trying to make my android phone a client to a server I wrote in python. The server works good (I have tried it) but I can't seem to connect the phone with the server.
This is the function that should create the connection:
public String createConnection() throws IOException{
InetAddress serverAddr = InetAddress.getByName(ipString);
clientSocket = new Socket(serverAddr, portNumber);
DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
outToServer.writeBytes(Client.INIT_HEY.name());
String ans = inFromServer.readLine();
return ans;
}
ipString is the server ip received by the user, portNumber is the port number and they are both correct.
When I try to connect to the server, I receive "null" as the error message.
Any thoughts?
Thanks!
Try it over wifi.
If you can find the phone's IP address, try ping it from your dev system.
If your are on cell network, try access web site from browser, make sure the network connection is working.
The error was because I was running a tcp connection on the main thread.. I could've fixed it by making the class an AsyncTask but I prefered adding this:
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
Thanks all!

Asmack 18 connection : google.com:5222 Exception: Could not connect to talk.google.com remote-server-timeout

I am trying to use asmack 18 to connect to gtlak server for XMPP connection.
public static final String HOST = "talk.google.com";
public static final int PORT = 5222;
public static final String SERVICE = "gmail.com";
ConnectionConfiguration connConfig = new ConnectionConfiguration(HOST, PORT, SERVICE);
XMPPConnection connection = new XMPPConnection(connConfig);
try {
//Connect to the server
connection.connect();
connection.login("xxxxxxxx#gmail.com", "password");
// Set the status to available
Presence presence = new Presence(Presence.Type.available);
connection.sendPacket(presence);
//xmppClient.setConnection(connection);
Log.d("connection","connection successfull");
} catch (XMPPException ex) {
connection = null;
Log.d("connection","connection fail");
//Unable to connect to server
}
But it gives timeout error.
talk.google.com:5222 Exception: Could not connect to talk.google.com:5222.; : remote-server-timeout(504)
-- caused by: java.net.UnknownHostException: talk.google.com
Read the ReadME =) http://asmack.freakempire.de/0.8.9/README
Static Code
In order to work correctly on Android, you need to register Smack's
XMPP Providers and Extensions manually and init some static code
blocks before you doing any XMPP activty. Calling
SmackAndroid.init(Context) (in org.jivesoftware.smack) will do this
for you.
SmackAndroid.init(getApplicationContext());
ConnectionConfiguration connConfig = new ConnectionConfiguration("talk.google.com", 5222, "gmail.com");
...
.

No response from the server error in ejabberd server

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.

Categories

Resources