我正在尝试使用 asmack 和 android 连接到 facebook。我可以连接但无法登录。当我尝试这样做时,我得到了关于super.authenticate();
如何解决此问题的 nullpointerexception,请提供一些示例
日志:
D/tag: connection succeeded
W/System.err: java.lang.NullPointerException
W/System.err: at org.jivesoftware.smack.sasl.SASLMechanism.authenticate(SASLMechanism.java:116)
W/System.err: at CustomSASLDigestMD5Mechanism.authenticate(CustomSASLDigestMD5Mechanism.java:36)
W/System.err: at org.jivesoftware.smack.SASLAuthentication.authenticate(SASLAuthentication.java:319)
W/System.err: at org.jivesoftware.smack.XMPPConnection.login(XMPPConnection.java:204)
W/System.err: at org.jivesoftware.smack.Connection.login(Connection.java:357)
W/System.err: at se.hig.MainActivity.login(MainActivity.java:69)
W/System.err: at se.hig.MainActivity.onCreate(MainActivity.java:24)
W/System.err: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
W/System.err: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
W/System.err: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
W/System.err: at android.app.ActivityThread.access$2200(ActivityThread.java:119)
W/System.err: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
W/System.err: at android.os.Handler.dispatchMessage(Handler.java:99)
07-18 10:55:33.384: W/System.err(255): at android.os.Looper.loop(Looper.java:123)
07-18 10:55:33.384: W/System.err(255): at android.app.ActivityThread.main(ActivityThread.java:4363)
07-18 10:55:33.393: W/System.err(255): at java.lang.reflect.Method.invokeNative(Native Method)
W/System.err: at java.lang.reflect.Method.invoke(Method.java:521)
W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
07-18 10:55:33.393: W/System.err(255): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
W/System.err: at dalvik.system.NativeStart.main(Native Method)
D/tag: login fails exeption :
D/tag: service-unavailable(503)
自定义SASLDigestMD5机制:
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import org.apache.harmony.javax.security.auth.callback.CallbackHandler;
import org.apache.harmony.javax.security.sasl.Sasl;
import org.jivesoftware.smack.SASLAuthentication;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.sasl.SASLMechanism;
public class CustomSASLDigestMD5Mechanism extends SASLMechanism
{
public CustomSASLDigestMD5Mechanism(SASLAuthentication saslAuthentication)
{
super(saslAuthentication);
}
@Override
public void authenticate(String username, String host, String password)
throws IOException, XMPPException
{
this.authenticationId = username;
this.password = password;
this.hostname = host;
String[] mechanisms = { getName() };
Map<String, String> props = new HashMap<String, String>();
sc = Sasl.createSaslClient(mechanisms, null, "xmpp", host, props, this);
super.authenticate();
}
@Override
public void authenticate(String username, String host, CallbackHandler cbh)
throws IOException, XMPPException
{
String[] mechanisms = { getName() };
Map<String, String> props = new HashMap<String, String>();
sc = Sasl.createSaslClient(mechanisms, null, "xmpp", host, props, cbh);
super.authenticate();
}
protected String getName()
{
return "DIGEST-MD5";
}
}
主要活动:
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.SASLAuthentication;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
public class MainActivity extends Activity
{
private XMPPConnection connection;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
connect();
login("username", "password");
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public String connect()
{
ConnectionConfiguration config = new ConnectionConfiguration("chat.facebook.com", 5222);
SASLAuthentication.registerSASLMechanism("DIGEST-MD5", CustomSASLDigestMD5Mechanism.class);
config.setSASLAuthenticationEnabled(true);
config.setTruststorePath("/system/etc/security/cacerts.bks");
config.setTruststoreType("bks");
config.setDebuggerEnabled(false);
connection = new XMPPConnection(config);
try
{
connection.connect();
Log.d("tag", "connection succeeded");
} catch (XMPPException e)
{
Log.d("tag", "connection fails exeption : \n " + e);
}
// fbml = new FBMessageListener(connection);
return connection.getConnectionID();
}
public boolean login(String userName, String password)
{
if ((connection != null) && (connection.isConnected()))
{
try
{
connection.login(userName, password);
} catch (XMPPException e)
{
Log.d("tag", "login fails exeption : \n " + e);
}
return true;
}
return false;
}
}