Wednesday, February 19, 2014

Do not turn off and stuck how to make the program throws an exception after


I do not close the socket when the wrong IP address or the server is not always an exception to open the program to stop, how to make the program after an error<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->
Reply:
Prints an exception, catch the exception does not handle will continue to run
Reply:
 try {
socket.connect ........
} Catch (Exception ex) {
Log.d (TAG, "IP address is wrong or the server is not open");
ex.printStackTrace ();
} Finally {
socket.close .....
}

Use try catch catch the exception does not cause the program to crash, you can continue to run.
Reply:
With a try {} catch, throw an exception.
Reply:
reference to the second floor ncepu307 reply:
 try {
socket.connect ........
} Catch (Exception ex) {
Log.d (TAG, "IP address is wrong or the server is not open");
ex.printStackTrace ();
} Finally {
socket.close .....
}

Use try catch catch the exception does not cause the program to crash, you can continue to run.
I used the try {} catch {} how or crash
Reply:
package com.example.chatactivity;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.UnknownHostException;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements Runnable {
private EditText ipaddr;
private EditText recvmsg;
private EditText sendmsg;
private Button send;
private Button start;
private Button stop;
private String ip = null;
private String sendtext;
Socket socket;
Thread thread;
InputStream in;
OutputStream out;

@ Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_main);
ipaddr = (EditText) findViewById (R.id.iptext);
recvmsg = (EditText) findViewById (R.id.recvtext);
sendmsg = (EditText) findViewById (R.id.sendtext);
send = (Button) findViewById (R.id.send);
start = (Button) findViewById (R.id.start);
stop = (Button) findViewById (R.id.stop);
stop.setEnabled (false);
send.setEnabled (false);
start.setOnClickListener (new View.OnClickListener () {

@ Override
public void onClick (View v) {
/ / TODO Auto-generated method stub
ip = ipaddr.getText () toString ();.
try {
socket = new Socket ();
socket.connect (new InetSocketAddress (ip, 8888));
/ / In = new DataInputStream (socket.getInputStream ());
/ / Out = new DataOutputStream (socket.getOutputStream ());
} Catch (UnknownHostException e) {
/ / TODO Auto-generated catch block
recvmsg.setText ("");
recvmsg.setText ("Unable to connect");
/ / E.printStackTrace ();
/ / Toast.makeText (this, "host name could not be resolved into an IP address", Toast.LENGTH_SHORT);
} Catch (IOException e) {
/ / TODO Auto-generated catch block
/ / E.printStackTrace ();
recvmsg.setText ("Unable to connect");
}
thread = new Thread (MainActivity.this);
thread.start ();
start.setEnabled (false);
stop.setEnabled (true);
send.setEnabled (true);
}
});
send.setOnClickListener (new View.OnClickListener () {

@ Override
public void onClick (View v) {
/ / TODO Auto-generated method stub
sendtext = sendmsg.getText () toString ();.
if (sendtext! = null) {
try {
out.write (sendtext.getBytes ());
} Catch (IOException e) {
/ / TODO Auto-generated catch block
e.printStackTrace ();
}
}
}
});
stop.setOnClickListener (new View.OnClickListener () {

@ Override
public void onClick (View v) {
/ / TODO Auto-generated method stub
try {
in.close ();
out.close ();
socket.close ();
} Catch (IOException e) {
/ / TODO Auto-generated catch block
e.printStackTrace ();
}
stop.setEnabled (false);
start.setEnabled (true);
send.setEnabled (true);
}
});
}

@ Override
public boolean onCreateOptionsMenu (Menu menu) {
/ / Inflate the menu; this adds items to the action bar if it is present
.getMenuInflater () inflate (R.menu.main, menu);.
return true;
}

@ Override
public void run () {
/ / TODO Auto-generated method stub

try {
in = new DataInputStream (socket.getInputStream ());
} Catch (IOException e1) {
/ / TODO Auto-generated catch block
e1.printStackTrace ();
}
try {
out = new DataOutputStream (socket.getOutputStream ());
} Catch (IOException e1) {
/ / TODO Auto-generated catch block
e1.printStackTrace ();
}
while (true) {
try {
String recvtext;
byte [] buffer = new byte [in.available ()];
in.read (buffer);
recvtext = new String (buffer);
Message m = mHandler.obtainMessage ();
m.what = 0x1;
Bundle bundle = new Bundle ();
bundle.putString ("recv", recvtext);
m.setData (bundle);
mHandler.sendMessage (m);
} Catch (IOException e) {
/ / TODO Auto-generated catch block
e.printStackTrace ();
}
}
}
Handler mHandler = new Handler () {
public void handleMessage (Message msg) {
if (msg.what == 0x1) {
recvmsg.append (msg.getData () getString ("recv").);
super.handleMessage (msg);
}
}
};

}
This is the code, as long as it will not connect to collapse

No comments:

Post a Comment