asmack 搜索好友报空指针解决方案

第一种

在  connection.connect();  建立连接之前

调用这个方法

SmackAndroid.init(this);

第二种

public static void configure(ProviderManager pm) {

		// Private Data Storage
		pm.addIQProvider("query", "jabber:iq:private", new PrivateDataManager.PrivateDataIQProvider());

		// Time
		try {
			pm.addIQProvider("query", "jabber:iq:time", Class.forName("org.jivesoftware.smackx.packet.Time"));
		} catch (ClassNotFoundException e) {
			Log.w("TestClient", "Can't load class for org.jivesoftware.smackx.packet.Time");
		}

		// Roster Exchange
		pm.addExtensionProvider("x", "jabber:x:roster", new RosterExchangeProvider());

		// Message Events
		pm.addExtensionProvider("x", "jabber:x:event", new MessageEventProvider());

		// Chat State
		pm.addExtensionProvider("active", "http://jabber.org/protocol/chatstates", new ChatStateExtension.Provider());
		pm.addExtensionProvider("composing", "http://jabber.org/protocol/chatstates", new ChatStateExtension.Provider());
		pm.addExtensionProvider("paused", "http://jabber.org/protocol/chatstates", new ChatStateExtension.Provider());
		pm.addExtensionProvider("inactive", "http://jabber.org/protocol/chatstates", new ChatStateExtension.Provider());
		pm.addExtensionProvider("gone", "http://jabber.org/protocol/chatstates", new ChatStateExtension.Provider());

		// XHTML
		pm.addExtensionProvider("html", "http://jabber.org/protocol/xhtml-im", new XHTMLExtensionProvider());

		// Group Chat Invitations
		pm.addExtensionProvider("x", "jabber:x:conference", new GroupChatInvitation.Provider());

		// Service Discovery # Items
		pm.addIQProvider("query", "http://jabber.org/protocol/disco#items", new DiscoverItemsProvider());

		// Service Discovery # Info
		pm.addIQProvider("query", "http://jabber.org/protocol/disco#info", new DiscoverInfoProvider());

		// Data Forms
		pm.addExtensionProvider("x", "jabber:x:data", new DataFormProvider());

		// MUC User
		pm.addExtensionProvider("x", "http://jabber.org/protocol/muc#user", new MUCUserProvider());

		// MUC Admin
		pm.addIQProvider("query", "http://jabber.org/protocol/muc#admin", new MUCAdminProvider());

		// MUC Owner
		pm.addIQProvider("query", "http://jabber.org/protocol/muc#owner", new MUCOwnerProvider());

		// Delayed Delivery
		pm.addExtensionProvider("x", "jabber:x:delay", new DelayInformationProvider());

		// Version
		try {
			pm.addIQProvider("query", "jabber:iq:version", Class.forName("org.jivesoftware.smackx.packet.Version"));
		} catch (ClassNotFoundException e) {
			// Not sure what's happening here.
		}

		// VCard
		pm.addIQProvider("vCard", "vcard-temp", new VCardProvider());

		// Offline Message Requests
		pm.addIQProvider("offline", "http://jabber.org/protocol/offline", new OfflineMessageRequest.Provider());

		// Offline Message Indicator
		pm.addExtensionProvider("offline", "http://jabber.org/protocol/offline", new OfflineMessageInfo.Provider());

		// Last Activity
		pm.addIQProvider("query", "jabber:iq:last", new LastActivity.Provider());

		// User Search
		pm.addIQProvider("query", "jabber:iq:search", new UserSearch.Provider());

		// SharedGroupsInfo
		pm.addIQProvider("sharedgroup", "http://www.jivesoftware.org/protocol/sharedgroup", new SharedGroupsInfo.Provider());

		// JEP-33: Extended Stanza Addressing
		pm.addExtensionProvider("addresses", "http://jabber.org/protocol/address", new MultipleAddressesProvider());

		// FileTransfer
		pm.addIQProvider("si", "http://jabber.org/protocol/si", new StreamInitiationProvider());

		pm.addIQProvider("query", "http://jabber.org/protocol/bytestreams", new BytestreamsProvider());

		// Privacy
		pm.addIQProvider("query", "jabber:iq:privacy", new PrivacyProvider());
		pm.addIQProvider("command", "http://jabber.org/protocol/commands", new AdHocCommandDataProvider());
		pm.addExtensionProvider("malformed-action", "http://jabber.org/protocol/commands", new AdHocCommandDataProvider.MalformedActionError());
		pm.addExtensionProvider("bad-locale", "http://jabber.org/protocol/commands", new AdHocCommandDataProvider.BadLocaleError());
		pm.addExtensionProvider("bad-payload", "http://jabber.org/protocol/commands", new AdHocCommandDataProvider.BadPayloadError());
		pm.addExtensionProvider("bad-sessionid", "http://jabber.org/protocol/commands", new AdHocCommandDataProvider.BadSessionIDError());
		pm.addExtensionProvider("session-expired", "http://jabber.org/protocol/commands", new AdHocCommandDataProvider.SessionExpiredError());
	}

在connection.connect();  建立连接之前

调用

configure(ProviderManager.getInstance());
/**
	 * 查询用户
	 *
	 * @param connection
	 * @param serverDomain
	 * @param userName
	 * @return
	 * @throws XMPPException
	 */
	public static List<UserBean> searchUsers(XMPPConnection connection,String userName) throws XMPPException
    {
        List<UserBean> results = new ArrayList<UserBean>();
        System.out.println("查询开始..............."+connection.getHost()+connection.getServiceName());
        UserSearchManager usm = new UserSearchManager(connection);
        Form searchForm = usm.getSearchForm("search."+connection.getServiceName());
        Form answerForm = searchForm.createAnswerForm();
        answerForm.setAnswer("Username", true);
        answerForm.setAnswer("search", userName);
        ReportedData data = usm.getSearchResults(answerForm, "search."+connection.getServiceName());
         Iterator<Row> it = data.getRows();
         Row row = null;
         UserBean user = null;
         while(it.hasNext())  {
             user = new UserBean();
             row = it.next();
             user.setUsername(row.getValues("Username").next().toString());
             user.setName(row.getValues("Name").next().toString());
             user.setEmail(row.getValues("Email").next().toString());
             System.out.println(row.getValues("Username").next());
             System.out.println(row.getValues("Name").next());
             System.out.println(row.getValues("Email").next());
             results.add(user);
             //若存在,则有返回,UserName一定非空,其他两个若是有设,一定非空
         }
         Toast.makeText(context, results.size()+"",1000).show();
         return results;
    }  
时间: 2024-11-06 09:52:59

asmack 搜索好友报空指针解决方案的相关文章

遇到的Fragment中使用setAdapter()设置ListView报空指针解决方案

场景是这样,底部4个tab导航栏.用的fragment. 但其中一个fragmentActivity1中使用ListVIew的setAdapter()方法时,总是报NullPointerException错误. 一整天都没有解决,晚上看国外的论坛上,看到对于ListView的深入讲解,才忽然理解了问题所在. 对于ListView,刚开始写应用的时候都是拿来就用,没有深入了解过它的本质.从博友上摘下来一段话: 一个ListView通常有两个职责. (1)将数据填充到布局. (2)处理用户的选择点击

终极解决方案 at org.apache.jsp.index_jsp._jspInit(index_jsp.java:22) 报空指针

今天使用maven建立webapp.发布后所有的jsp页面都会 at org.apache.jsp.index_jsp._jspInit(index_jsp.java:22) 报空指针 错误. 百度到引起他的原因是项目中对于servlet-api和jsp-api的引用和Tomcat的包冲突了. 所以,解决方式为:将项目中对于servlet-api和jsp-api的依赖包引用都标以<scope>provided</scope> 这两个依赖包不应该随项目进行打包只在compile和te

SpringJUnit4测试--测试无反应/控制台报空指针的解决---junit的jar冲突!

前言: 前些日子碰到一个诡异的问题--用springJUnit进行测试,运行方法什么反应也没有,控制台 也没有输出,百度也没有答案--只好暂时作罢.今天我只好用上了排除法,建个测试小项目,将只要能测试的几个Spring相关jar拷进去,测试正常,然后开始与原项目进行对比排除jar,看看到底是哪个/哪些jar惹的祸导致jar冲突,结果真是出乎我的意料--竟然是自家兄弟!原来这个项目中也有个junit-4.4的jar,把它删掉就好了.(项目build path添加junt4依赖时会导入相应jar,与

MyEclipse10破解replaceJar一步报错解决方案

MyEclipse10破解replaceJar一步报错解决方案 前段时间MyEclipse10到期了,于是去找破解.网上倒是有一个给力的破解器--MyEclipse 9.x Crack.不过在按照步骤破解时却意外的报了一个错误:在进行到replaceJar这一步时,按照步骤的说明应该会弹出一个文件选择框,然后选中MyEclipse目录->Common->plugins目录.但是这里却无法弹出文件选择器,此时查看控制台会发现错误信息: 似乎和操作系统有关? 看看按下replaceJar按钮后的处

android URL中文和空格会报错解决方案

url = URLEncoder.encode(urlStr,"utf-8").replaceAll("\\+", "%20"); //encode会将空格替换为+号,所有要讲+号替换为空格的转义%20 url = url.replaceAll("%3A", ":").replaceAll("%2F", "/"); //encode会把url里的/和:这2个符号变成%

getActionBar().setDisplayHomeAsUpEnabled(true)报空指针(已解决)

今天捣鼓了一下午.getActionBar().setDisplayHomeAsUpEnabled(true)总是报空指针.在我的还有一个Android4.4.2的项目中就没有一点问题.我还以为是我自己定义视图的问题.找了一下午.百度也未果. 原因: getActionBar()得到的是null,所以对actionbar设置不论什么操作都肯定会报空指针. 这个是由于我使用了Android6.0 解决方式: 在这就应该用getSupportActionBar().setDisPlayHomeAsU

安装opesntack mysql报错 解决方案

安装opesntack mysql报错 ERROR : Error appeared during Puppet run: 192.168.1.103_mysql.ppError: mysqladmin -u root  password 'f40e1dec1deb43d3' returned 1 instead of one of [0] # rpm -qa | grep -i mysqlmysql-server-5.1.71-1.el6.x86_64perl-DBD-MySQL-4.013-

LNMP安装报错解决方案

configure: error: no acceptable cc found in $PATH 解决方案:yum install -y gcc configure: error: xml2-config not found. Please check your libxml2 installation. 解决方案:yum install -y libxml2-devel configure: error: Cannot find OpenSSL's <evp.h> 解决方案:yum ins

android 控件透明度设置时setAplha()报空指针的问题

必须在android控件里设置android:background=“”属性否则报空指针 xml代码 <TextView android:id="@+id/dialog_back" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#000000" /> java代码 textBa