Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am new to XMPP please guide me how to send and receive iq packets in xmpp using android. Here is my code. when i debug my code it will give me this log error

06-12 16:08:22.032: E/AndroidRuntime(4063): FATAL EXCEPTION: AsyncTask #1
06-12 16:08:22.032: E/AndroidRuntime(4063): java.lang.RuntimeException: An error occured while executing doInBackground()

06-12 16:08:22.032: E/AndroidRuntime(4063): at android.os.AsyncTask$3.done(AsyncTask.java:299)
06-12 16:08:22.032: E/AndroidRuntime(4063): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
06-12 16:08:22.032: E/AndroidRuntime(4063): at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
06-12 16:08:22.032: E/AndroidRuntime(4063): at java.util.concurrent.FutureTask.run(FutureTask.java:239)
06-12 16:08:22.032: E/AndroidRuntime(4063): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
06-12 16:08:22.032: E/AndroidRuntime(4063): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
06-12 16:08:22.032: E/AndroidRuntime(4063): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
06-12 16:08:22.032: E/AndroidRuntime(4063): at java.lang.Thread.run(Thread.java:856)
06-12 16:08:22.032: E/AndroidRuntime(4063): Caused by: java.lang.ClassCastException: java.lang.Object[] cannot be cast to java.lang.String[]
06-12 16:08:22.032: E/AndroidRuntime(4063): at com.example.xmppclient.MyTask.doInBackground(MyTask.java:1)
06-12 16:08:22.032: E/AndroidRuntime(4063): at android.os.AsyncTask$2.call(AsyncTask.java:287)
06-12 16:08:22.032: E/AndroidRuntime(4063): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
06-12 16:08:22.032: E/AndroidRuntime(4063): ... 4 more
06-12 16:08:23.982: I/Choreographer(4063): Skipped 98 frames! The application may be doing too much work on its main thread.
06-12 16:08:25.391: I/Choreographer(4063): Skipped 59 frames! The application may be doing too much work on its main thread.

Here is my code
protected String doInBackground(String... urls) 
	     {  
        String host = "web.vlivetech.com";                                  //getText(R.id.host);
        String port =  "5222";                                             //getText(R.id.port);
        String service = "web.vlivetech.com";                             //getText(R.id.service);
        String username =  "has12345";                                   //getText(R.id.userid);
        String password =   "123";                                      //getText(R.id.password);

        // Create a connection
	        ConnectionConfiguration connConfig =
	        		new ConnectionConfiguration(host, Integer.parseInt(port), service);
	        XMPPConnection connection = new XMPPConnection(connConfig);

	        try {
	            connection.connect();
	            Log.i("XMPPClient", "[SettingsDialog] Connected to " + connection.getHost());
	        } catch (XMPPException ex) {
	            Log.e("XMPPClient", "[SettingsDialog] Failed to connect to " + connection.getHost());
	            MainActivity.setConnection(null);
	        }
	        try {
	            connection.login(username, password);
	            Log.i("XMPPClient", "Logged in as " + connection.getUser());

	            // Set the status to available
	            Presence presence = new Presence(Presence.Type.available);
	            connection.sendPacket(presence);
	            MainActivity.setConnection(connection);
	        } catch (XMPPException ex) {
	            Log.e("XMPPClient", "[SettingsDialog] Failed to log in as " + username);
	            MainActivity.setConnection(null);
	        }

	      return null;
	    }
Posted
Updated 12-Jun-14 6:16am
v2
Comments
Member 10810250 12-Jun-14 12:15pm    
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
Log.i("XMPPClient", "onCreate called");
setContentView(R.layout.activity_main);

mRecipient = (EditText) this.findViewById(R.id.recipient);
Log.i("XMPPClient", "mRecipient = " + mRecipient);
mSendText = (EditText) this.findViewById(R.id.sendText);
Log.i("XMPPClient", "mSendText = " + mSendText);
mList = (ListView) this.findViewById(R.id.listMessages);
Log.i("XMPPClient", "mList = " + mList);
setListAdapter();

// Dialog for getting the xmpp settings
mDialog = new SettingsDialog(this);
new MyTask().execute();
// Set a listener to show the settings dialog
Button setup = (Button) this.findViewById(R.id.setup);
setup.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
new MyTask().execute();
}
});

// Set a listener to send a chat text message
Button send = (Button) this.findViewById(R.id.send);
send.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
String to = mRecipient.getText().toString();
String text = mSendText.getText().toString();

Log.i("XMPPClient", "Sending text [" + text + "] to [" + to + "]");
Message msg = new Message(to, Message.Type.chat);
msg.setBody(text);
((org.jivesoftware.smack.XMPPConnection) connection).sendPacket(msg);
messages.add(((org.jivesoftware.smack.XMPPConnection) connection).getUser() + ":");
messages.add(text);
setListAdapter();
}
});
}

/**
* Called by Settings dialog when a connection is establised with the XMPP server
*
* @param connection
*/
public void setConnection
(XMPPConnection
connection) {
this.connection = connection;
if (connection != null) {
// Add a packet listener to get messages sent to us
PacketFilter filter = new MessageTypeFilter(Message.Type.chat);
((Connection) connection).addPacketListener(new PacketListener() {
public void processPacket(Packet packet) {
Message message = (Message) packet;
if (message.getBody() != null) {
String fromName = StringUtils.parseBareAddress(message.getFrom());
Log.i("XMPPClient", "Got text [" + message.getBody() + "] from [" + fromName + "]");
messages.add(fromName + ":");
messages.add(message.getBody());
// Add the incoming message to the list view
mHandler.post(new Runnable() {
public void run() {
setListAdapter();
}
});
}
}
}, filter);
}
}
Member 10810250 12-Jun-14 12:16pm    
here is my main activity code where is created connection

1 solution

It's ClassCastException
06-12 16:08:22.032: E/AndroidRuntime(4063): Caused by: java.lang.ClassCastException: java.lang.Object[] cannot be cast to java.lang.String[]

Find the relevant code, and fix it.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900