我正在尝试连接在我的 android 应用程序中运行的 Web 服务器,但是由于某种原因它失败了。
我可以正常连接到网页并使用手机的浏览器,我也可以使用下面的代码连接到谷歌,但由于某种原因我的代码无法连接到我的网络服务器。如果我在端口 80 上托管服务器,我每次都会收到错误代码 503,如果我在 8080 上托管它(这是最好的),我会收到一个超时异常,如下所示。
03-05 20:12:22.432: WARN/System.err(29254): java.net.SocketException: The operation timed out
03-05 20:12:22.602: WARN/System.err(29254): at org.apache.harmony.luni.platform.OSNetworkSystem.connectSocketImpl(Native Method)
03-05 20:12:22.602: WARN/System.err(29254): at org.apache.harmony.luni.platform.OSNetworkSystem.connect(OSNetworkSystem.java:125)
03-05 20:12:22.602: WARN/System.err(29254): at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:227)
03-05 20:12:22.612: WARN/System.err(29254): at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:521)
03-05 20:12:22.612: WARN/System.err(29254): at java.net.Socket.connect(Socket.java:1019)
03-05 20:12:22.612: WARN/System.err(29254): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:67)
03-05 20:12:22.612: WARN/System.err(29254): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnectionManager$ConnectionPool.getHttpConnection(HttpConnectionManager.java:151)
03-05 20:12:22.612: WARN/System.err(29254): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnectionManager.getConnection(HttpConnectionManager.java:73)
03-05 20:12:22.612: WARN/System.err(29254): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnection.getHTTPConnection(HttpURLConnection.java:826)
03-05 20:12:22.612: WARN/System.err(29254): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:812)
03-05 20:12:22.612: WARN/System.err(29254): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnection.getResponseCode(HttpURLConnection.java:1275)
03-05 20:12:22.612: WARN/System.err(29254): at com.project.library.remailer.ServerSelect.downloadDirectory(ServerSelect.java:84)
03-05 20:12:22.622: WARN/System.err(29254): at com.project.main.NewMessage$3$1$1.run(NewMessage.java:156)
代码是:
try {
URL url = new URL(directoryLocation);
HttpURLConnection httpURLConnection = (HttpURLConnection) url
.openConnection();
// The line below is where the exception is called
int response = httpURLConnection.getResponseCode();
if (response == HttpURLConnection.HTTP_OK) {
// Response successful
InputStream inputStream = httpURLConnection.getInputStream();
// Parse it line by line
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(inputStream));
String temp;
while ((temp = bufferedReader.readLine()) != null) {
// Parsing of data here
}
} else {
Log.e("SAMES", "INCORRECT RETURN CODE: " + response);
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
当我尝试 CommonsWare 为端口 8080 建议的链接中的代码时,我得到与以前相同的错误,即超时异常。对于端口 80,我得到以下异常,这两个错误都发生在 url.openStream() 上。
端口 80 异常:
03-06 11:53:53.296: WARN/System.err(639): java.io.FileNotFoundException: http://64.197.194.165:80/path
03-06 11:53:53.376: WARN/System.err(639): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1064)
03-06 11:53:53.376: WARN/System.err(639): at java.net.URL.openStream(URL.java:674)
03-06 11:53:53.376: WARN/System.err(639): at com.project.library.remailer.ServerSelect.downloadDirectory(ServerSelect.java:118)
03-06 11:53:53.376: WARN/System.err(639): at com.project.main.NewMessage$3$1$1.run(NewMessage.java:156)
我还尝试使用以下代码:
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(directoryLocation);
try
{
HttpResponse response = client.execute(request);
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String line;
while((line = reader.readLine()) != null) {
Log.e("SAMES", line);
}
}
catch(Exception e)
{
e.printStackTrace();
}
但是,这导致了端口 8080 的相同超时错误,该错误发生在调用 client.execute(response) 时。
对于端口 80,我得到一个网页,上面说它无法连接,其中很多 HTML 编辑,但是它在页面上说的主要内容是:
03-06 13:10:23.576:错误/SAMES(1267):Web 服务器可能已关闭、太忙或遇到其他问题,无法响应请求。您可能希望稍后再试一次。