java - XMPP and ANDROID -
i trying make xmpp client android using
http://davanum.wordpress.com/2007/12/31/android-just-use-smack-api-for-xmpp/
i have been facing multiple issues in it
first of host name , service name both same i.e. web.vlivetech.com
then not know jar file ned include in lib folder have included
1- asmack-android-7 giving me error on classes noclassdeffound
then have removed , included
smack-3.4.1-0cec571.jar
but giving me error
networkonmainthread
here code xmppclient
public class xmppclient extends activity { private arraylist<string> messages = new arraylist(); private handler mhandler = new handler(); private settingsdialog mdialog; private edittext mrecipient; private edittext msendtext; private listview mlist; private xmppconnection connection; /** * called activity first created. */ @override public void oncreate(bundle icicle) { super.oncreate(icicle); setcontentview(r.layout.activity_xmppclient); mrecipient = (edittext) this.findviewbyid(r.id.recipient); msendtext = (edittext) this.findviewbyid(r.id.sendtext); mlist = (listview) this.findviewbyid(r.id.listmessages); setlistadapter(); // dialog getting xmpp settings mdialog = new settingsdialog(this); // set listener show settings dialog button setup = (button) this.findviewbyid(r.id.setup); setup.setonclicklistener(new view.onclicklistener() { public void onclick(view view) { mhandler.post(new runnable() { public void run() { mdialog.show(); } }); } }); // set listener send chat text message button send = (button) this.findviewbyid(r.id.send); send.setonclicklistener(new view.onclicklistener() { public void onclick(view view) { string = mrecipient.gettext().tostring(); string text = msendtext.gettext().tostring(); log.i("xmppclient", "sending text [" + text + "] [" + + "]"); message msg = new message(to, message.type.chat); msg.setbody(text); connection.sendpacket(msg); messages.add(connection.getuser() + ":"); messages.add(text); setlistadapter(); } }); } /** * called settings dialog when connection establised xmpp server * * @param connection */ public void setconnection (xmppconnection connection) { this.connection = connection; if (connection != null) { // add packet listener messages sent packetfilter filter = new messagetypefilter(message.type.chat); 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() + "] [" + fromname + "]"); messages.add(fromname + ":"); messages.add(message.getbody()); // add incoming message list view mhandler.post(new runnable() { public void run() { setlistadapter(); } }); } } }, filter); } } private void setlistadapter () { arrayadapter<string> adapter = new arrayadapter<string>(this, android.r.layout.two_line_list_item, messages); mlist.setadapter(adapter); } }
and here code settingsdiologue
public class settingsdialog extends dialog implements android.view.view.onclicklistener { private xmppclient xmppclient; public settingsdialog(xmppclient xmppclient) { super(xmppclient); this.xmppclient = xmppclient; } protected void onstart() { super.onstart(); setcontentview(r.layout.settings); getwindow().setflags(4, 4); settitle("xmpp settings"); button ok = (button) findviewbyid(r.id.ok); ok.setonclicklistener(this); } public void onclick(view v) { 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 = "khubaib"; //gettext(r.id.userid); string password = "khubaib"; //gettext(r.id.password); // create connection connectionconfiguration connconfig = new connectionconfiguration(host, integer.parseint(port)); xmppconnection connection = new xmppconnection(connconfig); try { connection.connect(); log.i("xmppclient", "[settingsdialog] connected " + connection.gethost()); } catch (xmppexception ex) { log.e("xmppclient", "[settingsdialog] failed connect " + connection.gethost()); xmppclient.setconnection(null); } try { connection.login(username, password); log.i("xmppclient", "logged in " + connection.getuser()); // set status available presence presence = new presence(presence.type.available); connection.sendpacket(presence); xmppclient.setconnection(connection); } catch (xmppexception ex) { log.e("xmppclient", "[settingsdialog] failed log in " + username); xmppclient.setconnection(null); } dismiss(); } private string gettext(int id) { edittext widget = (edittext) this.findviewbyid(id); return widget.gettext().tostring(); }
am missing something? or there problem jar file? suggestions?
here latest error log
03-06 16:11:22.055: e/androidruntime(6196): fatal exception: main 03-06 16:11:22.055: e/androidruntime(6196): android.os.networkonmainthreadexception 03-06 16:11:22.055: e/androidruntime(6196): @ android.os.strictmode$androidblockguardpolicy.onnetwork(strictmode.java:1108) 03-06 16:11:22.055: e/androidruntime(6196): @ java.net.inetaddress.lookuphostbyname(inetaddress.java:391) 03-06 16:11:22.055: e/androidruntime(6196): @ java.net.inetaddress.getallbynameimpl(inetaddress.java:242) 03-06 16:11:22.055: e/androidruntime(6196): @ java.net.inetaddress.getbyname(inetaddress.java:295) 03-06 16:11:22.055: e/androidruntime(6196): @ java.net.inetsocketaddress.<init>(inetsocketaddress.java:105) 03-06 16:11:22.055: e/androidruntime(6196): @ java.net.inetsocketaddress.<init>(inetsocketaddress.java:90) 03-06 16:11:22.055: e/androidruntime(6196): @ org.jivesoftware.smack.proxy.directsocketfactory.createsocket(directsocketfactory.java:45) 03-06 16:11:22.055: e/androidruntime(6196): @ org.jivesoftware.smack.xmppconnection.connectusingconfiguration(xmppconnection.java:557) 03-06 16:11:22.055: e/androidruntime(6196): @ org.jivesoftware.smack.xmppconnection.connect(xmppconnection.java:1010) 03-06 16:11:22.055: e/androidruntime(6196): @ com.example.test.settingsdialog.onclick(settingsdialog.java:46) 03-06 16:11:22.055: e/androidruntime(6196): @ android.view.view.performclick(view.java:3574) 03-06 16:11:22.055: e/androidruntime(6196): @ android.view.view$performclick.run(view.java:14293) 03-06 16:11:22.055: e/androidruntime(6196): @ android.os.handler.handlecallback(handler.java:605) 03-06 16:11:22.055: e/androidruntime(6196): @ android.os.handler.dispatchmessage(handler.java:92) 03-06 16:11:22.055: e/androidruntime(6196): @ android.os.looper.loop(looper.java:137) 03-06 16:11:22.055: e/androidruntime(6196): @ android.app.activitythread.main(activitythread.java:4448) 03-06 16:11:22.055: e/androidruntime(6196): @ java.lang.reflect.method.invokenative(native method) 03-06 16:11:22.055: e/androidruntime(6196): @ java.lang.reflect.method.invoke(method.java:511) 03-06 16:11:22.055: e/androidruntime(6196): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:823) 03-06 16:11:22.055: e/androidruntime(6196): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:590) 03-06 16:11:22.055: e/androidruntime(6196): @ dalvik.system.nativestart.main(native method)
updated log file after implementing way suggested naveen
03-06 17:08:26.235: e/androidruntime(12037): fatal exception: asynctask #1 03-06 17:08:26.235: e/androidruntime(12037): java.lang.runtimeexception: error occured while executing doinbackground() 03-06 17:08:26.235: e/androidruntime(12037): @ android.os.asynctask$3.done(asynctask.java:278) 03-06 17:08:26.235: e/androidruntime(12037): @ java.util.concurrent.futuretask$sync.innersetexception(futuretask.java:273) 03-06 17:08:26.235: e/androidruntime(12037): @ java.util.concurrent.futuretask.setexception(futuretask.java:124) 03-06 17:08:26.235: e/androidruntime(12037): @ java.util.concurrent.futuretask$sync.innerrun(futuretask.java:307) 03-06 17:08:26.235: e/androidruntime(12037): @ java.util.concurrent.futuretask.run(futuretask.java:137) 03-06 17:08:26.235: e/androidruntime(12037): @ android.os.asynctask$serialexecutor$1.run(asynctask.java:208) 03-06 17:08:26.235: e/androidruntime(12037): @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1076) 03-06 17:08:26.235: e/androidruntime(12037): @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:569) 03-06 17:08:26.235: e/androidruntime(12037): @ java.lang.thread.run(thread.java:856) 03-06 17:08:26.235: e/androidruntime(12037): caused by: java.lang.nullpointerexception 03-06 17:08:26.235: e/androidruntime(12037): @ com.example.test.networkoperation.doinbackground(networkoperation.java:33) 03-06 17:08:26.235: e/androidruntime(12037): @ com.example.test.networkoperation.doinbackground(networkoperation.java:1)
you need connect internet other main thread i.e asynctask
or thread
.
if run network operation on main thread can networkonmainthreadexception
// button click public void onclick(view v) { new networkoperation().execute(); dialog.dismiss(); } private class networkoperation extends asynctask<string, void, string> { @override 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 = "khubaib"; //gettext(r.id.userid); string password = "khubaib"; //gettext(r.id.password); // create connection connectionconfiguration connconfig = new connectionconfiguration(host, integer.parseint(port)); xmppconnection connection = new xmppconnection(connconfig); try { connection.connect(); log.i("xmppclient", "[settingsdialog] connected " + connection.gethost()); } catch (xmppexception ex) { log.e("xmppclient", "[settingsdialog] failed connect " + connection.gethost()); xmppclient.setconnection(null); } try { connection.login(username, password); log.i("xmppclient", "logged in " + connection.getuser()); // set status available presence presence = new presence(presence.type.available); connection.sendpacket(presence); xmppclient.setconnection(connection); } catch (xmppexception ex) { log.e("xmppclient", "[settingsdialog] failed log in " + username); xmppclient.setconnection(null); } return null; } @override protected void onpostexecute(string result) { } }
Comments
Post a Comment