Search code examples
androidtcpandroid-versionandroid-min-sdktarget-sdk

Why my android TCP client app wont work on other phones??


I am using an android phone with an android version 4.1.1. .IT WORKS PROPERLY. I can transmit string data from this phone to my VB application in my PC via TCP/IP. However if I use my other phone, with an android version 4.4.4 kitkat .. I can still install the .apk file properly and run it..but it wont do its functions , same goes for my other phone 4.0.1.

here's my code:

XML file:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="hello"
    />
<EditText
    android:id="@+id/textout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    />
<Button
    android:id="@+id/buttonSend"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Send"
    />
<TextView
    android:id="@+id/textin"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    />

JAVA file:

public class MainActivity extends Activity {

EditText textOut;
TextView textIn;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    textOut = (EditText)findViewById(R.id.textout);
    Button buttonSend = (Button)findViewById(R.id.buttonSend);
    textIn = (TextView)findViewById(R.id.textin);
    buttonSend.setOnClickListener(buttonSendOnClickListener);




}

Button.OnClickListener buttonSendOnClickListener
        = new Button.OnClickListener(){

    @Override
    public void onClick(View arg0) {

        // TODO Auto-generated method stub
        Socket socket = null;
        DataOutputStream dataOutputStream = null;
        DataInputStream dataInputStream = null;

        try{
            // Creating new socket connection to the IP (first parameter) and its opened port (second parameter)
            Socket s = new Socket("192.168.1.3", 65535);

            // Initialize output stream to write message to the socket stream
            BufferedWriter out = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));

            String outMsg = "";

            outMsg = textOut.getText().toString();

            // Write message to stream
            out.write(outMsg);

            // Flush the data from the stream to indicate end of message
            out.flush();

            // Close the output stream
            out.close();

            // Close the socket connection
            s.close();
        }

        catch(Exception ex){
            //:TODO Handle exceptions
        }
    }};

}

Does it have anything to do with API levels ? minsdkversion and targetsdkversion? PLEASE HELP ME.. IM GOING CRAZYYY . HAHA XD Thanks in advance.


Solution

  • You cannot create socket connnections in an on click handler. You will have a NetworkOnMainThreadException. Put such code in a thread or AsyncTask.

    Your code should not work on 4.1.1 too.