当我想与我的服务器建立连接时,我的 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>