![](/img/trans.png)
[英]How to access the setRequestProperty set from the Android device from PHP code?
[英]How to access localhost from a real android device
我目前正在尝试使我的真实android设备(三星Galaxy S4 mini)访问本地主机中的php脚本。 以下代码是我拥有的位代码。 该代码应该使用创建的login_tenant.php脚本检查用户名和密码是否存在于我的mysql数据库中。
编码:
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地址。
有什么建议么? 感谢您的帮助! 提前致谢!
因此,通过模拟器您可以访问本地主机,但无法从android设备访问同一主机。
这意味着您的Android设备使用2G / 3G或WIFI连接来访问本地主机。 如果那时使用本地主机进行连接,那么您必须位于同一网络中,那么只有您可以访问本地主机。
但是在仿真器中,您可以在同一网络(可能是本地主机网络)中运行,这就是为什么。
最后,可能的解决方案是
还要在Android设备中使用相同的网络(本地主机网络)。 意味着使用Wifi连接到相同的本地主机Internet连接。 这次2G / 3G连接无法使用,因为它们不在同一网络中
将构建部分部署到外部,并访问外部IP地址进行连接。 这次2G / 3G或WIFI都可以使用
是否可以检查/ etc / apache2 / sites-available / default文件,下面的行本身是否是这样(允许)还是拒绝?
<Directory />
AllowOverride All
</Directory>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.