[英]connecting to localhost through android emulator with xampp server
running php script through android program with giving localhost address like 通过android程序运行php脚本,并给出localhost地址,例如
try {
url = "http://10.0.2.2:80/xxx/";
//optionally I tried
//url="http://localhost/xxx/";
//url="http://192.168.1.8/xxx/";
//url="http://127.0.0.1/xxx/";
//above all options are not working for me.
httpclient = new DefaultHttpClient();
request = new HttpGet(url);
response = httpclient.execute(request);
}
try {
BufferedReader rd = new BufferedReader(new InputStreamReader(
response.getEntity().getContent()));
String line = "";
while ((line = rd.readLine()) != null) {
// Appending result to textview
result.append(line);
}
all above url fails to connect even I checked my firewall setting but it's not working. 即使我检查了防火墙设置,上述所有url都无法连接,但无法正常工作。 need help I am stuck in this thread.
需要帮助我被困在这个线程中。
To access your local host from your android emulator Use the ip address given in ipconfig. 要从android模拟器访问本地主机,请使用ipconfig中提供的ip地址。
Open command prompt->type ipconfig-> Get IPV4 address.... something like 192.xx... 打开命令提示符->输入ipconfig->获取IPV4地址....类似192.xx ...
So your URL="http://192.xx.xx.xx/folder/file.php" 因此,您的URL =“ http://192.xx.xx.xx/folder/file.php”
Test this url with your android browser.... If it connects then there should be no problem in android app 使用您的android浏览器测试此网址。...如果连接成功,则android应用中应该没有问题
For setting up a local testing environment, you can use localtunnel . 要设置本地测试环境,可以使用localtunnel 。 This application creates a global https server for your local server.
该应用程序为您的本地服务器创建一个全局https服务器。
Eg 例如
lt --port 8080 will create https connection for : localhost:8080/ lt --port 8080将为以下主机创建https连接:localhost:8080 /
finally I tried hard and got success, Here I wish to post my answer with all those guys were completely stuck with such issues. 终于,我努力尝试并取得了成功,在这里,我希望所有那些完全陷入此类问题的家伙们发表我的回答。 ok Here I am using this Url Pattern
好的,我在这里使用这个网址模式
try {
url = "http://192.xxx.x.x/xxx/index.php";
Log.d("url is ", " "+url);
ThreadPolicy tp = ThreadPolicy.LAX;
StrictMode.setThreadPolicy(tp);
httpclient = new DefaultHttpClient();
request = new HttpGet(url);
//Log.d("requested url", " "+request);
response = httpclient.execute(request);
}
// response code
try {
BufferedReader rd = new BufferedReader(new InputStreamReader(
response.getEntity().getContent()));
String line = "";
while ((line = rd.readLine()) != null) {
// Appending result to textview
result.append(line);
}
}
adding these lines into AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
**Happy Coding to all of you.**
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.