aSmack + Android : MultiUserChat class; getMembers, getModerators, etc not working? - android

i'm using aSmack (Smack port for Android) to connect to and communicate with an XMPP server (Openfire 3.7.1). i can get multi user chat to work as far as sending messages with the MultiUserChat class. however, calling any method that lists the occupants of the room fails. relevant code:
setDefaultConnection();
if(connection!=null) {
mMuc = new MultiUserChat(connection, "hermitage_recycling#conference.jmartinw7");
try {
mMuc.join("chester");
mMuc.getModerators(); //line 71
ArrayList<Affiliate> dudes = new ArrayList<Affiliate>(mMuc.getMembers());
Iterator<Affiliate> iter = dudes.iterator();
while(iter.hasNext()) {
Affiliate dude = iter.next();
Log.w(this.getClass().getName(), dude.getNick());
}
} catch(XMPPException xmppe) {
Log.w(this.getClass().getName(), "MUC error: "+xmppe.getMessage());
}
}
logcat:
07-10 13:38:56.248: ERROR/AndroidRuntime(13003): FATAL EXCEPTION: main
java.lang.ClassCastException: org.jivesoftware.smack.util.PacketParserUtils$2
at org.jivesoftware.smackx.muc.MultiUserChat.getOccupants(MultiUserChat.java:1797)
at org.jivesoftware.smackx.muc.MultiUserChat.getModerators(MultiUserChat.java:1761)
at org.apache.android.xmpp.XMPPClient$1.onClick(XMPPClient.java:71)
at android.view.View.performClick(View.java:2532)
is there something wrong with the library or am i doing something wrong? getOccupants() seems to work.
MultiUserChat example and javadocs:
http://www.igniterealtime.org/builds/smack/docs/latest/javadoc/
http://www.igniterealtime.org/builds/smack/docs/latest/documentation/extensions/index.html
aSmack:
http://code.google.com/p/asmack/
http://code.google.com/p/asmack/issues/detail?id=72

I believe your problem is being caused by a failure for asmack to load the smack.providers file which tells asmack which classes to load. If you read the README it describes how to load the smack.providers file into asmack(Thanks Flow). You need to do this before starting any XMPP activity.
Try https://github.com/Flowdalic/asmack, as the version you are working off has not been updated in 2 years.

Please read the README of aSmack and follow the instructions about the ProviderManager.

Related

Xamarin SqlServer cant get a connection

I'm building an app with the Entity Framework on Xamarin that lets me compare some data. But when I start my "fetchdata" function, I receive the Error:
System.Data.SqlClient.SqlException (0x80131904): Snix_Connect (provider: SNI_PN7, error: 35 - SNI_ERROR_35)Snix_Connect (provider: SNI_PN7, error: 35 - SNI_ERROR_35)
I see many posts about Xamarin / Android & that it is not possible to get a connection to a SQL Server. Is there any way to fetch data from a SQL Server with .NET Core on Xamarin?
This is the string I put into SQL_Class folder with Sql_Common.cs
Fill up the brace brackets with actual parameters (removing the brace brakets too).
public static string SQL_connection_string = #"data source={server_address};initial catalog={database_name};user id={user_id};password={password};Connect Timeout={seconds}";
Then I access whenever I need it from any xamarin code just like we use in our asp.net c#
This works for me on my app without any issues.
using (SqlConnection Sql_Connection = new SqlConnection(Sql_Common.saralEHR_connection_string))
But as #Jason mentioned in his first reply, I too would get once again check the security part. I fexperienced before publishing Package to Google Play, they encrypt the App files with Hash Key Code and then only it gets upload to server
Yes it is possible (HuurrAYY!):
Im new in .net core, c# and so on and for me it was a hell of a work to get it working..
So here for the other noobs who are seeking for Help:
GuideĀ“s i used:
Building Android Apps with Entity Framework
https://medium.com/#yostane/data-persistence-in-xamarin-using-entity-framework-core-e3a58bdee9d1
https://blog.xamarin.com/building-android-apps-entity-framework/
Scaffolding
https://cmatskas.com/scaffolding-dbcontext-and-models-with-entityframework-core-2-0-and-the-cli/
How i did it:
Build your normal Xamarin app.
create new .net solution like in the tutorials (DONT WRITE YOUR Entity Framework CLASSES)
create a third solution what has to be a .net core console application
Scaffold your DB in your CONSOLE application move all created classes & folders in your "xamarin .net" solution & change the namespaces
Ready to Go!
Side Node: NuGets you need in every solution:
Microsoft.EntityFrameworkCore
Microsoft.EntityFrameworkCore.SqlServer
[EDIT: NuGets you need in every solution]
I am doing this way (working snippet):
string connectionString = #"data source={server};initial catalog={database};user id={user};password={password};Connect Timeout=10";
string databaseTable = "{table name}";
string selectQuery = String.Format("SELECT count(*) as Orders FROM {0}", databaseTable);
try
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
//open connection
connection.Open();
SqlCommand command = new SqlCommand(selectQuery, connection);
command.Connection = connection;
command.CommandText = selectQuery;
var result = command.ExecuteScalar().ToString();
//check if there is result
if(result != null)
{
OrdersLabel.Text = result;
}
}
}
catch (Exception ex)
{
OrdersLabel.Text = ex.Message;
}
It is working fine, but API call more elegant.
I hope it helps.

Enabling XEP-0198 in aSmack

I'm currently using asmack against an XMPP server which has XEP-0198 enabled.
I'm trying to enable this feature from the asmack library but I could not find any info on how to do this, so following http://xmpp.org/extensions/xep-0198.html I know I have to enable this feature by sending:
<enable xmlns='urn:xmpp:sm:3'/>
But I have no idea on how to do this, since this is not a stanza. Any help is appreciated.
EDIT:
As dant3 has pointed out XEP-0198 is not supported in Smack but I still want to enable it by sending that simple XML through Smack, I just have no idea how to do that.
It looks like smack do not have XEP-0198 implementation. So, you can't enable it.
It would be nice if you contribute it for smack (and thus - for asmack).
EDIT:
Smack 4.1.0 having now support of XEP-0198 stream management. See this wiki page for details.
I solved it by creating my own type of Package:
public class MyPacket extends Packet
{
private String content = "";
public MyPacket(String content)
{
this.content = content;
}
#Override
public String toXML()
{
return this.content;
}
}
I pass the xml in the constructor and then off it goes:
MyPacket p = new MyPacket("<enable xmlns='urn:xmpp:sm:3'/>");
this.connection.sendPacket(p);
Definitely not the best code in the world but it works.

Unable to get Joined Chat Rooms using asmack

My problem is i am not been able to receive joined chat rooms. I am using the openfire server 3.8.2 and asmack library asmack-android-16.jar. I receive item-not-found error when i call getJoinedRooms function. though i can see the user is joined in the room from the admin console. Is it the server problem or the client problem or some issue with asmack? Please tell me if someone is able to get joined chat rooms using openfire and asmack for android.
here is how i am call the function:
Iterator RoomsIterator=MultiUserChat.getJoinedRooms(MyService.getConnection(),"user#192.168.1.3");
i also tried this but it gives no response form server:
Iterator RoomsIterator=MultiUserChat.getJoinedRooms(MyService.getConnection(),"user#192.168.1.3/Smack");
Please help me with my problem
Thanks in advance.
I solved my problem by adding a packet listener after call get joined rooms function.. as i was getting an empty list but when i debug i check that the rooms was getting returned in the resultant xml stanze that was sent by the server therefore i run the getjoinedroom function of asmack and then i manually add ha packet listener like this:
public void AddPacketListener(){
PacketFilter filter = new IQTypeFilter(IQ.Type.RESULT);
MyService.getConnection().addPacketListener(new PacketListener()
{
public void processPacket(Packet paramPacket) {
if(paramPacket.getFrom().equals(MyService.getConnection().getUser())){
String xml=paramPacket.toXML();
String from[];
System.out.println(xml);
from=paramPacket.getFrom().split("/");
Pattern pattern = Pattern.compile("<item jid=\"(.*?)/>");
Matcher matcher = pattern.matcher(xml);
String parts[];
Roomlist.clear();
while (matcher.find()) {
parts=matcher.group(1).split("#");
Roomlist.add(parts[0]);
}
return;
}
}
},filter);
}

Not logged in to server while login into a server

I am trying to connect with ejabber using BOSH Manager in android with the help of asmack library.
but i am getting following error.
Caused by: java.lang.IllegalStateException: Not logged in to server.
at org.jivesoftware.smack.Roster.reload(Roster.java:197)
at org.jivesoftware.smack.BOSHConnection.login(BOSHConnection.java:351)
at org.jivesoftware.smack.Connection.login(Connection.java:353)
at com.bcl.BoradCastLocationActivity$StartXMPPConnection.doInBackground(BoradCastLocationActivity.java:75)
at com.bcl.BoradCastLocationActivity$StartXMPPConnection.doInBackground(BoradCastLocationActivity.java:1)
at android.os.AsyncTask$2.call(AsyncTask.java:185)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
... 4 more
and here is my code
SASLAuthentication.registerSASLMechanism("DIGEST-MD5",SASLDigestMD5Mechanism.class);
SASLAuthentication.supportSASLMechanism("DIGEST-MD5", 1);
System.setProperty("smack.debugEnabled", "true");
XMPPConnection.DEBUG_ENABLED = true;
SmackConfiguration.setPacketReplyTimeout(6000);
BOSHConfiguration bconfig = new
BOSHConfiguration(false, "example.com", 5280, "/xmpp-http-bind/", "example.com");
bconfig.setSASLAuthenticationEnabled(true);
BOSHConnection xmpp = new BOSHConnection(bconfig);
xmpp.connect();
xmpp.login("username","password"); // HERE I AM GETTING AN ERROR
A glance at the aSmack sources indicated that this an an bug. A quick workaround is to to call Configuration.setRosterLoadedAtLogin(false), a fix would include moving 3 lines from BOSHConnection.java:350 after the line authenticated = true;.
I have made the changes in aSmack, please run build.bash and use the generated .jar.

Cannot get list of public rooms from xmpp Server on Android?

Hello Everyone i am new to android and i am currently stuck on this.
I have to return list of public rooms created on xmpp server. The problem i am having is that the code below works fine for java but there is a null pointer exception in case of android.
Any help regarding this would be appreciated.
I am using an openfire server and testing it on local machine so that is the reason why i am using ip Address instead of domain name.
I am using smack library for JAVA and Asmack Library for android
String server_name = "192.168.3.113";
ConnectionConfiguration config = new ConnectionConfiguration(
server_name, 5222);
XMPPConnection connection = new XMPPConnection(config);
try {
connection.connect();
connection.login("s1", "123");
Collection<HostedRoom> rooms = MultiUserChat.getHostedRooms(
connection, "conference.geekoid");
for (HostedRoom room : rooms) {
System.out.println(room.getName());
}
} catch (XMPPException e) {
System.out.println("Error" + e.getMessage() + "\n"); //for JAVA
log.e("Android Error",e.getmessage()); // For Android
}
The problem is that the static block of the ServiceDiscoveryManager class has to be evaluated before any connection is created. In smack this is done via an configuration file, but this approach does not work on Android and therefore on aSmack.
The workaround mentioned in the answer is somehow ugly, since you really don't want to use the Constructor to fetch the SDM object, instead the get() method should be used. But the get() method only works if there was actually a SDM created for the connection.
So in order to init the SDM correctly on Android you need to call the full forName notation to init the static blocks of the class before you create the first (XMPP)Connection object.
Class.forName("org.jivesoftware.smackx.ServiceDiscoveryManager", true, ClassLoader.getSystemClassLoader()):
This is tracked as aSmack Issue 8
I have found the Solution to the problem.
The Android asmack library was using this in
getHostedRooms(Connection connection,
String serviceName) method
ServiceDiscoveryManager discoManager =ServiceDiscoveryManager.getInstanceFor(connection);
i replaced it with
ServiceDiscoveryManager discoManager = new ServiceDiscoveryManager(connection);
For those who are confused where this method is its in
Package: org.jivesoftware.smackx.muc
File: MultiUserChat.java
After you have done this. We have to register all the providers in Android whose detail can be found here. These providers are automatically registered when are using JAVA's smack library (In java Development) but in Android we have to register them ourself.

Categories

Resources