我想在设备连接到pc时显示消息。我的代码如下所示。但是发生了一些异常。在debuging之后,它从处理程序切换到catch块而不是在try块中。任何人都可以告诉我为什么会发生这种情况。 感谢。
package com.ex;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.util.Scanner;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;
public class Connection extends Activity implements OnClickListener {
public static final String TAG="Connection";
public static final int TIMEOUT=10;
Intent i=null;
TextView tv=null;
private String connectionStatus=null;
private Handler mHandler=null;
ServerSocket server=null;
Handler mHandler1=null;
Scanner socketIn;
PrintWriter socketOut;
boolean connected;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Set up click listeners for the buttons
View connectButton = findViewById(R.id.connect_button);
connectButton.setOnClickListener(this);
i = new Intent(this, Connected.class);
mHandler=new Handler();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.connect_button:
tv = (TextView) findViewById(R.id.connection_text);
//initialize server socket in a new separate thread
new Thread(initializeConnection).start();
String msg="Attempting to connect...";
Toast.makeText(Connection.this, msg, msg.length()).show();
break;
}
}
private Runnable initializeConnection = new Thread() {
public void run() {
Socket client = null;
try {
client = new Socket("192.168.18.116", 8080);
} catch (UnknownHostException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// initialize server socket
try{
server = new ServerSocket();
server.setReuseAddress(true);
server.setSoTimeout(TIMEOUT*1000);
server.bind(new InetSocketAddress(5037));
while((client = server.accept()) == null);
//attempt to ccept a connection
client = server.accept();
socketIn=new Scanner(client.getInputStream());
socketOut = new PrintWriter(client.getOutputStream(), true);
} catch (SocketTimeoutException e) {
// print out TIMEOUT
connectionStatus="Connection has timed out! Please try again";
System.out.println("++++++++++++++++nistha+++++++++++++"+connectionStatus);
mHandler.post(showConnectionStatus);
System.out.println("++++++++++++++++nistha+++++++++++++"+mHandler.post(showConnectionStatus));
} catch (IOException e) {
Log.e(TAG, ""+e);
} finally {
//close the server socket
try {
if (server!=null)
server.close();
} catch (IOException ec) {
Log.e(TAG, "Cannot close server socket"+ec);
System.out.println("#########Nis#########"+Log.e(TAG, "Cannot close server socket"+ec));
}
}
if (client!=null) {
connected=true;
// print out success
connectionStatus="Connection was succesful!";
mHandler.post(showConnectionStatus);
System.out.println("#########Nis#########"+mHandler.post(showConnectionStatus));
startActivity(i);
}
}
};
/**
* Pops up a "toast" to indicate the connection status
*/
private Runnable showConnectionStatus = new Runnable() {
public void run() {
Toast.makeText(Connection.this, connectionStatus, Toast.LENGTH_SHORT).show();
}
};
/*
*//**
* Initialize connection to the phone
*
*/
/*
public void initializeConnection(){
//Create socket connection
try{
final Socket socket = new Socket("localhost", 38300);
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
//in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
Scanner sc = new Scanner(socket.getInputStream());
System.out.println("########Nistha##########"+sc);
// add a shutdown hook to close the socket if system crashes or exists unexpectedly
Thread closeSocketOnShutdown = new Thread() {
public void run() {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
};
Runtime.getRuntime().addShutdownHook(closeSocketOnShutdown);
} catch (UnknownHostException e) {
Print.fatalError("Socket connection problem (Unknown host)"+e.getStackTrace());
} catch (IOException e) {
Print.fatalError("Could not initialize I/O on socket "+e.getStackTrace());
}
*/
}