this question has answer here:
- networkonmainthreadexception 4 answers
i'm getting networkonmainthreadexception while using runnable
code:
public class fullscreenactivity extends appcompatactivity { public socket socket; public int serverport = 5000; /* port 5000 (for testing) */ public string server_ip = "10.0.2.2"; /* local android address of localhost */ viewflipper flipper; listview listing; public void attemptconnect(view view) { try { edittext edittextaddress = (edittext) findviewbyid(r.id.address); edittext edittextport = (edittext) findviewbyid(r.id.port); string server_ip_loc = edittextaddress.gettext().tostring(); int serverport_loc = integer.parseint(edittextport.gettext().tostring()); system.out.println("attempt connect: ip " + server_ip_loc + " port: " + serverport_loc); system.out.println("set"); clientthread myclienttask = new clientthread(server_ip_loc, serverport_loc); myclienttask.run(); system.out.println("connected"); flipper.setdisplayedchild(1); } catch (exception e) { system.err.println(e.getmessage()); } } // thinks following not thread ?? class clientthread implements runnable { clientthread(string addr, int port) { server_ip = addr; serverport = port; } public void run() { try { inetaddress serveraddr = inetaddress.getbyname(server_ip); // gets line fine socket = new socket(serveraddr, serverport); } catch (exception e1) { e1.printstacktrace(); } } } }
my permissions set allow internet access:
<uses-permission android:name="android.permission.internet"/> <uses-permission android:name="android.permission.write_external_storage"/> <uses-permission android:name="android.permission.read_external_storage"/>
i'm getting following error:
6188-6188/test w/system.err: android.os.networkonmainthreadexception 6188-6188/test w/system.err: @ android.os.strictmode$androidblockguardpolicy.onnetwork(strictmode.java:1273) 6188-6188/test w/system.err: @ libcore.io.blockguardos.connect(blockguardos.java:110) 6188-6188/test w/system.err: @ libcore.io.iobridge.connecterrno(iobridge.java:137) 6188-6188/test w/system.err: @ libcore.io.iobridge.connect(iobridge.java:122) 6188-6188/test w/system.err: @ java.net.plainsocketimpl.connect(plainsocketimpl.java:183) 6188-6188/test w/system.err: @ java.net.plainsocketimpl.connect(plainsocketimpl.java:163) 6188-6188/test w/system.err: @ java.net.socket.startupsocket(socket.java:592) 6188-6188/test w/system.err: @ java.net.socket.<init>(socket.java:226) 6188-6188/test w/system.err: @ test.fullscreenactivity$clientthread.run(fullscreenactivity.java:363) 6188-6188/test w/system.err: @ test(fullscreenactivity.java:340) 6188-6188/test w/system.err: @ java.lang.reflect.method.invoke(native method) 6188-6188/test w/system.err: @ android.support.v7.app.appcompatviewinflater$declaredonclicklistener.onclick(test:270) 6188-6188/test w/system.err: @ android.view.view.performclick(view.java:5198) 6188-6188/test w/system.err: @ android.view.view$performclick.run(view.java:21147) 6188-6188/test w/system.err: @ android.os.handler.handlecallback(handler.java:739) 6188-6188/test w/system.err: @ android.os.handler.dispatchmessage(handler.java:95) 6188-6188/test w/system.err: @ android.os.looper.loop(looper.java:148) 6188-6188/test w/system.err: @ android.app.activitythread.main(activitythread.java:5417) 6188-6188/test w/system.err: @ java.lang.reflect.method.invoke(native method) 6188-6188/test w/system.err: @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:726) 6188-6188/test w/system.err: @ com.android.internal.os.zygoteinit.main(zygoteinit.java:616)
as far can tell, should function expected. seems think runnable isn't thread, can't figure out why.
you need thread
run runnable
you. calling run
on runnable
not run on different thread.
new thread(new clientthread(server_ip_loc, serverport_loc)).start();
Comments
Post a Comment