简体   繁体   中英

How to access localhost from a real android device

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!

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

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM