无法使用Android应用程序访问服务器套接字

时间:2013-07-31 00:42:08

标签: java android

我想看到我的Android应用程序在发出Web请求时发送的确切标题所以我想我只是在我的本地计算机上用java创建一个简单的服务器应用程序并让我的Android应用程序调用它。然后只需将请求转储到控制台,以便我可以看到应用程序正在发送的内容。但是,当我尝试连接时,应用程序挂起并停止响应。

我创建了一个简单的服务器,只接受连接并sysout它获取的数据。服务器运行正常,如果我从计算机上的Web浏览器点击它将从Web浏览器请求打印标题。所以我知道服务器工作正常。

以下是我的应用中的代码:

URL url = new URL("http://192.168.1.11:9000");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.connect();

PrintWriter writer = new PrintWriter(connection.getOuputStream(), true);
writer.write("hi");
writer.close();

简单。我毕竟只想要标题。现在我开始没有帖子并使用:

URL url = new URL("http://192.168.1.11:9000");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();

BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
in.close();

但这不起作用。该应用程序停止响应getInputStream()请求。它只是停止而不会继续。服务器也没有连接请求。

总而言之,应用程序阻止了url连接的getInputStream,我无法弄清楚原因。

现在我已经搜索了一段时间并发现了这些:

Android app communicating with server via sockets

socket exception socket not connected android

Android embedded browser cant connect to server on LAN

Using java.net.URLConnection to fire and handle HTTP requests

Client Socket cannot connect to running Socket server

但没有任何帮助。我没有像所有人一样使用localhost似乎是这个问题而且我已经尝试使用了机器人10.0.0.2,但这也不起作用。

我不在限制任何事情的网络上(我在家)我尝试使用显示的第一组代码,以便向我的服务器发送消息,但即使这样也行不通(它运行正常但是服务器永远不会得到客户端。这有效吗?)。

我尝试使用URLConnection和HttpURLConnection,它们都有同样的问题。

我也在我的应用中使用互联网权限,因此它确实拥有所需的权限。

我现在处于亏损状态。为什么我不能简单地拨打我的服务器?

修改

我使用了androids文档中的确切代码:

private String downloadUrl(String myurl) throws IOException {
    InputStream is = null;
    // Only display the first 500 characters of the retrieved
    // web page content.
    int len = 500;

    try {
        URL url = new URL("http://10.0.2.2:9000");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setReadTimeout(10000 /* milliseconds */);
        conn.setConnectTimeout(15000 /* milliseconds */);
        conn.setRequestMethod("GET");
        conn.setDoInput(true);
        // Starts the query
        conn.connect();
        int response = conn.getResponseCode();
        is = conn.getInputStream();

        // Convert the InputStream into a string
        String contentAsString = readIt(is, len);
        return contentAsString;

        // Makes sure that the InputStream is closed after the app is
        // finished using it.
    } finally {
        if (is != null) {
            is.close();
        }
    }
}

但即使这样也行不通。它仍然挂起。只是现在它挂在getResponseCode()上。然后抛出超时异常。服务器永远不会收到请求。

3 个答案:

答案 0 :(得分:0)

您的地址必须以“http://”开头,请重试!

答案 1 :(得分:0)

我认为你的问题的根源是Android在连接完成之前对你的应用进行FCing,因为我假设你没有将它包装在Loader,AsyncTask或Thread中。我建议您按照培训指南Google provides,将您的调用包装在AsyncTask中,看看是否能解决问题。

答案 2 :(得分:0)

我有一个用于发出HTTP GET请求的Java类,我猜它与你使用的android代码几乎相同,所以下面我已经抛弃了相关的代码部分。我在Java应用程序中多次使用过这个类(不是在Android上)。

currentUrl = new URL(getUrl);
conn = (HttpURLConnection)currentUrl.openConnection();
conn.setRequestProperty("Cookie",  getCookies(currentUrl.getHost()));
conn.setRequestProperty("User-Agent", "robadob.org/crawler");
if(referrer!=null){conn.setRequestProperty("Referrer",referrer);}
conn.setRequestMethod("GET");
conn.connect();
//Get response
String returnPage = "";
String line;
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
while ((line = rd.readLine()) != null) {
    returnPage+=line+"\n";
}
rd.close();

我看不出任何可能导致代码失败的明显事件,但希望你能从中找到一些东西。 setRequestProperty是我设置标题,因此您不需要这些。

如果失败,请使用System.out淹没您的代码,这样您就可以看到它停止的语句。