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!