2014-10-01 82 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

相关问题