我目前正試圖讓我真正的Android設備(三星Galaxy S4迷你)訪問我的PHP腳本,我在我的本地主機。下面的代碼是我有的代碼。該代碼應該檢查用戶名和密碼,如果它存在於我的mysql數據庫中,使用我創建的login_tenant.php腳本。如何從一個真正的Android設備訪問本地主機
代碼:
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());
}
}
我能夠從我的仿真器訪問本地主機,它工作正常。但
當我跑這在我的物理設備,它返回一個錯誤:「異常連接到10.0.2.2拒絕
能否一些你們的告訴我怎麼去解決這個問題,請 我試過嗎?一些東西,但他們沒有工作 1.我改變了本地主機的路徑到10.0.2.2:8080 ..等 2.我關掉了我的防火牆 3.我試圖更換到我的手機的IP路徑地址。
任何建議嗎?我感謝任何幫助!提前致謝!
您是否在manifest中添加了訪問INTERNET權限? – VVB
您使用2G還是Wifi? –
看看這裏如何> http://stackoverflow.com/questions/4779963/how-can-i-access-my-localhost-from-my-android-device –