我已将我的应用更改为使用Smack 4.2替换Smack 4.1
但是我在config.setServiceName();
中收到错误此方法的参数已从String更改为DomainBareJid。 我不知道如何在这个新的Smack中使用DomainBareJid和setServiceName。
XMPPTCPConnectionConfiguration.Builder config = XMPPTCPConnectionConfiguration
.builder();
config.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);
config.setServiceName("What should i put here?");
config.setHost(serverAddress);
config.setPort(5222);
config.setDebuggerEnabled(true);
connection = new XMPPTCPConnection(config.build());
connection.setUseStreamManagement(true);
如果我没有设置服务名称,那么我会收到以下错误:
03-28 13:44:29.834: E/AndroidRuntime(7104): FATAL EXCEPTION: main
03-28 13:44:29.834: E/AndroidRuntime(7104): java.lang.RuntimeException: Unable to create service com.marothiatechs.cpm.MyService: java.lang.IllegalArgumentException: Must provide XMPP service name
03-28 13:44:29.834: E/AndroidRuntime(7104): at android.app.ActivityThread.handleCreateService(ActivityThread.java:2715)
03-28 13:44:29.834: E/AndroidRuntime(7104): at android.app.ActivityThread.access$1600(ActivityThread.java:153)
03-28 13:44:29.834: E/AndroidRuntime(7104): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1351)
03-28 13:44:29.834: E/AndroidRuntime(7104): at android.os.Handler.dispatchMessage(Handler.java:99)
03-28 13:44:29.834: E/AndroidRuntime(7104): at android.os.Looper.loop(Looper.java:137)
03-28 13:44:29.834: E/AndroidRuntime(7104): at android.app.ActivityThread.main(ActivityThread.java:5289)
03-28 13:44:29.834: E/AndroidRuntime(7104): at java.lang.reflect.Method.invokeNative(Native Method)
03-28 13:44:29.834: E/AndroidRuntime(7104): at java.lang.reflect.Method.invoke(Method.java:525)
03-28 13:44:29.834: E/AndroidRuntime(7104): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
03-28 13:44:29.834: E/AndroidRuntime(7104): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
03-28 13:44:29.834: E/AndroidRuntime(7104): at dalvik.system.NativeStart.main(Native Method)
03-28 13:44:29.834: E/AndroidRuntime(7104): Caused by: java.lang.IllegalArgumentException: Must provide XMPP service name
03-28 13:44:29.834: E/AndroidRuntime(7104): at org.jivesoftware.smack.ConnectionConfiguration.<init>(ConnectionConfiguration.java:106)
03-28 13:44:29.834: E/AndroidRuntime(7104): at org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration.<init>(XMPPTCPConnectionConfiguration.java:52)
03-28 13:44:29.834: E/AndroidRuntime(7104): at org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration.<init>(XMPPTCPConnectionConfiguration.java:36)
03-28 13:44:29.834: E/AndroidRuntime(7104): at org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration$Builder.build(XMPPTCPConnectionConfiguration.java:126)
03-28 13:44:29.834: E/AndroidRuntime(7104): at com.marothiatechs.cpm.MyXMPP.initialiseConnection(MyXMPP.java:134)
03-28 13:44:29.834: E/AndroidRuntime(7104): at com.marothiatechs.cpm.MyXMPP.init(MyXMPP.java:119)
03-28 13:44:29.834: E/AndroidRuntime(7104): at com.marothiatechs.cpm.MyXMPP.<init>(MyXMPP.java:84)
03-28 13:44:29.834: E/AndroidRuntime(7104): at com.marothiatechs.cpm.MyXMPP.getInstance(MyXMPP.java:93)
03-28 13:44:29.834: E/AndroidRuntime(7104): at com.marothiatechs.cpm.MyService.onCreate(MyService.java:74)
03-28 13:44:29.834: E/AndroidRuntime(7104): at android.app.ActivityThread.handleCreateService(ActivityThread.java:2705)
请提供一些示例代码来设置serviceName。
答案 0 :(得分:17)
DomainBareJid serviceName = JidCreate.domainBareFrom("example.org");
config.setServiceName(serviceName);
答案 1 :(得分:2)
服务名称应该是您提供的主机名。 例如:
config.setServiceName("gtalk.com");
答案 2 :(得分:0)
background-color: $primary;
我已经对此进行了测试,并且工作正常。
答案 3 :(得分:0)
以下是使用Smack 4.4的方法
XMPPTCPConnectionConfiguration conf = XMPPTCPConnectionConfiguration.builder()
.setHostAddress(InetAddress.getByName(host))
.setXmppDomain(JidCreate.domainBareFrom(Domain))
.setUsernameAndPassword("username", "password")
.setPort(5222)
.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled)
.build();
AbstractXMPPConnection connection = new XMPPTCPConnection(conf);
connection.connect();
connection.login();
答案 4 :(得分:-1)
这是我连接的代码,它工作得很好。
DomainBareJid domainBareJid = JidCreate.domainBareFrom(mServiceName) ;
XMPPTCPConnectionConfiguration.Builder builder=
XMPPTCPConnectionConfiguration.builder();
builder.setServiceName(domainBareJid);
builder.setHostAddress(InetAddress.getByName(mServiceName));
builder.setUsernameAndPassword(mUsername, mPassword);
builder.setResource("resource");
builder.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);
mConnection = new XMPPTCPConnection(builder.build());
mConnection.addConnectionListener(this);
mConnection.connect();
mConnection.login();