我已完成this1,this2和this3,但仍无法找到问题的解决方案。
我可以知道我的问题在于我在这段代码中所做的是:
这是我到目前为止所做的代码。
public class Registration extends Activity implements OnClickListener {
private final static String SERVER_HOST = "10.0.2.2";
private final static int SERVER_PORT = 5222;
private static final String TAG = null;
private ProviderManager pm;
private EditText username;
private Button btn;
private XMPPConnection connection;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.registration);
try {
initConnection();
} catch (XMPPException e) {
e.printStackTrace();
}
username = (EditText) this.findViewById(R.id.txtregusername);
btn=(Button) this.findViewById(R.id.btncheck);
btn.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch(v.getId())
{
case R.id.btncheck:
try {
check();
} catch (XMPPException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
}
}
private void check() throws XMPPException{
Toast.makeText(this,"Function called",Toast.LENGTH_SHORT).show();
pm.addIQProvider("query","jabber:iq:search", new UserSearch.Provider());
UserSearchManager search = new UserSearchManager(connection);
Form searchForm = search.getSearchForm("search."+connection.getServiceName());
Form answerForm = searchForm.createAnswerForm();
answerForm.setAnswer("Username", true);
Toast.makeText(this,username.getText().toString(),Toast.LENGTH_SHORT).show();
answerForm.setAnswer("search", username.getText().toString());
ReportedData data = search.getSearchResults(answerForm,"search."+connection.getServiceName());
if(data.getRows() != null)
{
Toast.makeText(this,"Username Exists",Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(this,"Username Available",Toast.LENGTH_SHORT).show();
}
}
private void initConnection() throws XMPPException{
ConnectionConfiguration config=new ConnectionConfiguration(SERVER_HOST,SERVER_PORT);
config.setSecurityMode(SecurityMode.enabled);
config.setSASLAuthenticationEnabled(true);
connection=new XMPPConnection(config);
try {
connection.connect();
}
catch (XMPPException e) {
Log.e(TAG, "Error connection to XMPP Server");
e.printStackTrace();
}
}
}
答案 0 :(得分:2)
试试这个。它解决了我的问题。
UserSearchManager search = new UserSearchManager(mXMPPConnection);
Form searchForm = search.getSearchForm("search."+mXMPPConnection.getServiceName());
Form answerForm = searchForm.createAnswerForm();
answerForm.setAnswer("Username", true);
answerForm.setAnswer("search", user);
org.jivesoftware.smackx.ReportedData data = search.getSearchResults(answerForm,"search."+mXMPPConnection.getServiceName());
if(data.getRows() != null)
{
Iterator<Row> it = data.getRows();
while(it.hasNext())
{
Row row = it.next();
Iterator iterator = row.getValues("jid");
if(iterator.hasNext())
{
String value = iterator.next().toString();
Log.i("Iteartor values......"," "+value);
}
//Log.i("Iteartor values......"," "+value);
}
Toast.makeText(_service,"Username Exists",Toast.LENGTH_SHORT).show();
);
}
如果Server没有任何具有该指定名称的entery,那么Itearator没有任何值,并且代码将不会进入(it.hasNext)。
答案 1 :(得分:-1)
您需要先使用 ProviderManager ,否则搜索将无法正常工作。我的工作代码完美无缺,希望能有所帮助:
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.ConnectionConfiguration.SecurityMode;
import org.jivesoftware.smack.PrivacyList;
import org.jivesoftware.smack.PrivacyListManager;
import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.RosterListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.packet.PrivacyItem;
import org.jivesoftware.smack.provider.PrivacyProvider;
import org.jivesoftware.smack.provider.ProviderManager;
import org.jivesoftware.smackx.Form;
import org.jivesoftware.smackx.GroupChatInvitation;
import org.jivesoftware.smackx.PrivateDataManager;
import org.jivesoftware.smackx.ReportedData.Row;
import org.jivesoftware.smackx.bytestreams.socks5.provider.BytestreamsProvider;
import org.jivesoftware.smackx.packet.ChatStateExtension;
import org.jivesoftware.smackx.packet.LastActivity;
import org.jivesoftware.smackx.packet.OfflineMessageInfo;
import org.jivesoftware.smackx.packet.OfflineMessageRequest;
import org.jivesoftware.smackx.packet.SharedGroupsInfo;
import org.jivesoftware.smackx.packet.VCard;
import org.jivesoftware.smackx.provider.AdHocCommandDataProvider;
import org.jivesoftware.smackx.provider.DataFormProvider;
import org.jivesoftware.smackx.provider.DelayInformationProvider;
import org.jivesoftware.smackx.provider.DiscoverInfoProvider;
import org.jivesoftware.smackx.provider.DiscoverItemsProvider;
import org.jivesoftware.smackx.provider.MUCAdminProvider;
import org.jivesoftware.smackx.provider.MUCOwnerProvider;
import org.jivesoftware.smackx.provider.MUCUserProvider;
import org.jivesoftware.smackx.provider.MessageEventProvider;
import org.jivesoftware.smackx.provider.MultipleAddressesProvider;
import org.jivesoftware.smackx.provider.RosterExchangeProvider;
import org.jivesoftware.smackx.provider.StreamInitiationProvider;
import org.jivesoftware.smackx.provider.VCardProvider;
import org.jivesoftware.smackx.provider.XHTMLExtensionProvider;
import org.jivesoftware.smackx.search.UserSearch;
import org.jivesoftware.smackx.search.UserSearchManager;
public static List<String> getUserListBySearch(XMPPConnection mXMPPConnection, String searchString){
ProviderManager.getInstance().addIQProvider("query","jabber:iq:search", new UserSearch.Provider());
List<String> l = new ArrayList<String>();
try {
UserSearchManager search = new UserSearchManager(mXMPPConnection);
Form searchForm = search.getSearchForm("search."+mXMPPConnection.getServiceName());
Form answerForm = searchForm.createAnswerForm();
answerForm.setAnswer("Username", true);
answerForm.setAnswer("search", searchString);
org.jivesoftware.smackx.ReportedData data = search.getSearchResults(answerForm,"search."+mXMPPConnection.getServiceName());
if(data.getRows() != null)
{
Iterator<Row> it = data.getRows();
while(it.hasNext())
{
Row row = it.next();
System.out.println(row);
Iterator iterator = row.getValues("jid");
if(iterator.hasNext())
{
String value = iterator.next().toString();
l.add(value);
System.out.println("Iteartor values......"+value);
}
//Log.i("Iteartor values......"," "+value);
}
System.out.println("UserName Exists");
}
} catch (Exception e) {
System.out.println("Exception in Loading user search"+e);
}
return l;
}