0

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.

M360
  • 1
  • 1

2 Answers2

1

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.

Alex Lockwood
  • 83,063
  • 39
  • 206
  • 250
  • This is assuming you are writing your application for ICS, of course. If not, you should still ensure that you are creating new `Socket`s on a separate `Thread` only. – Alex Lockwood Apr 07 '12 at 19:03
  • I just looked at your updated post... the problem was that the `new Socket()` instruction was blocking the UI thread from drawing the screen layout... a combination of both our answers :). Make sure you fix this problem, it is very important!! – Alex Lockwood Apr 07 '12 at 21:41
  • could you explain a little better im new to android and java i use c++ – M360 Apr 07 '12 at 22:03
  • Take a look at [this](http://stackoverflow.com/q/4356919/844882) for some sample code. – Alex Lockwood Apr 07 '12 at 22:30
  • I just updated my answer... hope that clarifies things for you. I'm not familiar with C++ threading, so I can't be sure :). – Alex Lockwood Apr 07 '12 at 22:45
  • allright i look into threading thank for the tip.just to make sure my program can force close cause im hanging up the ui? – M360 Apr 07 '12 at 22:52
  • Yup. If the UI hangs for ~5 seconds the system will automatically force close your app. – Alex Lockwood Apr 07 '12 at 23:25
0

What if you move the setContentView above the try statement?

Alex Lockwood
  • 83,063
  • 39
  • 206
  • 250
Thomas Kaliakos
  • 3,274
  • 4
  • 25
  • 39
  • thanks i did a you suggested and it no longer force closes but i found in logat it says tcptest error(that how i got it programmed). Log.e("TcpTest","Error"); is there a way to change it to get more descriptive error? – M360 Apr 07 '12 at 19:59