Note that there are some explanatory texts on larger screens.

plurals
  1. PONot Connected to server(Openfire) using asmack api in android project
    primarykey
    data
    text
    <p>I have a simple code which will show "Available" and "Unavailable" using asmack api, but whenever i click on the button it throws a message of not being connected to server. I am posting my code below.</p> <p>AndroidManifest.xml</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.usingxmpp" android:versionCode="1" android:versionName="1.0" &gt; &lt;uses-sdk android:minSdkVersion="11" android:targetSdkVersion="18" /&gt; &lt;uses-permission android:name="android.permission.INTERNET" /&gt; &lt;application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" &gt; &lt;activity android:name="com.example.usingxmpp.MainActivity" android:label="@string/app_name" &gt; &lt;intent-filter&gt; &lt;action android:name="android.intent.action.MAIN" /&gt; &lt;category android:name="android.intent.category.LAUNCHER" /&gt; &lt;/intent-filter&gt; &lt;/activity&gt; &lt;/application&gt; &lt;/manifest&gt; </code></pre> <p>activity_main.xml</p> <pre><code>&lt;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" &gt; &lt;TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/string_head" /&gt; &lt;Button android:id="@+id/button1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/button1" android:onClick="onClick1" /&gt; &lt;Button android:id="@+id/button2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/button2" android:onClick="onClick2" /&gt; &lt;/LinearLayout&gt; </code></pre> <p>string.xml</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;resources&gt; &lt;string name="app_name"&gt;Usingxmpp&lt;/string&gt; &lt;string name="action_settings"&gt;Settings&lt;/string&gt; &lt;string name="string_head"&gt;Hello world!&lt;/string&gt; &lt;string name="button1"&gt;Avail&lt;/string&gt; &lt;string name="button2"&gt;Unavail&lt;/string&gt; &lt;/resources&gt; </code></pre> <p>MainActivity.java</p> <pre><code>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; } } </code></pre> <p>Logcat </p> <pre><code>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) </code></pre> <p>When I press Avail button the above errors comes in logcat. Please help me in solving this problem of connecting to server. Thanks (Note: Runnning this project on my device)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload