簡體   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