我正在尝试使用Android客户端连接到TCP服务器。但是当我尝试连接(在调试中)时,我总是得到套接字为空的消息。
的AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="">
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
<application
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
MainActivity.java
boolean connection = false;
PrintWriter out;
BufferedReader in;
Socket client;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try
{
client = new Socket("xxx.xxx.xx.xx", xx);
connection=true;
}
catch (Exception ex) {
listItem.add("Could not connect");
adapter.notifyDataSetChanged();
}
}