I am trying to implement an XMPP server in my Google Cloud module (endpoints) for my Android app using Android Studio in order to send downstream messages to devices.
I have found an example project which uses Smark here. The following is the code for the connection:
CcsClient ccs = CcsClient.prepareClient(Utils.FCM_ID,Utils.FCM_KEY,false);
try {
ccs.connect();
} catch (XMPPException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (SmackException e) {
e.printStackTrace();
}
But I am getting the following SmackException:
The following addresses failed: 'fcm-xmpp.googleapis.com:5236' failed because
java.net.SocketException: Permission denied: connection to
(10, [2607:f8b0:4001:c06::bc]:5236,6) denied due to policy
How can I fix this?
Related
I have the same code written in Java and in Android to connect to a mysql-server.
The same code in Java works, but in android it doesn't.
My Code:
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
String url1 = "jdbc:mysql://192.168.178.91:3306/db_meinungskanal?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC";
String user = "user1";
String password = "root";
connection = DriverManager.getConnection(url1, user, password);
if (connection != null) {
System.out.println("Connected to the database");
}
} catch (SQLException ex) {
System.out.println("An error occurred. Maybe user/password is invalid");
ex.printStackTrace();
}
In my android app I have granted acces to the internet with permissions, so I'm able to connect to internet.
If I run the code in android it doesn't work and I get the following:
System.out: An error occurred. Maybe user/password is invalid 02-11
20:29:13.261 17036-17036/camelgmbh.com.meinungskanal W/System.err:
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:
Communications link failure 02-11 20:29:13.261
17036-17036/camelgmbh.com.meinungskanal W/System.err: The last packet
sent successfully to the server was 0 milliseconds ago. The driver has
not received any packets from the server.
Need help ;D
Android is not supporting MYSQL.You can utilize SQLite or Create web services to access MySQL data from the app.
MultiUserChatManager manager = MultiUserChatManager.getInstanceFor(xmppconnection.getConnection());
try {
MultiUserChat muc = manager.getMultiUserChat("test2#conference.cca");
muc.join("test2#conference.cca");
Message msg = new Message("test2#conference.cca", Message.Type.groupchat);
msg.setBody("Hi Testing..Group chat..");
muc.sendMessage(msg);
// muc.join("test", "1234");
} catch (SmackException.NotConnectedException e) {
e.printStackTrace();
} catch (SmackException e) {
e.printStackTrace();
} catch (XMPPException.XMPPErrorException e) {
e.printStackTrace();
} catch (XMPPException e) {
e.printStackTrace();
}
Error is:
error code="403" type="auth" forbidden xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"/>**
There are several errors, logical and procedurals.
With this invocation:
MultiUserChat muc = manager.getMultiUserChat("test2#conference.cca");
you have in muc object your groupchat.
So you need to check if you already joined this groupchat or double join will raise an exception.
so
if (!muc.isJoined())
muc.join("My nickname");
more, when you join, you MUST provide an unique nickname per User to join, or you'll obtain an exception with the second user. Set as nickname the same name of the groupchat it's 99% a logical error.
Finally, to send a message, just send it through MUC object or you'll risk, like in this case, to miss some information.
So just send it with
muc.send("Hi Testing..Group chat..");
Last but not least: of course multiuserchat must exists or inititilized before properly, it's a prerequisite to do all this. As first step, just create it in Openfire with http-admin-panel (make it persistant)
I use NCache java API for connecting to NCache server but it throws an exception:
com.alachisoft.ncache.runtime.exceptions.GeneralFailureException: No server is available to process the request.
I also tried some suggestion from this site
but cannot solve the problem.
Here are the java codes.
String cacheId = "mycache";
try {
cache = NCache.initializeCache(cacheId);
} catch (Exception ex) {
ex.printStackTrace();
log.log(Level.SEVERE, "NCacheConnection()", ex.getMessage());
}
I have installed ejabberd on my local server. This was then tested in spark for its functionality and it worked fine. Now I want to add a new user through the android app.
I tried adding a new user through spark and it worked fine. The fields I have given are uesrname, password, confirm password, server. But when I tried to do it using the smack api in the android app it gave the following error:
org.jivesoftware.smack.XMPPException$XMPPErrorException: XMPPError: forbidden - auth
I was using createAccount(), seen in the code I was using below, to create the new account in smack.
AccountManager accountManager = AccountManager.getInstance(conn1);
try {
accountManager.createAccount("tryuser", "qwerty");
Log.i("log", "created user successfully");
} catch (SmackException.NoResponseException e) {
e.printStackTrace();
} catch (XMPPException.XMPPErrorException e) {
e.printStackTrace();
} catch (SmackException.NotConnectedException e) {
e.printStackTrace();
}
I have checked if it supports new account creation by supportsAccountCreation() and it returned true.
I have changed my register rule to allow all in ejabberd server. and i don't think it has any problem because i can create account from spark, but getting error in smack.
I have looked into the following SO questions related to this topic but no luck.
Ejabberd can't register new user
How to register a new user on XMPP using (a)Smack library
Does anyone have any suggestions on how to solve this?
Please give a try with below -
AccountManager accountManager = AccountManager.getInstance(connection);
try {
if (accountManager.supportsAccountCreation()) {
accountManager.sensitiveOperationOverInsecureConnection(true);
accountManager.createAccount("userName", "password");
}
} catch (SmackException.NoResponseException e) {
e.printStackTrace();
} catch (XMPPException.XMPPErrorException e) {
e.printStackTrace();
} catch (SmackException.NotConnectedException e) {
e.printStackTrace();
}
And you also need to set below in ejabberd.cfg (config file)
{access, register, [{allow, all}]}.
which means - In-band registration allows registration of any possible username. To disable in-band registration, replace 'allow' with 'deny'.
And in mod_register (module in same config file) please set below -
{access_from, register}
& before that please check you are connected to XMPP server.
Probably this will resolve your issue.
I want to use the latest release of smack 4.0.0 in my project for xmpp with android studio 0.6.0,the code as below:
try{
ConnectionConfiguration config = new ConnectionConfiguration("wooxonline.com",4000);
XMPPConnection connection = new XMPPTCPConnection(config);
connection.connect();
connection.login("cliff","cliff123");
}
catch (SmackException e) {
e.printStackTrace();
}
catch (java.io.IOException e)
{
e.printStackTrace();
}
catch (XMPPException e) {
e.printStackTrace();
Log.i(TAG, "login failed!");
}
this almost like sample with the smack sample code,but I have a compile issue that can't be solved as below trace info:
Error:(58, 29) error: cannot access SaslException
class file for javax.security.sasl.SaslException not found
I'm stuck here and anyone can help to have a look?
I ran into the same problem. This is the appropriate import sentence:
import org.apache.harmony.javax.security.sasl.SaslException;
According to the official forum "Smack does not work on Android. You need aSmack." So you should try to change the library to ASmack.
You can use maven to get the jar or download it from here
Reference:https://community.igniterealtime.org/thread/52833