0

I am trying to set up a client socket in my android app. On a button press, I am trying to start an async task that connects to a socket and determines if the connection was successful.

public class MainActivity extends Activity {

public Socket mySocket;
String str1 = "default";
private static final int SERVERPORT = 34776;
private static final String SERVER_IP = "172.24.1.1";


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

}

public void onClick(View view) {
    StatusCheck myStatusCheck = new StatusCheck();
    myStatusCheck.execute();

        Context context = getApplicationContext();
        CharSequence text = str1;
        int duration = Toast.LENGTH_SHORT;
        Toast toast = Toast.makeText(context, text, duration);
        toast.show();

}

private class StatusCheck extends AsyncTask<Void, Void, Void> {


    @Override
    protected Void doInBackground(Void... params) {
        //debug vibrations and text
        Vibrator vib = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
        vib.vibrate(100);
        str1 = "test2";
        try {
            InetAddress serverAddr = InetAddress.getByName(SERVER_IP);

            mySocket = new Socket(serverAddr, SERVERPORT);

        } catch (UnknownHostException e1) {
            e1.printStackTrace();
        } catch (IOException e1) {
            e1.printStackTrace();
        }

        str1 = "test";

        if (mySocket.isConnected()) {
            str1 = "Connected";
            vib.vibrate(500);
        } else {
            str1 = "Not Connected";
            vib.vibrate(1000);
        }

        return null;
    }

}
}

When I hit the connect button, I display str1 in a toast message. When I first hit the button, the message is "default" as expected. The phone vibrates shortly. On subsequent presses of the button, the message changes to "test2". However, the phone does not vibrate anymore. This tells me the code is hung up somewhere in the try block. After a minute or so, the app crashes with this error:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.net.Socket.isConnected()' on a null object reference

This tells me that mySocket remains null and is not set, yet I have code right above it that should define mySocket. What I don't understand is why mySocket is not getting defined. Any ideas on how to fix this? Thank you for any help!

Gigaxalus
  • 107
  • 2
  • 12
  • Your Toast will be inaccurate because your task is async. On a sidenote, have you tried debugging this? – Brovoker Feb 24 '17 at 06:37
  • Hmm I believe you are right about the toast, but I feel like if the asyc task was successfully executed and finished, then I should expect that value of toast to change. Please correct me if I'm wrong. I have tried to debug this and found NullPointerException on my .isConnected(), which means mySocket is never being defined. What I don't understand is why mySocket is not being defined. – Gigaxalus Feb 24 '17 at 18:25
  • If your socket is not being defined I guess your try block didn't work. Can you try putting a breakpoint in both your catch clauses? – Brovoker Feb 26 '17 at 17:34
  • It took me a while, but I ended up getting this code to work. Turns out my cellular data was somehow interfering with the WiFi network. As soon as I turned my data off, it started working. – Gigaxalus Feb 28 '17 at 18:27

1 Answers1

0

I've been working on the code for a few days and finally have it working. Disregarding all the other minor flaws in my code, such as the improper use of .isconnected() and toast messages, it turns out the problem was somehow related to my cellular data. Turns out my cellular data was somehow interfering with the WiFi network. As soon as I turned my data off, it started working.

Gigaxalus
  • 107
  • 2
  • 12