Click here to Skip to main content
15,886,074 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I would like to send a socket of binary numbers to a program in PC, I'm working in android 2.3 platform on Windows 7. When I run the program in android emulator, its runs perfectly, but when I press the button which is create a socket object, the app will stop working. Here is the code the cause the problem in socket object:

public class ServerClient extends Activity {
	private Button bt;
	   private TextView tv;
	   private static final int REDIRECTED_SERVERPORT = 5000;
	   private String serverIpAddress = "10.0.2.2";
	   @Override
	   public void onCreate(Bundle savedInstanceState) {
	      super.onCreate(savedInstanceState);
	      setContentView(R.layout.main);
	      bt = (Button) findViewById(R.id.myButton);
	      tv = (TextView) findViewById(R.id.myTextView);


	      bt.setOnClickListener(new OnClickListener() {
	    	   Socket socket;
	      
	         public void onClick(View v) {
	        	
	   	      try {
	   	         InetAddress serverAddr = InetAddress.getByName(serverIpAddress);
	   	         socket = new Socket(serverAddr, REDIRECTED_SERVERPORT);
	   	       
	   	      } catch (UnknownHostException e1) {
	   	         e1.printStackTrace();
	   	      } catch (IOException e1) {
	   	         e1.printStackTrace();
	   	      }
	            try {
	               EditText et = (EditText) findViewById(R.id.EditText01);
	               String str = et.getText().toString();
	               PrintWriter out = new PrintWriter(new BufferedWriter(
	                     new OutputStreamWriter(socket.getOutputStream())),
	                     true);
	               out.println(str);
	               Log.d("Client", "Client sent message");

	            } catch (UnknownHostException e) {
	               tv.setText("Error1");
	               e.printStackTrace();
	            } catch (IOException e) {
	               tv.setText("Error2");
	               e.printStackTrace();
	            } catch (Exception e) {
	               tv.setText("Error3");
	               e.printStackTrace();
	            }
	         }
	      });
	   }
	}
Posted
Comments
Sergey Alexandrovich Kryukov 10-Mar-12 17:51pm    
Where is the exception? In what line? Where is the comprehensive exception information?
--SA
kaqa90 11-Mar-12 4:16am    
the exception happens at this line:
Socket socket;

and the information is: android.os.NetworkOnMainThreadException
so when the button is pressed the exception will happen
Sergey Alexandrovich Kryukov 12-Mar-12 11:57am    
Sorry, I can't be. Socket is the declaration, nothing happens, and it contradicts to the next statement about "when the button is pressed". This is your other problem with exceptions. Can you output the exception stack with code lines? Run under debugger?
--SA
kaqa90 12-Mar-12 16:48pm    
The problem exception in LogCat is: Network on Main Thread Exception
Sergey Alexandrovich Kryukov 13-Mar-12 1:04am    
I explained why the exception could not be in this line. What line in fact?
--SA

Please see my comment to the question.

One important note: You are doing one bad thing: prevent all exceptions from the event handler from further propagation. Exceptions are not designed for catching them here and there: they are much more powerful then you might thing. Basically, in most cases you should allow all exceptions go, except some special cases. They should be caught on the very top of stack. Also, they should be caught in a main cycle of the event-oriented UI. Do you do it?

—SA
 
Share this answer
 
Personally I would declare the socket private an null

private Socket socket = null ;


that should fix up the problem
 
Share this answer
 
v2
Comments
kaqa90 12-Mar-12 16:47pm    
I try this out, but the problem does not fixed yet !!
Darren_vms 13-Mar-12 5:12am    
This works on the emulator and not on a phone and that is down to the address 10.0.2.2 (special loop back 127.0.0.1).

If you want this to work on a phone, you need to change the ip address. So if you are using wireless within your hub use the registered address from the hub. If your outside using 3g or another wireless network you will need to find the external address of your hub and allow a port forward)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900