I am working upon a android based chat application using ejabberd-17.01 server on windows. Using Smack 4.1 for android. I want to search for users using following code
public void searchUser(){
try {
UserSearchManager userSearchManager = new UserSearchManager(connection);
Collection<String> services = userSearchManager.getSearchServices();
if(services.isEmpty()){
return;
}
Form searchForm = userSearchManager.getSearchForm(services.iterator().next());
Form answerForm = searchForm.createAnswerForm();
answerForm.setAnswer("Name", true);
answerForm.setAnswer("search", "test"); // here i'm passsing the Text value to search
ReportedData resultData;
resultData = userSearchManager.getSearchResults(answerForm, "search." + connection.getServiceName());
Iterator<ReportedData.Row> it = (Iterator)resultData.getRows();
ReportedData.Row row = null;
while (it.hasNext()) {
String value = it.next().toString();
Log.i("Iteartor values......", " " + value);
System.out.println("Jabber_id :" + row.getValues("jid").toString());
System.out.println("Name :" + row.getValues("Name").toString());
row = it.next();
}
}
catch (SmackException.NoResponseException | SmackException.NotConnectedException | XMPPException.XMPPErrorException | XmppStringprepException e){
Log.w("app" , e.toString());
}
}
my problem is that
userSearchManager.getSearchServices()
returns zero values. I read about that i should initialize smack for android by calling SmackAndroid.init(context) before connection but whenever i write this it only says create class SmackAndroid(context) and does not show any import.
compile "org.igniterealtime.smack:smack-im:4.1.0"
compile "org.igniterealtime.smack:smack-android:4.1.0"
compile "org.igniterealtime.smack:smack-tcp:4.1.0"
compile "org.igniterealtime.smack:smack-extensions:4.1.0"
i also tested all dependencies mentioned in smack document but cant find this method. What am i missing or is there any other way to search register user in ejabberd. Also i want to know do i need to add any module to ejabberd for searching purpose like search module is added manually in openfire server?
Related
I'm learning Javamail these days following this website:
http://www.tutorialspoint.com/javamail_api/
I test the sending & added some extra stuff because it was on Android & it worked!
But things have changed completely when I tried to follow receiving email Tutorial which makes me wonder..
Is it possible to make this code :
https://www.tutorialspoint.com/javamail_api/javamail_api_fetching_emails.htm
works on android but using XML interface?!
You can use the following code:
public static void receiveEmail(String pop3Host, String storeType, user, String password) {
try {
//1) get the session object
Properties properties = new Properties();
properties.put("mail.pop3.host", pop3Host);
Session emailSession = Session.getDefaultInstance(properties);
//2) create the POP3 store object and connect with the pop server
POP3Store emailStore = (POP3Store) emailSession.getStore(storeType);
emailStore.connect(user, password);
//3) create the folder object and open it
Folder emailFolder = emailStore.getFolder("INBOX");
emailFolder.open(Folder.READ_ONLY);
//4) retrieve the messages from the folder in an array and print it
Message[] messages = emailFolder.getMessages();
for (int i = 0; i < messages.length; i++) {
Message message = messages[i];
System.out.println("---------------------------------");
System.out.println("Email Number " + (i + 1));
System.out.println("Subject: " + message.getSubject());
System.out.println("From: " + message.getFrom()[0]);
System.out.println("Text: " + message.getContent().toString());
}
//5) close the store and folder objects
emailFolder.close(false);
emailStore.close();
} catch (NoSuchProviderException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
call your method passing some parameters:
String host = "pop.gmail.com";//change accordingly
String mailStoreType = "pop3";
String username= "example#gmail.com";
String password= "xxxxx";//change accordingly
receiveEmail(host, mailStoreType, username, password);
Source: Steps for receiving the email using JavaMail API
I am facing the issue to retrieve offline message in android apps using smack api from XMPP Mongoose server. As per the code flow Try to retrive offline message when user become login in xmpp mongoose server before sending available presence to mongoose server.
Tried with two different way to retrieve offline message still not able to find working solution for the same. Both method which i tried those all things are explain below with detail.
Below Api we are using for the xmpp connection and all other extension :
// Smack (XMPP Client Library)
compile 'org.igniterealtime.smack:smack-android:4.1.6'
compile 'org.igniterealtime.smack:smack-tcp:4.1.6'
compile 'org.igniterealtime.smack:smack-im:4.1.6'
compile 'org.igniterealtime.smack:smack-android-extensions:4.1.6'
Tried retrive offline message Using offlineMessageManager
Below is code which I tried to retrieve offline message after login and before send available presence to server
try {
Boolean isFelxibleRetrievalSupport = offlineMessageManager.supportsFlexibleRetrieval();
Iterator i = (Iterator) offlineMessageManager.getMessages();
while (i.hasNext())
{
Message msg = i.next();
System.out.println("Got text [" + msg.getBody() + "] from [" + msg.getFrom() + "]");
} catch (XMPPException e)
{
System.out.println("Error Offline Message.");
e.printStackTrace();
}
catch (SmackException.NotConnectedException e)
{
System.out.println("Error Offline Message. No connection");
e.printStackTrace();
}
catch (SmackException.NoResponseException e)
{
System.out.println("Error Offline Message. No Reponse");
e.printStackTrace();
}
Issue case 1:
Below is exception detail which generate when above code execute
I got Exception when execute below line of code.
Iterator i = (Iterator) offlineMessageManager.getMessages();
Below is exception description which Generate when above line execute
org.jivesoftware.smack.XMPPException$XMPPErrorException: XMPPError: service-unavailable - cancel
Issue Case 2:
If checking is Flexible offline message supported from android code using smack from xmmp mongoose server so i got false value. Below is code which i used for testing.
Boolean isFelxibleRetrievalSupport = offlineMessageManager.supportsFlexibleRetrieval();
Issue Case 3:
When I try to retrieve supported features using below method using smack code like below.
ServiceDiscoveryManager manager = ServiceDiscoveryManager
.getInstanceFor(connection);
List AllFetures = manager.getFeatures();
Below is features list which i retrived:
http://jabber.org/protocol/bytestreams,
jabber:iq:privacy, urn:xmpp:ping,
http://jabber.org/protocol/commands,
jabber:iq:version,
jabber:iq:last,
http://jabber.org/protocol/xdata-validate,
http://jabber.org/protocol/xhtml-im,
vcard-temp,
http://jabber.org/protocol/chatstates,
urn:xmpp:receipts, urn:xmpp:time,
http://jabber.org/protocol/xdata-layout,
http://jabber.org/protocol/muc,
http://jabber.org/protocol/disco#items,
http://jabber.org/protocol/disco#info,
http://jabber.org/protocol/caps,
jabber:x:data
Tried to retreive offline message Using package listener from XMPP MongooseIM
below is code which i tried using package listener from smack api 4.1.6.
private static final StanzaFilter MESSAGE_PACKET_FILTER= new OrFilter(StanzaTypeFilter.MESSAGE);
configuration = XMPPTCPConnectionConfiguration.builder()
.setServiceName(SERVICE_NAME)
.setHost(KDevelopmentXMPPServer)
.setPort(PORT)
.setSendPresence(false)
.build();
// Create Connection object of xmpp connection with configured detail
connection = new XMPPTCPConnection(configuration);
connection.addAsyncStanzaListener(new StanzaListener() {
#Override
public void processPacket(Stanza packet) throws SmackException.NotConnectedException {
Log.d("CheckPacket", "OfflineMEssage");
Message message = (Message) packet;
if (message != null) {
if (message.getBody() != null) {
Log.i("XMPPClient", "Got text [" + message.getBody()
+ "] from [" + message.getFrom() + "]");
}
}
}
}, MESSAGE_PACKET_FILTER);
connection.login(user, password);
Thanks In Advance, Please anybody help me for best working solution for my critical issue.
The issue is in trying to fetch offline messages before
sending the initial presence. XEP-0160 states:
When the recipient next sends non-negative available presence to the server, the server delivers the message to the resource that has sent that presence. [...]
MongooseIM works with accordance to this recommendation.
You already pointed out what is signaled by isFlexibleRetrievalSupport - the server does not support flexible offline message retrieval.
I know I am writing this too late, but if someone like me who is hunting for same query as in "how to save and get archive messages in android app" can refer this answer :
Note :
this is implemented in the lastest version
Requrirements :
lastest openfire as of now 4.1.4
install Archives plugin in openfire
MamManager mamManager = MamManager.getInstanceFor(connection);
boolean isSupported = mamManager.isSupportedByServer();
if (isSupported) {
MamManager.MamQueryResult mamQueryResult = mamManager.queryArchive(500);
List<Forwarded> forwardedMessages = mamQueryResult.forwardedMessages;
Forwarded d = forwardedMessages.get(0);
}
Please Refer : Documention for MamManager Class
I am working on android chat application using ejabberd 2.1.11. For searching whether a particular user exists or not i'm using UserSearchManager
public boolean isUserExists(IrishContact ic) {
try {
UserSearchManager search = new UserSearchManager(connection);
Collection<String> services = search.getSearchServices();
if (services.isEmpty()) {
Log.v("IrishuserSearch ", "no service found");
}
Log.v("service name: ", connection.getServiceName());
Form searchForm = search.getSearchForm("vjud."
+ connection.getServiceName());
Form answerForm = searchForm.createAnswerForm();
answerForm.setAnswer("Username", true);
answerForm.setAnswer("search", ic.getPhoneNumber());
org.jivesoftware.smackx.ReportedData data = search
.getSearchResults(answerForm,
"search." + connection.getServiceName());
if (data.getRows() != null) {
Iterator<Row> it = data.getRows();
if (it.hasNext()) {
return true;
} else
return false;
} else
return false;
} catch (XMPPException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
On the basis on this answer
i'm using
Form searchForm = search.getSearchForm("vjud."
+ connection.getServiceName());
instead of
Form searchForm =
search.getSearchForm("search."+connection.getServiceName());
the later part was giving 503 service not found exception so i changed my code and it got solved, but now the problem is I am getting IllegalArgumentException at
answerForm.setAnswer("Username", true);
I am using asmack-android-8-0.8.10.
STACK_TRACE=java.lang.IllegalArgumentException: Field not found for the specified variable name.
at org.jivesoftware.smackx.Form.setAnswer(Form.java:258)
at com.irishtalk.utilities.IrishUserSearch.isUserExists(IrishUserSearch.java:42)
at com.irishtalk.utilities.IrishContactsHelper.addContactToDefaultRoster(IrishContactsHelper.java:51)
at com.irishtalk.utilities.IrishContactsHelper.getRoster(IrishContactsHelper.java:32)
at com.irishtalk.service.IXmppAidlStub$1.run(IXmppAidlStub.java:221)
can anyone help me out why this is happening? Thanks
Ejabberd does not support the field value "Username". It is used in openfire. Please use the field vale "user" and pass arguments as string in ejabberd. Thanks. Please Vote up if your error is solved.
Am using the Xabber open source project and am able to create a new group, But it always says: This room is locked from entry until configuration is confirmed. I tried to set a default configuration but it throws me exception: 401 not authorized. Whats exactly the problem.
final MultiUserChat multiUserChat;
try {
multiUserChat = new MultiUserChat(xmppConnection, room);
// CHANAKYA: set default config for the MUC
// Send an empty room configuration form which indicates that we want
// an instant room
try {
multiUserChat.sendConfigurationForm(new Form(Form.TYPE_SUBMIT));
} catch (XMPPException e) {
e.printStackTrace();
}
I was also facing the same error. Here I modified the code and it's work for me.
Error 401 is not authorized error when we are calling the any getConfigurationForm(), without joining it.
multiUserChat.join(nickname, password);
setConfig(multiUserChat); // Here I am calling submit form
private void setConfig(MultiUserChat multiUserChat) {
try {
Form form = multiUserChat.getConfigurationForm();
Form submitForm = form.createAnswerForm();
for (Iterator<FormField> fields = submitForm.getFields(); fields
.hasNext();) {
FormField field = (FormField) fields.next();
if (!FormField.Type.hidden.equals(field.getType())
&& field.getVariable() != null) {
submitForm.setDefaultAnswer(field.getVariable());
}
}
submitForm.setAnswer("muc#roomconfig_publicroom", true);
submitForm.setAnswer("muc#roomconfig_persistentroom", true);
multiUserChat.sendConfigurationForm(submitForm);
} catch (Exception e) {
e.printStackTrace();
}
}
And now I am able to successfully submit the form without any exception. Hope this will work for you.
You must have permissions to set the configuration. This can usually be changed in the server settings. If you have Openfire for example you should go to Group Chat>Group chat settings>Click your Group Chat service>Room Creation Permissions or Administrators.
You are probably unable to change this client side, it's only possible if you have access to the server you are trying to connect to.
I am making gtalk Im using smack 3.2.1 API.
I am stuck with group chat implementation.
here is my code for initiate a group chat:
conf = new MultiUserChat(connection, "room#groupchat.google.com"); // create object of multiserchat class
try
{ // Create the room
conf.create("room#groupchat.google.com");
// Send an empty room configuration form which indicates that we want an instant room
conf.sendConfigurationForm(new Form(Form.TYPE_SUBMIT));
}
catch (XMPPException e)
{
Toast.makeText(this, "Error in create room: "+e.toString(), Toast.LENGTH_LONG).show();
}
conf.invite("userid", "Invitation for group chat");
But I am getting error: Service not Available (503)
Thanks in advance.
Room should be -- private-chat-
And there is no need to create room.
Simply join the room with users name and password. That will satishfy:
Code snippet:
String room = "private-chat-" + UUID.randomUUID().toString();
room = room + "#groupchat.google.com";
MultiUserChat muc = new MultiUserChat(cc, room);
muc.join("username", "password");
muc.invite("username", "hi");
Let me know if this works.