java - Android socket app takes too much time to load -
i able connect server using socket. problem activity takes time load. changing textview every line in network class using handler , runnable. seems everythhing done , output... have made network thread thinking run in background , app load instantly. suggestions can do?
main activity
package com.abhishek.ally2; import android.os.bundle; import android.os.handler; import android.os.strictmode; import android.annotation.suppresslint; import android.app.activity; import android.view.menu; import android.widget.textview; public class mainactivity extends activity { textview textview; string header; @suppresslint("newapi") @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); if (android.os.build.version.sdk_int > 9) { strictmode.threadpolicy policy = new strictmode.threadpolicy.builder().permitall().build(); strictmode.setthreadpolicy(policy); } textview = (textview)findviewbyid(r.id.status); header = "get /ally.php http/1.0\nhost: easyvote.co.in\n\n"; handler handler = new handler(); thread connect = new network("easyvote.co.in", 80, header, textview, handler); connect.start(); ((network) connect).statusshow(); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.main, menu); return true; } }
network class
package com.abhishek.ally2; import java.io.datainputstream; import java.io.dataoutputstream; import java.io.ioexception; import java.net.socket; import java.net.unknownhostexception; import android.os.handler; import android.util.log; import android.widget.textview; public class network extends thread { socket client = null; dataoutputstream os = null; datainputstream = null; string host; int port; string responseline; string data; string lastmsg; public boolean status; handler handler; textview txt; runnable r; int count; network(string host, int port, string data, textview status, handler handler) { this.status = false; this.txt = status; this.handler = handler; this.host = host; this.port = port; this.data = data; count = 0; //while(!status) r = new runnable(){ @override public void run() { //log.d("response", responseline); txt.settext("connecting..."); } }; this.handler.post(r); } @suppresswarnings({ "deprecation" }) public void statusshow() { try { client = new socket(host, port); os = new dataoutputstream(client.getoutputstream()); = new datainputstream(client.getinputstream()); if(client != null && os != null && != null) { os.writebytes(data); while((responseline = is.readline()) != null) { lastmsg = responseline; try { thread.sleep(1000); } catch (interruptedexception e) { // todo auto-generated catch block e.printstacktrace(); } log.d("response", responseline); } os.close(); is.close(); client.close(); log.d("response", lastmsg); r = new runnable(){ @override public void run() { txt.settext(lastmsg); } }; this.handler.post(r); } } catch (unknownhostexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } } }
thanks in advance :)
you calling method statusshow()
oncreate()
method of mainactivity
.
hence function executed on ui/main thread. try calling method inside runnable r
. not block main thread.
edit: might work (i haven't tested it)
although code below might work, doing wrong. way doing it, don't need separate class (let alone subclass thread
). write runnable r
in mainactivity
entirely , post
there.
main activity:
protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); if (android.os.build.version.sdk_int > 9) { strictmode.threadpolicy policy = new strictmode.threadpolicy.builder().permitall().build(); strictmode.setthreadpolicy(policy); } textview = (textview)findviewbyid(r.id.status); header = "get /ally.php http/1.0\nhost: easyvote.co.in\n\n"; handler handler = new handler(); thread connect = new network("easyvote.co.in", 80, header, textview, handler); connect.start(); }
in network class:
network(string host, int port, string data, textview status, handler handler) { this.status = false; this.txt = status; this.handler = handler; this.host = host; this.port = port; this.data = data; count = 0; //while(!status) r = new runnable(){ @override public void run() { //log.d("response", responseline); txt.settext("connecting..."); string result = statusshow(); txt.settext(result); } }; this.handler.post(r); } @suppresswarnings({ "deprecation" }) public void statusshow() { try { client = new socket(host, port); os = new dataoutputstream(client.getoutputstream()); = new datainputstream(client.getinputstream()); if(client != null && os != null && != null) { os.writebytes(data); while((responseline = is.readline()) != null) { lastmsg = responseline; try { thread.sleep(1000); } catch (interruptedexception e) { // todo auto-generated catch block e.printstacktrace(); } log.d("response", responseline); } os.close(); is.close(); client.close(); log.d("response", lastmsg); return lastmsg; } } catch (unknownhostexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } return "error"; }
Comments
Post a Comment