2012-05-14 44 views
0

當我想與我的服務器建立連接時,我的android應用程序在創建新套接字的線路上崩潰。客戶端 - 服務器應用程序中的Android套接字錯誤

我在我的ClientAppl.java中進行連接的代碼如下所示。

public class ClientAppl { 
private InetAddress host; 
private int port; 
private Socket link = null; 
ObjectInputStream istream; 
ObjectOutputStream ostream; 
private static MainActivity mainActivity; 
private static ClientAppl instance = new ClientAppl(mainActivity); 


private ClientAppl(MainActivity frm){ 
ClientAppl.mainActivity = frm; 
} 

public static ClientAppl getInstance(){ 
return instance; 
} 

public void makeConnection(String sIP, int port) throws IOException, java.net.ConnectException{ 
    if(link == null){ 

    System.out.println("Make connection..."); 
    this.host = InetAddress.getByName(sIP); 
     this.port = port; 
     link = new Socket(host, port); 

     System.out.println("Inputkanaal & outputkanaal vastleggen..."); 
     ostream = new ObjectOutputStream(link.getOutputStream()); 
     System.out.println("OK - output"); 
     istream = new ObjectInputStream(link.getInputStream()); 
     System.out.println("OK - input"); 

     AcceptMessageHandler amh = new AcceptMessageHandler(); 
     Thread t = new Thread(amh); 
     t.start(); 
    } 
} 

在MainActivity.java我將此代碼添加到啓動的連接。

txtServerIP = (EditText)findViewById(R.id.txtServerIP); 
txtServerPoort = (EditText)findViewById(R.id.txtServerPoort); 
try { 
ClientAppl.getInstance().makeConnection(txtServerIP.getText().toString(), Integer.parseInt(txtServerPoort.getText().toString())); 
} catch (NumberFormatException e) { 
e.printStackTrace(); 
} catch (UnknownHostException e) { 
e.printStackTrace(); 
} catch (ConnectException e) { 
e.printStackTrace(); 
} catch (IOException e) { 
e.printStackTrace(); 
} 

這是我的清單文件。怎麼了? :(

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="be.howest" 
android:versionCode="1" 
android:versionName="1.0" > 

<uses-sdk android:minSdkVersion="15" /> 
<uses-permission android:name="android.permission.INTERNET"> 
</uses-permission> 
<application android:icon="@drawable/ic_launcher" android:label="@string/app_name"> 
    <activity 
     android:name="project.client.pc.view.MainActivity" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
</application> 

+1

plz發佈您的日誌貓 – MAC

+0

您傳遞給套接字構造函數的值是什麼?你確定它們是有效的主機名和端口號嗎? –

+0

哦,是的,忘記發佈logcat。 這些值是服務器的IP地址和端口號。它們是正確的,因爲我的Java客戶端版本使用相同的值並且沒有錯誤。 Logcat:https://docs.google.com/document/d/1HvR9TL5-Ajq57TKsVzRxZVAow2P3i5j0_Hd8MCG1yU8/edit – user1380883

回答

1
5-14 15:55:28.669: E/AndroidRuntime(572): android.os.NetworkOnMainThreadException 

您一直警告不要在主(UI)線程執行網絡操作,並且你現在有效地通過積極的檢查,這將導致禁止這樣做這個致命的例外

1

正如克里斯指出的那樣,你需要在UI線程以外的線程上完成所有的TCP套接字交互,在我的代碼中,我在AsyncTask中建立套接字,這聽起來很可怕,但如果我可以做到這一點,你當然可以。這是一個體面的tuto里亞爾:

http://www.vogella.com/articles/AndroidPerformance/article.html

隨後,產卵每個插座的「對話」的新話題 - 這不是太困難或者。

+0

我發現的有關Android TCP套接字通信的其他內容:限制的確切性質是您不能做很長時間在UI線程上的操作。我相信其他人也指出了這一點。有趣的是,只有套接字接收操作可能很長(從應用程序的角度來看)。顯然,你可以無限制地從UI線程發送Tcp數據。當然,「如果你不期望得到答覆,爲什麼要使用Tcp?」當然是一個有效的問題。 – Rich

相關問題