AssyncTask and socket not working in android java -
i'm making simple app client-server in java, want phone recive , send messages pc, i'm on same lan , pc's ip 192.168.1.8, serversocket running on port 7777. reason client android cant connect think because created socket in thread, have read should use asynctask or handle, tried exception too.
class server:
public class myserver implements runnable{ private serversocket server; private objectinputstream in; private objectoutputstream out; private socket clientedconnected; public exampleserverznuc(){ try { server= new serversocket(7777); system.out.println("server opened on port 7777"); } catch (ioexception ex) { logger.getlogger(myserver.class.getname()).log(level.severe, null, ex); } } public void sendmessage(string message){ try { out.writeobject(message); out.flush(); } catch (ioexception ex) { logger.getlogger(myserver.class.getname()).log(level.severe, null, ex); } } public void sendnotifiy(){ try { out.writeobject("send notify"); out.flush(); } catch (ioexception ex) { logger.getlogger(myserver.class.getname()).log(level.severe, null, ex); } } public string getmessage(){ return ""; } public void run(){ system.out.println("method run started"); system.out.println("waiting client connect"); try { socket clientedconnected; clientedconnected = server.accept(); system.out.println("a client connected : "+clientedconnected.getinetaddress().gethostaddress()); in= new objectinputstream(clientedconnected.getinputstream()); out = new objectoutputstream(clientedconnected.getoutputstream()); } catch (ioexception ex) { logger.getlogger(myserver.class.getname()).log(level.severe, null, ex); } system.out.println("end of method run"); } }
this output when create run server:
server opened on port 7777 method run started waiting client connect
client android: class connect server( atm want recive message):
import java.io.ioexception; import java.io.objectinputstream; import java.io.objectoutputstream; import java.io.optionaldataexception; import java.net.socket; import java.net.unknownhostexception; import android.util.log; import android.view.view; import android.widget.textview; public class myconnect implements runnable{ private socket myconnect=null ; private textview txtmessage; private objectinputstream in; private objectoutputstream out; public myconnect( ){ try { // pc's ip, phone connected same lan log.d("inizializating socket",""); myconnect= new socket("192.168.1.8",7777); in = new objectinputstream(myconnect.getinputstream()); out = new objectoutputstream(myconnect.getoutputstream()); } catch (unknownhostexception e) { log.d("my error connecting",e.getmessage()); } catch (ioexception e) { log.d("my error connecting",e.getmessage()); } } @override public void run() { try { string message = (string)in.readobject(); log.d("messaged recived",message); } catch (optionaldataexception e) { log.d("my error connecting",e.getmessage()); } catch (classnotfoundexception e) { log.d("error classnotfoundexception",e.getmessage()); } catch (ioexception e) { log.d("error ioexception",e.getmessage()); } } }
this error when click on button should create socket: on create use connect= new myconnect(); , on onclick event use:
@override public void onclick(view v) { switch(v.getid()){ case r.id.btnexample: new thread(connect).start(); break; } }
error message :
05-29 19:02:05.905: e/androidruntime(6520): fatal exception: main 05-29 19:02:05.905: e/androidruntime(6520): android.os.networkonmainthreadexception 05-29 19:02:05.905: e/androidruntime(6520): @ android.os.strictmode$androidblockguardpolicy.onnetwork(strictmode.java:1118) 05-29 19:02:05.905: e/androidruntime(6520): @ libcore.io.blockguardos.connect(blockguardos.java:84) 05-29 19:02:05.905: e/androidruntime(6520): @ libcore.io.iobridge.connecterrno(iobridge.java:127) 05-29 19:02:05.905: e/androidruntime(6520): @ libcore.io.iobridge.connect(iobridge.java:112) 05-29 19:02:05.905: e/androidruntime(6520): @ java.net.plainsocketimpl.connect(plainsocketimpl.java:192) 05-29 19:02:05.905: e/androidruntime(6520): @ java.net.plainsocketimpl.connect(plainsocketimpl.java:172) 05-29 19:02:05.905: e/androidruntime(6520): @ java.net.socket.startupsocket(socket.java:566) 05-29 19:02:05.905: e/androidruntime(6520): @ java.net.socket.tryalladdresses(socket.java:127)
edit:
class mysynctask extends asynctask<integer, integer, string>{ string advice; @override protected string doinbackground(integer... params) { try { s = new socket("192.168.1.8",7777); log.d("socket connected",""); objectinputstream streamreader = new objectinputstream(s.getinputstream()); advice = (string)streamreader.readobject(); } catch (unknownhostexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (classnotfoundexception e) { // todo auto-generated catch block e.printstacktrace(); } return ""; } protected void onpostexecute(string result) { txtmessage.settext("you got message: " + advice); } protected void onpreexecute() { log.d("my started","start"); } }
and used
mysynctask asynctask=new mysynctask (); asynctask.execute();
on oncreate() method reason not connecting server
logcat asynctask:
05-29 19:46:21.900: d/my started(26169): start 05-29 19:46:22.020: d/libegl(26169): loaded /system/lib/egl/l ibegl_mali.so 05-29 19:46:22.030: d/libegl(26169): loaded /system/lib/egl/libglesv1_cm_mali.so 05-29 19:46:22.040: d/libegl(26169): loaded /system/lib/egl/libglesv2_mali.so 05-29 19:46:22.080: d/openglrenderer(26169): enabling debug mode 0
edit problem solved
after searching on internet why socket not connecting found had similar problem , found answer here android socket not being instantiated had initializate socket first , use connect
this happening because trying perform networking operation on main thread. bad design, not want lock user interface while run process can take long time execute.
put network method in asynctask or service.
private class downloadfilestask extends asynctask<url, integer, long> { protected long doinbackground(url... urls) { int count = urls.length; long totalsize = 0; (int = 0; < count; i++) { totalsize += downloader.downloadfile(urls[i]); publishprogress((int) ((i / (float) count) * 100)); // escape if cancel() called if (iscancelled()) break; } return totalsize; } protected void onprogressupdate(integer... progress) { setprogresspercent(progress[0]); } protected void onpostexecute(long result) { showdialog("downloaded " + result + " bytes"); } }
Comments
Post a Comment