2014-10-01 66 views
1

我的筆記本電腦和Android設備在同一個WIFI網絡上。我在我的筆記本電腦上安裝了本地主機(XAMPP),並且我想通過真實設備測試我的應用程序(仿真程序與本地主機正常工作)。使用筆記本電腦作爲本地WiFi的Android開發服務器

我做了一些Google搜索,發現我需要特別注意端口。因此,我將http conf從listen 80更改爲listen 8888。我試圖在我的筆記本電腦瀏覽器上訪問localhost:8888,它的工作正常。

不過,我不斷收到此異常試圖訪問http://my real IP address:8888/test/index.php我的Android設備上的:

java.net.SocketTimeoutException: failed to connect to /my real ip address (port 8888) after 30000ms 

這就是發生異常:

try{ 

    URL url = new URL(getUrl); 
    //Logr.e("WebGetURL: "+getUrl); 
    urlConnection = (HttpURLConnection) url.openConnection(); 
    if(isJson) 
    { 
     urlConnection.setRequestProperty("Content-Type", "application/json; charset=utf-8"); 
     urlConnection.setRequestProperty("Expect", "100-continue"); 
    } 
    urlConnection.setRequestProperty("Cache-Control", "no-cache"); 
    urlConnection.setConnectTimeout(30*1000); 
    urlConnection.setReadTimeout(timeout*1000); 
    urlConnection.setRequestMethod("POST"); 
    urlConnection.setDoInput(true); 
    urlConnection.setDoOutput(true); 

    //urlConnection.setRequestProperty("User-Agent", "Mozilla/5.0 (Linux; U; Android; en-us;) AppleWebKit/522+ (KHTML, like Gecko) Safari/419.3"); 
    //urlConnection.addRequestProperty("http.agent", "Commons-HttpClient/3.1()"); 

    for (Entry<String, List<String>> entry : 
     urlConnection.getRequestProperties().entrySet()) { 
    } 

    OutputStream in = new BufferedOutputStream(urlConnection.getOutputStream()); 
    ret = convertStreamToStringOutputStream(urlConnection,in,postData); 
    //Logr.d("xx",ret); 
} catch (Exception e) { 
    e.printStackTrace(); 
}finally { 
    //Logr.d("zz", "error, disconnected"); 
    urlConnection.disconnect(); 
} 
return ret; 
+1

您的防火牆可能會阻止連接。 – Squonk 2014-10-01 09:34:53

+0

使用Android的終端模擬器來ping您的IP。如果它響應,那麼在你的代碼中使用它。 – 2014-10-01 09:58:25

+0

@dpsingh我可以從Android設備ping我的IP地址...但仍然我的應用程序拋出異常 – 2014-10-01 11:19:13

回答

1

嘗試使用localhost或本地網絡IP address而比全球IP地址(我的IP地址)。

+0

localhost不工作​​,我可以在哪裏找到我的本地網絡IP?我用192.168.bla bla – 2014-10-01 11:16:48

+0

是的。這應該工作 – 2014-10-01 11:27:29

+0

它不能正常工作,這就是爲什麼我創建了這個問題 – 2014-10-01 11:32:07

相關問題