2013-03-11 54 views
1

我編寫了一個應用程序,使用套接字類來創建一個TCP/IP的客戶喜歡在此之前:插座及SDK MINVERSION

Socket soc; 
DataOutputStream out; 
DataInputStream in; 

//in try-catch loop 
soc = new ("192.168.1.101", 100); 
out = new DataOutputStream(socket.getOutputStream()); 
in = new DataInputStream(socket.getInputStream()); 

.....//code on I/O through socket 

並能正常工作在我以前的AP,運行SDK 9

現在我試圖在我的新應用程序中使用相同的功能,它使用了片段並且需要sdk 11.但是,只要運行套接字代碼,應用程序就會崩潰。 一旦我在manifest.xml中將android:minSdkVersion更改爲9,它將再次運行。但我需要minSDK 11的片段。

我該怎麼辦?我只有幾個月的Android編碼,如果我問了一個愚蠢的問題,請原諒我。謝謝!

編輯:這是的OnClick功能觸發插座funcitons:

private Button.OnClickListener m_BtnConnectDisconnectOnClick = new Button.OnClickListener() 
{ 
    public void onClick(View v) 
    { 
     try 
     { 
      //Obtaining IP Address & Port number 
      String str_ip = m_EditPumpIP.getText().toString(); 
      int int_port = Integer.parseInt(m_EditPumpPort.getText().toString()); 

      //Establish Pump Connection 
      socket = new Socket(str_ip, int_port); 
      out = new DataOutputStream(socket.getOutputStream()); 
      in = new DataInputStream(socket.getInputStream()); 

      //Update Status 
      m_TextSystemStatus.setText("OK"); 
     } 
     catch (IOException e) 
     { 
      //Update Status 
      m_TextSystemStatus.setText("Fail"); 
     } 
    } 
}; 
+0

嘗試在你的清單'<使用-SDK設置 android:minSdkVersion =「9」 android:targetSdkVersion =「17」/>' – 2013-03-11 07:47:11

+1

發佈您的logcat,可以讓我你是在UI線程上運行網絡 – DjHacktorReborn 2013-03-11 07:47:25

+0

@MoshErsan他需要minsdk 11 – DjHacktorReborn 2013-03-11 07:48:05

回答

1

將這個中的AsyncTask

//Establish Pump Connection 
      socket = new Socket(str_ip, int_port); 
      out = new DataOutputStream(socket.getOutputStream()); 
      in = new DataInputStream(socket.getInputStream()); 

Like This example