4

I'm trying to test android studio connection to mysql based on this tutorial using my android device for debugging purposes instead of android emulator. But the problem is, it results to:

java.net.ConnectException: failed to connect to /192.168.15.186 (port 80): connect failed: ETIMEDOUT (Connection timed out)

Take note that this is running on real device. localhost or 127.0.0.1:80 would return a result of ECONNREFUSED because obviously, this are computer addresses that the database aren't in the device but in the computer itself so it would be a completely waste of time if I test these 2 out or any alternative IP's.

I've tested out 10.0.2.2:80/login.php on emulator and it returns a true result hinting that the login and connection is success.

So I'm guessing that maybe the connection is block through the windows firewall, but I dont know how to modify it.

LOGCAT

03-06 13:59:29.935 20951-20951/com.example.smdojt.mysqldemo W/InputEventReceiver: Attempted to finish an input event but the input event receiver has already been disposed.
03-06 14:01:30.744 20951-25612/com.example.smdojt.mysqldemo W/System.err: java.net.ConnectException: failed to connect to /192.168.15.186 (port 80): connect failed: ETIMEDOUT (Connection timed out)
03-06 14:01:30.744 20951-25612/com.example.smdojt.mysqldemo W/System.err:     at libcore.io.IoBridge.connect(IoBridge.java:124)
03-06 14:01:30.744 20951-25612/com.example.smdojt.mysqldemo W/System.err:     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:200)
03-06 14:01:30.744 20951-25612/com.example.smdojt.mysqldemo W/System.err:     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:513)
03-06 14:01:30.744 20951-25612/com.example.smdojt.mysqldemo W/System.err:     at java.net.Socket.connect(Socket.java:894)
03-06 14:01:30.744 20951-25612/com.example.smdojt.mysqldemo W/System.err:     at com.android.okhttp.internal.Platform.connectSocket(Platform.java:174)
03-06 14:01:30.744 20951-25612/com.example.smdojt.mysqldemo W/System.err:     at com.android.okhttp.Connection.connect(Connection.java:152)
03-06 14:01:30.744 20951-25612/com.example.smdojt.mysqldemo W/System.err:     at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:276)
03-06 14:01:30.744 20951-25612/com.example.smdojt.mysqldemo W/System.err:     at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:211)
03-06 14:01:30.744 20951-25612/com.example.smdojt.mysqldemo W/System.err:     at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:382)
03-06 14:01:30.744 20951-25612/com.example.smdojt.mysqldemo W/System.err:     at com.android.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:106)
03-06 14:01:30.744 20951-25612/com.example.smdojt.mysqldemo W/System.err:     at com.android.okhttp.internal.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:217)
03-06 14:01:30.745 20951-25612/com.example.smdojt.mysqldemo W/System.err:     at com.example.smdojt.mysqldemo.BackgroundWorker.doInBackground(BackgroundWorker.java:48)
03-06 14:01:30.745 20951-25612/com.example.smdojt.mysqldemo W/System.err:     at com.example.smdojt.mysqldemo.BackgroundWorker.doInBackground(BackgroundWorker.java:23)
03-06 14:01:30.745 20951-25612/com.example.smdojt.mysqldemo W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:292)
03-06 14:01:30.745 20951-25612/com.example.smdojt.mysqldemo W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
03-06 14:01:30.745 20951-25612/com.example.smdojt.mysqldemo W/System.err:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
03-06 14:01:30.745 20951-25612/com.example.smdojt.mysqldemo W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
03-06 14:01:30.745 20951-25612/com.example.smdojt.mysqldemo W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
03-06 14:01:30.745 20951-25612/com.example.smdojt.mysqldemo W/System.err:     at java.lang.Thread.run(Thread.java:818)
03-06 14:01:30.745 20951-25612/com.example.smdojt.mysqldemo W/System.err: Caused by: android.system.ErrnoException: connect failed: ETIMEDOUT (Connection timed out)
03-06 14:01:30.745 20951-25612/com.example.smdojt.mysqldemo W/System.err:     at libcore.io.Posix.connect(Native Method)
03-06 14:01:30.745 20951-25612/com.example.smdojt.mysqldemo W/System.err:     at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:111)
03-06 14:01:30.745 20951-25612/com.example.smdojt.mysqldemo W/System.err:     at libcore.io.IoBridge.connectErrno(IoBridge.java:137)
03-06 14:01:30.745 20951-25612/com.example.smdojt.mysqldemo W/System.err:     at libcore.io.IoBridge.connect(IoBridge.java:122)
03-06 14:01:30.745 20951-25612/com.example.smdojt.mysqldemo W/System.err:   ... 18 more
03-06 14:01:30.748 20951-20951/com.example.smdojt.mysqldemo D/wangcy9: setStatusIcon occur wrong theme!
03-06 14:01:30.782 20951-20951/com.example.smdojt.mysqldemo D/ViewRootImpl: loadSystemProperties PersistDebugEvent: false RoDebugEvent: false
03-06 14:02:30.313 20951-20951/com.example.smdojt.mysqldemo W/InputEventReceiver: Attempted to finish an input event but the input event receiver has already been disposed.

MAINACTIVITY.java

public class MainActivity extends AppCompatActivity {
    EditText UsernameEt, PasswordEt;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        UsernameEt = (EditText) findViewById(R.id.etUserName);
        PasswordEt = (EditText) findViewById(R.id.etPassword);

    }

    public void OnLogin(View view)
    {
        String username = UsernameEt.getText().toString();
        String password = PasswordEt.getText().toString();
        String type = "login";
        BackgroundWorker backgroundWorker = new BackgroundWorker(this);
        backgroundWorker.execute(type, username, password);
    }

}

BACKGROUNDWORKER.java

public class BackgroundWorker extends AsyncTask<String, Void, String> {

    Context context;
    AlertDialog alertDialog;
    BackgroundWorker (Context ctx)
    {
        context = ctx;
    }

    @Override
    protected String doInBackground(String... params) {
        String type = params[0];
        String login_url = "http://192.168.15.186:80/login.php"; //declare want you want to connect with

        if (type.equals("login"))
        {
            try {
                String user_name = params[1];
                String password = params[2];

                URL url = new URL(login_url);
                HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection(); //declare http connection class
                httpURLConnection.setRequestMethod("POST");
                httpURLConnection.setDoOutput(true);
                httpURLConnection.setDoInput(true);
                OutputStream outputStream = httpURLConnection.getOutputStream();
                BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
                String post_data = URLEncoder.encode("user_name","UTF-8") + "=" +URLEncoder.encode(user_name, "UTF-8")+"&"
                        +URLEncoder.encode("password","UTF-8") + "=" +URLEncoder.encode(password, "UTF-8");
                bufferedWriter.write(post_data);
                bufferedWriter.flush();
                bufferedWriter.close();
                outputStream.close();
                //below: read and get post respone
                InputStream inputStream = httpURLConnection.getInputStream();
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));
                String result="";
                String line="";

                while ((line = bufferedReader.readLine())!=null)
                {
                    result += line;
                }
                bufferedReader.close();
                inputStream.close();
                httpURLConnection.disconnect();
                return result;
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }
            //Clause for httpurlconnection
            catch (IOException e) {
                e.printStackTrace();
            }
        }

        return null;
    }

    @Override
    protected void onPreExecute() {
        alertDialog = new AlertDialog.Builder(context).create();
        alertDialog.setTitle("Login Status");
    }

    @Override
    protected void onPostExecute(String result) {
        alertDialog.setMessage(result);
        alertDialog.show();
    }

    @Override
    protected void onProgressUpdate(Void... values) {
        super.onProgressUpdate(values);
    }
}
SovietSenpai
  • 277
  • 2
  • 3
  • 16
  • have you tried opening `http://192.168.15.186:80/login.php` in mobile web browser? Are you sure you have access to this URL from your mobile? – Vladyslav Matviienko Mar 06 '17 at 06:36
  • @VladMatvienko My phone doesnt have a built in browser. I even tried it to a downloaded browser but it wouldn't work. I'm also suspecting the network connection of the pc and the device. Do they need to be connected in the same network? – SovietSenpai Mar 06 '17 at 06:44
  • there is nothing about Android programming until you make sure that this URL can be accessed from mobile device. Yes, they have to be connected to the same network (obviously), or there should be some route between those networks. Also same network connection does not guarantee that it would connect since your server (PC) should accept connections on the `80` port. – Vladyslav Matviienko Mar 06 '17 at 06:52
  • @VladMatvienko with that said, I need to test out the the pc and device on the same network. my pc here in the company is connected via LAN and quite the opposite of my android device wherein it is connected to the Wifi. If thats the case, maybe I should contact the network admin, or else i'll test it out on different PC or connection. – SovietSenpai Mar 06 '17 at 07:00
  • If your PC has a WIFI module (e.g it's a laptop), you can switch your PC to a WIFI hotspot, and connect your mobile to it. That is just another way to solve a problem, which I use actually. – Vladyslav Matviienko Mar 06 '17 at 07:06
  • `test android studio connection to mysql `???? Android Studio? Should Android Studio connect to a database? Do you believe that yourself? – greenapps Mar 06 '17 at 07:42
  • `my pc here in the company is connected via LAN and quite the opposite of my android device wherein it is connected to the Wifi. `. Cable or wifi does not matter. As long as both devices are connected to the same router. At that moment they are in the same network/LAN. – greenapps Mar 06 '17 at 07:44
  • Common sense sir, I didn't mean to completely imply the sentence of what I'm trying to say. I think "connecting android to mysql through php" would be the best term. Even @VladMatvienko knows the problem I'm dealing with even without being over reacting to english mistakes that I've made according to you. – SovietSenpai Mar 06 '17 at 07:56
  • Also I'm not that good either with android devices or development, thats why I tend to learn and ask questions to some folks here. – SovietSenpai Mar 06 '17 at 07:57

4 Answers4

5

I had the same Issue today, and i solved it by making my PC discoverable in private networks. Steps to make your PC Discoverable:

Go to network settings.
>Go to Manage known networks.
>Choose your network.
>Now turn on the PC discoverable feature.

It helped for me, i hope it will help you.

Vinod Kumar
  • 51
  • 1
  • 3
  • I forgot to answer this thread. I'm glad It worked out for you by that method, but the way I did it is I asked for our Network Admin's assistance and he created a virtual network with the different Local IP's but with same DNS and some network protocols since the firewall of the company is very well secured. – SovietSenpai Jan 16 '18 at 16:00
  • The connection was successful and the register function in my android app worked well with the php. Then from what I remember, the net admin traced the connection from my app to the local server and so it seems that firewall was really the issue and that it keeps blocking unwanted IP's from the very start. – SovietSenpai Jan 16 '18 at 16:04
  • Hence the code is still the same together with the tutorial link that I've stated above. – SovietSenpai Jan 16 '18 at 16:06
1

I have faced such problem but overcome through allow port 1433(my port) to firewall .

I have Followed below Steps: Configure Firewall and Security Setting -> Advanced Setting ->Inbound Rules -> New Rule-> Port -> Next ->Specific Local Port(1433)->Next-> Allow the Connection -> Next ->Next-> Add Name-> Finish

Sukanta Pal
  • 33
  • 1
  • 1
  • 6
0

I also faced the same problem today and I solved it when I turned off the fire wall and network protection setting.

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
jaffar
  • 1
  • 1
0

I solved my problem when I change Network profile from private to public. enter image description here

ZarNi Myo Sett Win
  • 1,353
  • 5
  • 15
  • 37