java - Connect to server via socket in Android -


i pretty new android app development, , have created following below activity's oncreate():

public string test(){     string test = "";     try{     socket s = new socket(inetaddress.getbyname("ipaddress"), port#);     test +=(s.isconnected());      bufferedreader readfromhost = new bufferedreader(new inputstreamreader(s.getinputstream()));     readfromhost.ready();       test += (readfromhost.readline());     s.close();     }catch(exception e){         e.printstacktrace();     }     return test;  } 

this routes a:

textview2.settext(test()); 

but string "test_message" grabbed server not appear in text view. however, when

textview2.settext("hi"); 

it appears in view, or if run test() in java outside of android program resulting "test_message" appears in console. thoughts on why not appear in android runtime? ports treated differently? great.

attached logcat. there appears "networkonmainthreadexception"?

all occurs in program click button opens new activity textview.

log:

06-09 12:13:18.189: w/applicationpackagemanager(4768): getcscpackageitemtext() 06-09 12:13:18.239: e/moreinfohpw_viewgroup(4768): parent view not textview 06-09 12:13:18.369: e/spannablestringbuilder(4768): span_exclusive_exclusive spans cannot have 0 length 06-09 12:13:18.369: e/spannablestringbuilder(4768): span_exclusive_exclusive spans cannot have 0 length 06-09 12:13:22.773: w/applicationpackagemanager(4768): getcscpackageitemtext()  06-09 12:13:22.783: w/system.err(4768): android.os.networkonmainthreadexception 06-09 12:13:22.783: w/system.err(4768):     @ android.os.strictmode$androidblockguardpolicy.onnetwork(strictmode.java:1156) 06-09 12:13:22.783: w/system.err(4768):     @ libcore.io.blockguardos.connect(blockguardos.java:84) 06-09 12:13:22.783: w/system.err(4768):     @ libcore.io.iobridge.connecterrno(iobridge.java:127) 06-09 12:13:22.783: w/system.err(4768):     @ libcore.io.iobridge.connect(iobridge.java:112) 06-09 12:13:22.783: w/system.err(4768):     @ java.net.plainsocketimpl.connect(plainsocketimpl.java:192) 06-09 12:13:22.783: w/system.err(4768):     @ java.net.plainsocketimpl.connect(plainsocketimpl.java:172) 06-09 12:13:22.783: w/system.err(4768):     @ java.net.socket.startupsocket(socket.java:566) 06-09 12:13:22.783: w/system.err(4768):     @ java.net.socket.<init>(socket.java:226) 06-09 12:13:22.783: w/system.err(4768):     @ com.example.myfirstapp.displaymessageactivity.test(displaymessageactivity.java:42) 06-09 12:13:22.783: w/system.err(4768):     @ com.example.myfirstapp.displaymessageactivity.oncreate(displaymessageactivity.java:31) 06-09 12:13:22.783: w/system.err(4768):     @ android.app.activity.performcreate(activity.java:5426) 06-09 12:13:22.783: w/system.err(4768):     @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1105) 06-09 12:13:22.783: w/system.err(4768):     @ android.app.activitythread.performlaunchactivity(activitythread.java:2269) 06-09 12:13:22.783: w/system.err(4768):     @ android.app.activitythread.handlelaunchactivity(activitythread.java:2363) 06-09 12:13:22.783: w/system.err(4768):     @ android.app.activitythread.access$900(activitythread.java:161) 06-09 12:13:22.783: w/system.err(4768):     @ android.app.activitythread$h.handlemessage(activitythread.java:1265) 06-09 12:13:22.783: w/system.err(4768):     @ android.os.handler.dispatchmessage(handler.java:102) 06-09 12:13:22.783: w/system.err(4768):     @ android.os.looper.loop(looper.java:157) 06-09 12:13:22.783: w/system.err(4768):     @ android.app.activitythread.main(activitythread.java:5356) 06-09 12:13:22.783: w/system.err(4768):     @ java.lang.reflect.method.invokenative(native method) 06-09 12:13:22.783: w/system.err(4768):     @ java.lang.reflect.method.invoke(method.java:515) 06-09 12:13:22.783: w/system.err(4768):     @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1265) 06-09 12:13:22.783: w/system.err(4768):     @ com.android.internal.os.zygoteinit.main(zygoteinit.java:1081) 06-09 12:13:22.783: w/system.err(4768):     @ dalvik.system.nativestart.main(native method) 06-09 12:13:22.803: e/moreinfohpw_viewgroup(4768): parent view not textview 

you can't network requests in main thread. create own thread , string server:

private textview textview2;  public void test(){      new thread(new runnable() {         @override         public void run() {             final stringbuilder test = new stringbuilder();             try{                 socket s = new socket(inetaddress.getbyname("ipaddress"), 1111);                 test.append(s.isconnected());                  bufferedreader readfromhost = new bufferedreader(new inputstreamreader(s.getinputstream()));                 readfromhost.ready();                   test.append(readfromhost.readline());                 s.close();             }catch(exception e){                 e.printstacktrace();             }              runonuithread(new runnable() {                 @override                 public void run() {                     textview2.settext(test.tostring());                 }             });         }     }).start(); } 

Comments

Popular posts from this blog

C# random value from dictionary and tuple -

cgi - How do I interpret URLs without extension as files rather than missing directories in nginx? -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -