繁体   English   中英

通过带有xampp服务器的android模拟器连接到localhost

[英]connecting to localhost through android emulator with xampp server

通过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);
        }

即使我检查了防火墙设置,上述所有url都无法连接,但无法正常工作。 需要帮助我被困在这个线程中。

要从android模拟器访问本地主机,请使用ipconfig中提供的ip地址。

打开命令提示符->输入ipconfig->获取IPV4地址....类似192.xx ...

在此处输入图片说明

因此,您的URL =“ http://192.xx.xx.xx/folder/file.php”

使用您的android浏览器测试此网址。...如果连接成功,则android应用中应该没有问题

要设置本地测试环境,可以使用localtunnel 该应用程序为您的本地服务器创建一个全局https服务器。

例如

lt --port 8080将为以下主机创建https连接:localhost:8080 /

终于,我努力尝试并取得了成功,在这里,我希望所有那些完全陷入此类问题的家伙们发表我的回答。 好的,我在这里使用这个网址模式

 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.

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