M360
M360

Reputation: 1

Android client force-closing

im new to android and was wondering if someone can help me with this this is the code i have.

import java.io.*;
import java.net.*;
import android.app.*;
import android.util.Log;
import android.os.*;
import android.view.*;

public class MainActivity extends Activity
  {
  @Override
  public void onCreate(Bundle savedInstanceState)
    {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    try
      {
      Socket TestSocket = new Socket("96.29.64.93",2215);
      OutputStream writer = TestSocket.getOutputStream();
      writer.write(new byte[]{1});
      writer.flush();

      InputStream reader = TestSocket.getInputStream();
      byte array[] = new byte[1];
      int i = reader.read(array);
      }catch(IOException ex)
        {
        Log.e("TcpTest","Error",ex);
        }
     }
   }

EDIT: internet permissions set in manifest app force closes here is logcat. http://i43.tinypic.com/25tc1o7.png

if i take off internet permissions it doesn't force close.

And thanks to all that posted answers here so far.

Upvotes: 0

Views: 298

Answers (2)

Alex Lockwood
Alex Lockwood

Reputation: 83311

You are attempting to make a connection on the UI thread and ICS is crashing your app (as it should, since attempting to connect to a web server on the UI thread is almost certainly a guarantee that your app will not function correctly). Ensure that you are making the connection using an AsyncTask or a Thread.


Edit:

You asked for some clarification, so here it goes. From an Android article on the Developer's site:

When an application is launched, the system creates a thread called "main" for the application. The main thread, also called the UI thread, is very important because it is in charge of dispatching the events to the appropriate widgets, including drawing events. It is also the thread where your application interacts with running components of the Android UI toolkit.

For instance, if you touch the a button on screen, the UI thread dispatches the touch event to the widget, which in turn sets its pressed state and posts an invalidate request to the event queue. The UI thread dequeues the request and notifies the widget to redraw itself.

This single-thread model can yield poor performance unless your application is implemented properly. Specifically, if everything is happening in a single thread, performing long operations such as network access or database queries on the UI thread will block the whole user interface. No event can be dispatched, including drawing events, while the long operation is underway. From the user's perspective, the application appears hung. Even worse, if the UI thread is blocked for more than a few seconds (about 5 seconds currently) the user is presented with the infamous "application not responding" (ANR) dialog.

This applies to your situation, as attempting to establish a socket connection between a client and a server will block the UI Thread--the user will not be able to interact with the screen at all until the connection is made (and this won't get you too many good reviews in the Android Market :P). Therefore, it is very important to perform potentially expensive/long-term operations on a separate Thread. The easiest way to do this is to use an AsyncTask, which are very easy to implement and essentially abstract the entire idea of Threads so you don't have to worry about it.

Upvotes: 1

Thomas Kaliakos
Thomas Kaliakos

Reputation: 3304

What if you move the setContentView above the try statement?

Upvotes: 0

Related Questions