-2

I'm newbie on Android and Java programming and I'm stuck.

I was able to create a TCP/IP communication client (Over LAN) which sends text messages on a windows based web server. The code is working quite well on my Samsung Galaxy S Advance mobile phone which is currently running on 2.3.6. However trying the apk file on two other devices running Android 4.0 and Android 4.1 the App is running but no message arrives on the PC (test are preformed on the same network).

The function I'm using for sending packets is the following:

public void sednit(String IP,String MSG) {
try {
        // Socket  s = new Socket ("192.168.128.102",39999);
      Socket  s = new Socket (ipaddress,39999);
       //outgoing stream redirect to socket
       OutputStream  out = s.getOutputStream();
       PrintWriter  output = new PrintWriter (out);
       output.println(MSG);
       output.flush();
       BufferedReader  input = new BufferedReader (new InputStreamReader(s.getInputStream)));
       //read line(s)
       String  st = input.readLine();
       //Close connection
       s.close();
      } catch (Exception e) {
        // Toast.makeText(getApplicationContext(), e.toString(),Toast.LENGTH_LONG).show();
          Toast.makeText(getApplicationContext(), "Unable to communicate with player", Toast.LENGTH_LONG).show();
      }

What Am I doing wrong?

Is there any limitation applied on Android 4 and newer or I have messed up my manifest file?

Thank you.

2
  • use AsyncTask to perform network calls Nov 9, 2012 at 9:02
  • you probably get NetworkOnUIThread exception. Check your logcat for details. Solution - move the network operations on separate thread
    – mihail
    Nov 9, 2012 at 9:02

2 Answers 2

1

You are most likely getting a NetworkOnMainThread exception. Since Android Honeycomb, you are required to perform network operations in separate threads to improve UI responsiveness. The easiest ways to do this are to use an AsyncTask to manage your network operation if it's short, or to extend java.io.Thread if the connection needs to be maintained.

2
  • It's not 'since Gingerbread' but since Honeycomb. That's why it works under 2.3.6. However whatever the API level it's either impossible, or a bad idea to do do the comms on the main thread and AysncTask is as good a way as any to correct the problem.
    – NickT
    Nov 9, 2012 at 10:42
  • Oops, sorry. You're right, Gingerbread was the last one before it was implemented, not the first one after. I'll edit my answer.
    – Techwolf
    Nov 9, 2012 at 19:20
0

As Tomislav says in comment use Asynctask to network communication.
Also catch (Exception e) { Is an extremely bad Idea.. Catch the exceptions you are counting on, so that others may be thrown and you can see them. We have no idea what so ever what is going wrong with you program when you are doing this...

So please either do e.printStackTrace(); or remove the try/catch so we can get your logcat and help you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.