1

I am currently trying to make my real android device (Samsung Galaxy S4 mini) access my php scripts that I have in my localhost. The following code is a bit code that I have. The code supposed to check the username and password if it exist in my mysql database using the login_tenant.php script that I created.

The code:

void loginAsTenant() {

    try {

        HttpClient httpclient = new DefaultHttpClient();

        HttpPost httpPost = new    HttpPost("http://10.0.2.2/aircraftoperatorapp/login_tenant.php");

        String username = txtUsername.getText().toString();
        String password = txtPassword.getText().toString();

        nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("username", username)); 
        nameValuePairs.add(new BasicNameValuePair("password", password));

        response = httpclient.execute(httpPost);

        httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));


        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        final String response = httpclient.execute(httpPost, responseHandler);
        System.out.println("Response: " + response);

        runOnUiThread(new Runnable() {
            public void run() {
                //lblResponse.setText("Response from PHP: " + response);
                dialog.dismiss();
            }
        });

        if(response.equalsIgnoreCase("User Found")){
            runOnUiThread(new Runnable() {
                public void run() {
                    Toast.makeText(Homepage.this,"Login Success", Toast.LENGTH_SHORT).show();
                    //Intent i = new Intent(getApplicationContext(), ArrivalTimeSched.class);
                    //startActivity(i);
                }
           });
            Intent intent = new Intent("com.example.aircraftoperatorapp.TenantPage");
            users = new Bundle();

            users.putString("loggedInTenant", username);

            intent.putExtras(users);
            startActivityForResult(intent, requestCode);

        }
        else {
            showAlert();                
        }

    }
    catch (Exception e ){
         dialog.dismiss();
         System.out.println("Exception : " + e.getMessage());

    }

}

I am able to access the local host from my emulator, it is working fine. But

When I ran this in my physical device it returned an error: "exception connection to 10.0.2.2 refused.

Can some of you guys tell me how to get around this problem, please? I tried a couple of things but they didn't work 1. I change the path of the localhost into 10.0.2.2:8080.. so on 2. I turned off my firewall 3. I tried replacing the path into my phone's ip address.

Any suggestions? I appreciate any help! Thanks in advance!

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
paopao33
  • 107
  • 4
  • 13

2 Answers2

1

So, through your emulator your accessing the local host but your not able access the same from android device.

It means your android device your using 2G/3G or WIFI connection to hit the localhost. If you used localhost for connection that time you must be in the same network, then only you can access the localhost.

But in emulator your in the same network (probably local host network) that's why it is working.

Finally the possible solutions for your approach is

  • Use the same network (local host network) in your android device also. Means connect with Wifi to same local host internet connection. This time 2G/3G connections won't work, as they are not in same network

  • Deploy the build some where externally and access the external ip address for connection. this time both 2G/3G or WIFI will work

King of Masses
  • 18,405
  • 4
  • 60
  • 77
  • I do have the permission.INTERNET. My android emulator can access the localhost/mysql database. My phone is currently connected directly into my computer via USB. My program is working on the emulator, so I am trying it using a physical device. The IP address is the same as you said I do use Wifi – paopao33 May 04 '15 at 04:57
  • thank you @Gangulian I'll check your suggestions out – paopao33 May 04 '15 at 05:12
  • The problem still exist. Is there any problem with the code if possible? – paopao33 May 04 '15 at 05:22
  • if it is working in the emulator means, there is no problem with the code. Only problem is with the network which is used in android device – King of Masses May 04 '15 at 05:36
  • I accessed the localhost using the ipv4 address on my phone and it works, however it tells me i have don't permission to access the server. How do I fix this? does the httpd.config or any config files have to do with this? my version of apache is apache2.4.9, just so you guys know! – paopao33 May 05 '15 at 15:18
  • for that you need to give permissions in your server for accessing by other. So now the problem is not from the android mobile side. – King of Masses May 06 '15 at 04:26
0

Can you check the /etc/apache2/sites-available/default file, whether the below line is like this itself(Allow) or Deny

<Directory />
    AllowOverride All
 </Directory>
Sanjay Kumar N S
  • 4,653
  • 4
  • 23
  • 38