2011-05-20 164 views
1

我需要使用tcp socket連接才能從bluebox獲取數據,如果輸入註釋,比如「getcolor」,bluebox會給我發送像「red,blue」這樣的信息。 在這種情況下,藍盒作爲服務器,我不需要編程它,但我有問題顯示EditText上的信息。安卓tcp socket問題

public class sender { 
public static void main(String[] args)throws IOException{ 

     Socket socket = new Socket("192.168.1.176",14111); 

     OutputStream out = socket.getOutputStream(); 

     BufferedReader msg = new BufferedReader(new InputStreamReader(System.in)); 
     BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));   
     PrintWriter ou = new PrintWriter(new BufferedWriter(new OutputStreamWriter(out)),true); 

     String buffer = new String(""); 
     String ms = ""; 
     while(true) 
     { 
      while(in.ready()) 

       buffer+= in.readLine()+ "\n"; 
      String[] line = buffer.split("\n"); 

      while(msg.ready()) 
       ms = msg.readLine(); 
      if(ms.equals("exit")) 
      { 
       break; 
      } 
      if(!ms.equals("")) 
      { 
       ou.println(ms); 
       ou.flush(); 
       ms = ""; 
      } 
      if(!buffer.equals("")) 
      { 

       System.out.print(buffer); 
       buffer = ""; 

      } 

     } 

     in.close(); 
     out.close(); 
     socket.close(); 

} 
} 

這個java代碼工作,但它在下面的Android的代碼失敗:

public class BlueBoxApp extends Activity { 
/** Called when the activity is first created. */ 

Context appInstance = this; 
private EditText info; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
      info = (EditText)findViewById(R.id.EditText01); 
    try{ 

     InetAddress serverAddr = InetAddress.getByName("192.168.1.176");//TCP服務器IP地址 

      Log.d("TCP", "server,receiving..."); 

      Socket socket = new Socket(serverAddr,14111); 


      try { 


      OutputStream out = socket.getOutputStream(); 
      BufferedReader msg = new BufferedReader(new InputStreamReader(System.in)); 
      BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));   
     PrintWriter ou = new PrintWriter(new BufferedWriter(new OutputStreamWriter(out)),true); 

      String buffer = new String(""); 
      String ms = "getsensorno"; 
      Log.d("TCP", "sending:'"+ms+"'"); 
     while(true) 
     { 
      while(in.ready()) 

       buffer+= in.readLine()+ "\n"; 

      while(msg.ready()) 
       ms = msg.readLine(); 
      if(ms.equals("exit")) 
      { 
       break; 
      } 
      if(!ms.equals("")) 
      { 
       ou.println(ms); 
       ou.flush(); 
       ms = ""; 
      } 
      if(!buffer.equals("")) 
      { 

      info.setText(buffer); 
      buffer = ""; 

      } 
     } 

      } catch (Exception e) { 

      Log.e("TCP", "error",e); 

     }finally{ 

      socket.close(); 

     } 

     }catch(Exception e){ 

      Log.e("TCP", "error",e); 

     } 
} 
} 

是什麼問題以及如何設置一個線程呢?謝謝!

+0

什麼是例外?可能是它無法連接到模擬器的互聯網? – 2011-05-20 20:14:55

+0

究竟是什麼問題? – Haphazard 2011-05-20 20:15:29

回答

3

正如您所指出的,問題在於您正在主線程上執行聯網部分。

設置新線程很容易,可以考慮使用AsyncTask。請閱讀文檔(這是非常好的),然後跳到實施它,它會使它更容易恕我直言。

此外,請確保您有網絡許可,您AndroidManifest.xml中

+0

謝謝,它現在可行! – user763344 2011-06-03 12:55:01