Android模擬器訪問本地Web應用


一個早上就是想讓android的模擬器可以訪問到web的應用程序,但是一直是不可以,弄的不知所措。

On Windows, open the Control Panel, choose Programs, and click Turn Windows Features On or Off. Check the box labeled Internet Information Services and then click the + to its left; continue to drill down into World Wide Web Services→Application Development Features, then check the box to enable CGI (you’ll need the CGI feature to install PHP in Chapter 6, Going Offline). Click OK. After you’ve done this, you can put your web documents in your IIS document root, which is typically located at C:\inetpub\wwwroot. Try this with the test.html file you created in Chapter 1, Getting Started; you should be able to load that file by going to http://localhost/test.html in your browser.

You’ll probably need to authenticate each time you put files into that folder. To work around this problem, you can either use the IIS Manager (Start→Control Panel→System and Security→Administrative Tools) to add a new virtual directory in a folder you have permissions to modify, or you can give yourself control of C:\inetpub\wwwroot (right-click on the folder, choose Properties→Security, and then click Edit→Add, type your username, click OK, then allow Full Control and click OK). If you want to connect to your Windows web server, even over your local network, you’ll need to go into the Control Panel→System and Security→Windows Firewall→Allow A Program or Feature Through Windows Firewall and enable World Wide Web Services.

有一段資料是配置自己的電腦,就像上面說的那樣:打開控制面板--選擇程序和功能--點擊左邊的開啟或關閉windows功能--然后選則CGI,等等...試了很久還是不可以。

后來在上面查的時候看到的是有一段資料的介紹:

如何在Android模擬器上訪問本地的Web應用? 例如,在你的開發機器上啟動一個Tomcat服務,接着打開電腦上的瀏覽器,默認情況下輸入http://localhost:8080/就能夠訪問到Tomcat的主頁面;那么,如果我們想在Android模擬器上來訪問,可以嗎?答案是肯定的,但是訪問的URL會有所變化,這又是為什么呢?請繼續往下看。
        在一般的Java Web程序開發中,我們通常使用localhost或者127.0.0.1來訪問本機的Web服務,但是如果我們在Android模擬器中也采用同樣的地址來訪問,Android模擬器將無法正常訪問到我們的服務,這是為什么呢?我們可以這樣來理解:Android的底層是Linux kernel,包括Android本身就是一個操作系統,因此,這時我們在模擬器的瀏覽器中輸入的localhost或127.0.0.1所代表的是Android模擬器(Android虛擬機),而不是你的電腦,明白了嗎?這就是為什么你在模擬器中使用localhost時會報“Web page not available”的原因。

        那到底要如何才能訪問到本地電腦上的Web應用呢?在Android中,將我們本地電腦的地址映射為10.0.2.2,因此,只需要將原先的localhost或者127.0.0.1換成10.0.2.2,就可以在模擬器上訪問本地計算機上的Web資源了。
        那下面我們就啟動電腦上的Tomcat,然后運行Android模擬器,打開模擬器上的瀏覽器,然后訪問:http://10.0.2.2:8888 是不是已經看到那只可愛的Tomcat貓了呢?訪問截圖如下圖所示。(說明:在我的電腦上,Tomcat服務使用的端口是8888,默認情況是8080)

圖:

試用了,很受用。O(∩_∩)O~呵呵


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM