2017-08-18 2 views
-2

Ceci est la classe en questionbloque l'application Android lorsque la classe est appelée qui utilise un nouveau thread pour se connecter au serveur

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(); 
       is = 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(); 
    } 
} 
} 

Il bloque mon application lorsqu'il est appelé. J'ai essayé d'utiliser un nouveau thread pour chacun, et ce code fonctionne, j'ai essayé de l'utiliser comme méthode, mais c'est un projet de groupe donc je veux une classe que tout le monde peut facilement utiliser, mais qui ne fonctionne pas actuellement. Il devrait se connecter à un serveur qui renvoie la connexion si elle réussit, nigol si elle échoue, le serveur fonctionne, nous l'avons testé avec d'autres plates-formes, mais je n'arrive pas à obtenir cette classe

Répondre

1

Vous ne pouvez pas afficher le message toast de tout fil autre que l'interface utilisateur. Je recommande d'utiliser un AsyncTask ou utiliser ceci:

MyActivity.this.runOnUiThread(new Runnable() { 
    public void run() { 
     Toast.makeText(activity, "Hello", Toast.LENGTH_SHORT).show(); 
    } 
});