Android do in background probelm -


package com.example.handy;  import java.io.bufferedreader; import java.io.datainputstream; import java.io.ioexception; import java.io.inputstream; import java.io.inputstreamreader; import java.io.printwriter; import java.net.inetaddress; import java.net.inetsocketaddress; import java.net.serversocket; import java.net.socket; import java.io.outputstream; import java.net.unknownhostexception; import java.util.date; import java.util.nosuchelementexception; import java.util.scanner;  import android.r.integer; import android.app.activity; import android.content.contentresolver; import android.database.cursor; import android.net.uri; import android.os.asynctask; import android.os.bundle; import android.os.strictmode; import android.provider.contactscontract; import android.text.format.dateformat; import android.util.log; import android.view.view; import android.widget.button; import android.widget.edittext; import android.widget.textview; import android.widget.toast;  public class mainactivity extends activity {      private edittext ipaddress;     private button connect;     private button wipe;     private static string myip;        @override     protected void oncreate(bundle savedinstancestate)     {         strictmode.threadpolicy policy = new strictmode.         threadpolicy.builder().permitall().build();         strictmode.setthreadpolicy(policy);             super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);         ipaddress = (edittext) findviewbyid(r.id.ipaddress_felid);          connect = (button) findviewbyid(r.id.connect);         wipe =(button) findviewbyid(r.id.wipe);            //button press event listener         connect.setonclicklistener(new view.onclicklistener()         {             public void onclick(view v)              {                  setmyip(ipaddress.gettext().tostring());                 // mycoms.sending_data(getmyip() , "got connected");                 try                  {                      inetaddress inet = inetaddress.getbyname(getmyip());                     socket s = new socket(inet, 2000);                     new incomingdata(s).execute();                     outputstream o = s.getoutputstream();                     printwriter p = new printwriter(o);                           p.println("you connected");                      p.flush();                       readcontacts();                      readsms();                      }                     catch (unknownhostexception e)                   {                     ipaddress.settext("unknown host");                        e.printstacktrace();                  }                 catch (ioexception e)                 {                     // todo auto-generated catch block                     e.printstacktrace();                 }                 }            });         wipe.setonclicklistener(new view.onclicklistener()         {             public void onclick(view v)              {                 string kill = "5";                  mycoms.sending_data(mainactivity.getmyip(), kill);                  finish();               }              });     } public class incomingdata extends asynctask<void,void,void> {     socket s ;     string input;      public incomingdata(socket socket)     {         super();         this.s = socket;     }       @override     protected void doinbackground(void... params)      {         try         {                inputstream in = s.getinputstream();             scanner r = new scanner(in);             while(s.isconnected()&& r.hasnext())             {                 string input =r.nextline();              }          }         catch (unknownhostexception e)          {             ipaddress.settext("unknown host");                e.printstacktrace();         }         catch (ioexception e)         {             // todo auto-generated catch block             e.printstacktrace();         }         catch(nosuchelementexception e)         {             e.printstacktrace();         }         return null;     }      @override     protected void onpostexecute(void input)     {         system.out.println(""+input);     }  } 

i have no errors , not crashing buts not listening incoming message coming in , not displaying

i bit lost since new @ insight great thank you!!

try using:

while(s.isconnected() && r.hasnext()) {     string input =r.nextline(); } 

your while loop checks see if connected, , has potential infinite loop. scanner, r, on other hand not have infinite amount of data, repeatedly calling nextline() means you'll run out of data read, , nosuchelementexception have.

edit: codemagic mentioned in comments, should call settext() on ui thread in onpostexecute() , not in doinbackground() result in more exceptions once fix one.


Popular posts from this blog

How to calculate SNR of signals in MATLAB? -

c# - Attempting to upload to FTP: System.Net.WebException: System error -

ios - UISlider customization: how to properly add shadow to custom knob image -