I am sending and applying data to one server and in reverse using sockets from an Android device. I'm using an AsyncTask class to handle connections. My problem is that at some point in my main activity class I have to read from the input stream.
conhandler = new connection handler (); Conhandler.execute (); Logs. I ("appoin", "reading from the stream"); Message = conhandler.readFromStream (); And what has happened is that this code snippet has been executed before my connection. The handler class has established a socket connection. Is there any way to fix this issue? Here is the code of my connectionhandler class:
Public category connectionhandler Async Task and Late; Wide, voided, vioid & gt; {Public static string serverp = "192.168.1.100"; Public fixed integer serverport = 7777; Socket s; Public DataInputStream dis = null; Public DataOutputStream DOS = Faucet; Public int message; @ Override protected voice downline background (zero ... parameter) {try {log.i ("asyncTank", "doInBackgoung: creating socket"); S = new socket ("192.168.1.100", 7777); } Hold (exception e) {Log.i ("AsyncTank", "doInBackgoung: can not create socket"); } Logs. I ("Asyncantank", "Doinbackguug: Socket Made"); If (s.isConnected ()) {try {log.i ("asyncTank", "doInBackgoung: try input inside"); Dis = New DataInputStream (s.getInputStream ()); Logs. I ("Asyncantank", "Do not Beggong: InputStream Word"); } Catch (IOE EXPRESS E) {// Todo Auto-Generated Coke Block Log.i ("Asyncantank", "AinBackgung: Stream Not Assigned, Socket Not Connected"); E.printStackTrace (); } {Log.} ("Asyncancel", "EyeBackgung: Outer Output Troy"); Dos = new dataoutputstream (S.A.AutputTream ()); Logs. I ("Asyncantank", "Doin Beckong: Outputstream Appointed"); } Catch (IOE EXPRESS E) {// Todo Auto-Generated Coke Block Log.i ("Asyncantank", "AinBackgung: Stream Not Assigned, Socket Not Connected"); E.printStackTrace (); }} And {Log (i.e. "Asyncancel", "EyeBackgung: stream can not be assigned, socket is off"); } Return tap; } Public Zero writeStostream (double latitude, double LAN) {try {log. ("Asynchronous Tasks", "Written Tytreme: Writing Writing"); Dos.writeDouble (lat); Dos.flush (); } Hold (exception e) {Log.i ("Asyncansk", "writeostostum: writing failed"); } {Log. I ("Asyncansk", "Writeostostream: Writing LAN"); Dos.writeDouble (longitude); Dos.flush (); } Hold (exception e) {Log.i ("Asyncansk", "writeostostum: writing failed"); }} Public int readFromStream () {try {if (s.isConnected ()) {Log.i ("AsynkTask", "readFromStream: Reading Message"); Message = dis.readInt (); } Else {Log.i ("Asyncask", "Refreshstream: can not be read, socket is off"); }} Hold (Exception e) {Log.i ("Asyncansk", "Riffromstream: Reading Failed"); } Return message; } }
Yes, you all < You can also do anything after overwriting the network operation in the / em> ASyncTask thread, while overriding the system. It runs on the Studio UI thread, so that you can manipulate text view, layout, etc. For example, how you can use the acctcoccus to open the socket and repeat it:
increases the public class ASyncExample activity {Private TextView mTextView; @ Override Protected Zero (InstanceStatus saved from Bundle) {// Set Up Super Super. Ultimate (Saved Instantstate); } Private Class WriteToServer AsyncTask and lt; Double, zero, zero> {Private last string serverp = "192.168.1.100"; Private Final Pro Serverport = 7777; Socket s; Private data outputTream dos = faucet; @ Override protected voice downline background (double ... parameter) {try {// stream s = new socket ("192.168.1.100", 7777); Dos = new dataoutputstream (S.A.AutputTream ()); // Stream Dosage Double repeated repeats in double (param [0]); Dos.flush (); } Hold (exception e) {Log.i ("asyncantank", "something went wrong"); } Return tap; }}}
Comments
Post a Comment