我想在android中實現java套接字客戶端,我的服務器是我的筆記本電腦。我希望我的android手機通過java套接字連接到筆記本電腦。但我在客戶端發生異常:「SocketException」,它看起來是因爲socket()調用無法正確創建套接字。以下是我的Android客戶端代碼進行審查。有沒有什麼解決的決心異常:Android客戶端套接字拋出SocketException:
public class SimpleActivityExampleActivity extends Activity {
/** Called when the activity is first created. */
private String usrName;
private String vendorName;
private String message;
public Socket socket;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void submitTheForm(View view) {
final EditText edittext1 = (EditText) findViewById(R.id.editText1);
final EditText edittext2 = (EditText) findViewById(R.id.EditText01);
final EditText edittext3 = (EditText) findViewById(R.id.editText2);
usrName = edittext1.getText().toString();
vendorName = edittext2.getText().toString();
message = edittext3.getText().toString();
message = usrName+ "," + vendorName + "," + message;
byte[] msg = message.getBytes();
try {
InetAddress serverAddr = InetAddress.getByName("192.168.1.2");
Log.d("ClientActivity", "C: Connecting...");
socket = new Socket(serverAddr, 2200);
socket.getOutputStream().write(msg);
socket.getOutputStream().flush();
} catch (UnknownHostException e) {
Toast.makeText(SimpleActivityExampleActivity.this, "unknownhostException", Toast.LENGTH_SHORT).show();
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SocketException e) {
// TODO Auto-generated catch block
Toast.makeText(SimpleActivityExampleActivity.this, "Sockexception", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}catch (IOException e) {
// TODO Auto-generated catch block
//System.out.println("in IOexception for sure");
Toast.makeText(SimpleActivityExampleActivity.this, "IOexception", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
catch (Exception e){
Toast.makeText(SimpleActivityExampleActivity.this, "Exception", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}finally {
if(socket != null){
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
submitTheForm()函數被調用從main.xml中的文件「機器人:的onClick = 「點擊一個按鈕submitTheForm」
你可以請張貼與異常關聯的堆棧跟蹤嗎? – slayton