アンドロイド電話からサーバーへのソケット接続があり、正しいIPアドレスとポート番号を入力すると正常に動作しますが、アプリケーションが応答しなくなり、強制終了する。私はcatch(UnknownHostException e)がそれをキャッチしますが、コードが応答を停止すると仮定しますsocket = new Socket(_ip、_portNum);。 _ipと_portは、ユーザーが電話機に入力した内容によって渡されます。私はそれを作る必要があるので、誰かが間違ったIPやポートを入力するとクラッシュすることはありませんが、再度入力するように指示します。Java Androidソケット接続。アプリケーションが応答を停止する原因となる間違ったIPアドレス
public void Connect(String _ip, int _portNum) throws ParserConfigurationException, SAXException, IOException{
//CREATES SOCKET
Socket socket = null;
DataInputStream input = null;
DataOutputStream output = null;
//CREATE DATA STREAMS
try{
Log.e("trying to connect","trying to connect"); //sends to log
socket = new Socket(_ip, _portNum); //stops responding here.
Log.i("Socket Connected", "Socket Connected"); //not sent to log
output = new DataOutputStream(socket.getOutputStream());
input = new DataInputStream(socket.getInputStream());
}catch(UnknownHostException e){
}catch(IOException e){
}
//IF STREAMS ARE OPEN SEND MESSAGES
if (socket != null && output != null && input != null){
try{
//MESSAGES FOR TESTING
output.writeBytes("HELLO2 \n");
String response;
while((response = input.readLine()) != null){
System.out.println("Server: " + response);
//MESSAGE FOR TESTING
output.writeBytes("Test \n");
TextView test = (TextView) findViewById(R.id.TestText);
// display text returned from server
test.setText(response);
}
//CLOSE STREAMS AND CONNECTION
output.close();
input.close();
socket.close();
Log.i("Connections closed", "Connections closed");
}catch(UnknownHostException e){
}catch(IOException e){
}
}
}
いずれかの支援が大幅に修正されました。