2014-02-19 59 views
0

我有一個包含我的服務器的文件,從客戶端應用程序獲取內容。如何將服務器收到的內容發送給我的MainActvity? 如果我嘗試MainActivity main = new MainActivity();在服務器文件中應用程序崩潰。從文件發送消息(字符串)到MainActivity

服務器文件。

class ServerThread implements Runnable { 

    public void run() { 

     Socket socket = null; 
     try { 
      serverSocket = new ServerSocket(SERVERPORT); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     while (!Thread.currentThread().isInterrupted()) { 
      try { 
       socket = serverSocket.accept(); 
       CommunicationThread commThread = new CommunicationThread(socket); 
       new Thread(commThread).start(); 
       connected_server = true; 
      } catch (IOException e) { 
       connected_server = false; 
       e.printStackTrace(); 
      } 
     } 
    } 
} 

class CommunicationThread implements Runnable { 
    private Socket clientSocket; 
    private BufferedReader input; 
    public CommunicationThread(Socket clientSocket) { 
     this.clientSocket = clientSocket; 
     try { 
      this.input = new BufferedReader(new InputStreamReader(this.clientSocket.getInputStream())); 

     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 

    public void run() { 
     while (!Thread.currentThread().isInterrupted()) { 
      try { 
       String read = input.readLine(); 
       updateConversationHandler.post(new updateUIThread(read)); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
    } 

} 

class updateUIThread implements Runnable { 
    private String msg; 
    public updateUIThread(String str) { 
     this.msg = str; 
    } 

    @Override 
    public void run() { 
     if (msg != null){ 
      //Where I need to send the received content to the main activity 

      Log.e("INPUT", msg); 
     } 
    } 
} 


    Main Activity 
    public class MainActivity extends Activity { 
    … 
public void message_recieve(String msg){ 
    // do stuff with messages 
} 



} 

回答

0

你需要實現一個回調,這樣就可以在服務器請求結束

正確接收的結果
相關問題