java - Android app crashes when class is called that uses a new thread to connect to server -


this class in question

public class serverlink {  private static context context; private socket sock; private string ip; private int port; private inputstream is; private outputstream out; private string temp; private boolean btemp;  public serverlink(context contxt){     this(contxt,"18.220.103.72",2267); }  public serverlink(context contxt,string inip, int inport){     context = contxt;     ip = inip;     port = inport; }  public boolean sendlogintoserver(final string pass, final string username){     thread t1 = new thread(new runnable() {         public void run()         {             try{                 btemp = false;                 sock = new socket(ip,port);                 out = sock.getoutputstream();                 = sock.getinputstream();                  sendtoserver("login");                 sendtoserver(username.length() + ";" + username + ";" + pass);                  temp = "";                 string serveranswer = listener();                  if(serveranswer.equals("")){                     toast.maketext(context,"wtf",toast.length_long).show();                 }                 else if(serveranswer.equals("login")){                     toast.maketext(context,"yay",toast.length_long).show();                     btemp = true;                 }                 else if(serveranswer.equals("nigol")){                     toast.maketext(context,"nay",toast.length_long).show();                 }             }             catch (java.io.ioexception e){                 toast.maketext(context,e.getmessage().tostring(),toast.length_long).show();             }         }});     t1.start();     return btemp; }  public void sendtoserver(string input){     try{          string tosend = input;         byte[] tosendbytes = tosend.getbytes();         int tosendlen = tosendbytes.length;         byte[] tosendlenbytes = new byte[4];         tosendlenbytes[0] = (byte)(tosendlen & 0xff);         tosendlenbytes[1] = (byte)((tosendlen >> 8) & 0xff);         tosendlenbytes[2] = (byte)((tosendlen >> 16) & 0xff);         tosendlenbytes[3] = (byte)((tosendlen >> 24) & 0xff);         out.write(tosendlenbytes);         out.write(tosendbytes);      }     catch (java.io.ioexception e){         toast.maketext(context,e.getmessage().tostring(),toast.length_long).show();     } }  public string listener(){     try {         byte[] lenbytes = new byte[4];         is.read(lenbytes, 0, 4);         int len = (((lenbytes[3] & 0xff) << 24) | ((lenbytes[2] & 0xff) << 16) |                 ((lenbytes[1] & 0xff) << 8) | (lenbytes[0] & 0xff));         byte[] receivedbytes = new byte[len];         is.read(receivedbytes, 0, len);         string message = new string(receivedbytes, 0, len);         return message;     }     catch (java.io.ioexception e){         toast.maketext(context,e.getmessage().tostring(),toast.length_long).show();         return  e.getmessage().tostring();     } } } 

it crashes app when called. tried using new thread each, , code work, i've tried using methods, group project want class can use, doesn't work currently. should connect server returns login if successful, nigol if unsuccessful, server work, we've tested other platforms, cannot seem class correct

you can not show toast message thread other ui. recommend using asynctask or use this:

myactivity.this.runonuithread(new runnable() {     public void run() {         toast.maketext(activity, "hello", toast.length_short).show();     } }); 

Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -