Android Socket: Read Continuous Data -


i trying read data continuously using following code:

public class myclienttask extends asynctask<void, void, void> {          string dstaddress;         int dstport;         string response = "";          myclienttask(string addr, int port){             dstaddress = addr;             dstport = port;          }          @override         protected void doinbackground(void... arg0) {              socket socket = null;              try {                 socket = new socket(dstaddress, dstport);                  bytearrayoutputstream bytearrayoutputstream = new bytearrayoutputstream(1024);                 byte[] buffer = new byte[1024];                  int bytesread;                 inputstream inputstream = socket.getinputstream();                   while ((bytesread = inputstream.read(buffer)) != -1){                     readinpt = inputstream.tostring();                     bytearrayoutputstream.write(buffer, 0, bytesread);                     response = bytearrayoutputstream.tostring("utf-8");                 }                 textresponse.settext(readinpt);              } catch (unknownhostexception e) {                 // todo auto-generated catch block                 e.printstacktrace();                 response = "unknownhostexception: " + e.tostring();             } catch (ioexception e) {                 // todo auto-generated catch block                 e.printstacktrace();                 response = "ioexception: " + e.tostring();             }finally{                 if(socket != null){                     try {                         socket.close();                     } catch (ioexception e) {                         // todo auto-generated catch block                         e.printstacktrace();                     }                 }             }             return null;         }          @override         protected void onpostexecute(void result) {             textresponse.settext(response);             super.onpostexecute(result);         }      } 

but reason, doesn't show me output in textbox. appreciated.

there @ least 2 issues in code.

frist, i'm not sure method tostring() on inputstream going work, because documentation says returns description of object (which different string recieved). might confusing contents of buffer might want.

readinpt = inputstream.tostring();  // wrong 

second. you're updating user interface background thread, inside doinbackground() , forbidden.

textresponse.settext(readinpt); // forbidden, move somewhere else, e.g. onpostexecute() 

Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -