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.
Related
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
In my application i am using API's which are hosted on local server, and can be accessed on network. On emulator it works fine as it is connected to proper network. When I am using app on my phone it wont.
Is it possible to access local API's through phone with our normal internet connection?
I am using below http code for accessing API's.
public String getResponse(String url, int method, String postParameter) {
HttpResponse response = null;
// Creating HTTP client
HttpClient httpClient = new DefaultHttpClient();
// Creating HTTP Post
HttpPost httpPost = new HttpPost(url);
// Building post parameters
// key and value pair
List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(2);
nameValuePair.add(new BasicNameValuePair("jObj", postParameter));
// Url Encoding the POST parameters
try {
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair));
} catch (UnsupportedEncodingException e) {
// writing error to Log
e.printStackTrace();
}
// Making HTTP Request
try {
response = httpClient.execute(httpPost);
// writing response to log
Log.d("Http Response:", response.toString());
} catch (ClientProtocolException e) {
// writing exception to log
e.printStackTrace();
} catch (IOException e) {
// writing exception to log
e.printStackTrace();
} catch (Exception e2) {
e2.printStackTrace();
}
return response.toString();
}
Is there any setting which we can do for accessing through our normal internet?
Thanks in Advance
If you have it hosted on your local machine, you will have to find a way to connect both your phone and your local machine on the same network (most commonly Wifi). A simple work-around to this is creating a hotspot in your android device and connecting your local machine to it. Make sure to set the correct IP address in the android app.
Find your local machine ip address where the api's are hosted
using ipconfig and pass on the ip address in url
your url should be like 192.168.0.102 which is assigned by modem.
Answering as I cant comment, Please check that your phone is connected to your network and not using mobile data or some other WIFI network outside of your network.
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);
I'm having trouble connection to my bosh server, it says it needs "sid":
org.igniterealtime.jbosh.BOSHException: Connection Manager session creation response did not include required 'sid' attribute
this is how I'm trying to connect / login:
BOSHConfiguration.Builder configBuilder = BOSHConfiguration.builder();
configBuilder.setUsernameAndPassword(USERNAME, PASSWORD);
configBuilder.setHost(HOST);
configBuilder.setPort(PORT);
configBuilder.setFile(FILE_PATH);
configBuilder.setUseHttps(true);
configBuilder.setServiceName(SERVICE);
AbstractXMPPConnection connection = new XMPPBOSHConnection(configBuilder.build());
try {
connection.connect();
} catch (SmackException e) {
I have same problem and resolved when I added below code :
Bytestream stream = new Bytestream();
stream.setSessionID(username);
configBuilder.setResource(stream.toXML().toString());
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;
}