我有一个简单的代码,使用asmack api显示“可用”和“不可用”,但每当我点击按钮时,它会抛出一条未连接到服务器的消息。我在下面发布我的代码。
的AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.usingxmpp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.usingxmpp.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
activity_main.xml中
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/string_head" />
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/button1"
android:onClick="onClick1"
/>
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/button2"
android:onClick="onClick2"
/>
</LinearLayout>
string.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Usingxmpp</string>
<string name="action_settings">Settings</string>
<string name="string_head">Hello world!</string>
<string name="button1">Avail</string>
<string name="button2">Unavail</string>
</resources>
MainActivity.java
package com.example.usingxmpp;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.packet.Presence;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends Activity {
ConnectionConfiguration connfig = new ConnectionConfiguration("acer-PC",5222,"acer-pc");
XMPPConnection connection = new XMPPConnection(connfig);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try
{
connection.connect();
connection.login("rahul","rahul");
}
catch(Exception e)
{
e.printStackTrace();
}
}
public void onClick1(View v)
{
new Thread(new Runnable()
{
public void run()
{
try
{
Presence presence = new Presence(Presence.Type.available);
presence.setStatus("I am available");
connection.sendPacket(presence);
//String status = presence.getStatus();
Toast.makeText(getBaseContext(), "You are Available!", Toast.LENGTH_SHORT).show();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
).start();
}
public void onClick2(View v)
{
new Thread(new Runnable()
{
public void run()
{
try
{
Presence presence = new Presence(Presence.Type.unavailable);
presence.setStatus("I am unavailable");
connection.sendPacket(presence);
//String status = presence.getStatus();
Toast.makeText(getBaseContext(), "UnAvailable now!", Toast.LENGTH_SHORT).show();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
).start();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Logcat
12-02 14:41:40.809: W/System.err(5464): java.lang.IllegalStateException: Not connected to server.
12-02 14:41:40.839: W/System.err(5464): at org.jivesoftware.smack.XMPPConnection.login(XMPPConnection.java:194)
12-02 14:41:40.839: W/System.err(5464): at org.jivesoftware.smack.Connection.login(Connection.java:355)
12-02 14:41:40.839: W/System.err(5464): at com.example.usingxmpp.MainActivity$1.run(MainActivity.java:44)
12-02 14:41:40.839: W/System.err(5464): at java.lang.Thread.run(Thread.java:856)
当我按下Avail按钮时,上面的错误会出现在logcat中。 请帮我解决连接服务器的这个问题。 谢谢 (注意:在我的设备上运行此项目)
答案 0 :(得分:0)
问题在于此代码序列:
connection.login("rahul","rahul");
connection.connect();
您需要在之前连接才能登录。听起来很合理,是吗?因此,您需要将代码更改为:
connection.connect();
connection.login("rahul","rahul");
答案 1 :(得分:0)
可能是因为你的android xmpp客户端无法连接到你的主机(在你的情况下是acer-PC)。我提到了安装了xmpp服务器的主机的IP地址,它对我有用。您必须小心使用IP地址,您可以直接使用本地WIFI IP地址,如果您的PC和PC都可以从Android设备连接到xmpp服务器。 android设备正在使用相同的WIFI。如果您使用的是外部IP地址,则可能需要执行一些其他配置,如本link
中所述。