- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以我的程序遇到了这个相当奇怪的问题。我尝试做的是检查 Content-Length
URL 的编号,然后在非静态方法中使用该数字以及连接其他类的 if-else 语句。但我的问题是,当我研究互联网以某种方式解决这个问题时,我可以使用这些静态方法来获取 contentLength 并且它不适用于非静态。所以我尝试了一下,但从未真正得到任何结果。我基本上想做的是通过另一个类中的非静态方法调用带有静态方法的类,我想这不应该是问题(或者应该?我真的不知道,因为我从来没有之前使用过静态方法)。然后我使用此代码来确定具有更改日期的随机网页的 contentLength:
public static void main() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();
c.setTime(new Date()); // Now use today date.
c.add(Calendar.DATE, 1); // Adding 1 day
String output = sdf.format(c.getTime());
try {
String urlStr = "http://www.sasintern.de/show_pdf.php?fn=" + output + ".pdf";
URL url = new URL(urlStr);
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
int contentLength = httpConn.getContentLength();
if (contentLength < 2000) {
new NotWorkingActivity();
System.out.println("unknown content length");
} else {
new download2Activity();
System.out.println("content length: " + contentLength + " bytes");
}
InputStream inStream = httpConn.getInputStream();
// now read data
// ...
// close connection
httpConn.disconnect();
} catch (Exception ex) {
ex.printStackTrace();
}
}
之后,当确定页面大小小于2000字节时,应该在另一个类中显示一个Toast,表明该页面不可用,但是当它更大时,则应该打开一个新类,正如您在 else 语句中看到的那样。但它不起作用,我想我只是搞砸了静态方法。我只是对他们不太有经验。对于我可能犯的任何其他错误,我深表歉意。如果我的问题有不同的解决方案,希望更容易理解且更好使用,我将不胜感激。
拜托,我需要有关此问题的帮助,希望您能理解我的意思。预先感谢:)
最佳答案
阅读手册
http://docs.oracle.com/javase/7/docs/api/java/net/URLConnection.html#getContentLength()public int getContentLength()
Returns the value of the content-length header field.
并非所有资源都设置 Content-Length header 。因此,您需要获取输入流并读取所有字节直到结束以对其进行计数。
关于java - 如何使用 URLConnection.getContentLength(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29499733/
在我的程序中,我需要跟踪已打开的与某些 HTTP 服务器的连接列表 - 以便在需要时立即断开连接。 我遇到了以下问题。如果我连接到 HTTP 服务器,一切正常,但如果连接到 HTTPS,则连接不会从列
我正在尝试写信给 URLConnection#getOutputStream ,但是,在我调用 URLConnection#getInputStream 之前,实际上并没有发送任何数据。 .即使我设置
我有一组对象,我试图通过 POST 发送到 API。第一个对象将按原样通过,然后我得到: java.net.ProtocolException: cannot write request body a
我在尝试发送文本时遇到 URLConnection 编码问题。 我的代码是这样的: final URL url = new URL(urlString); final URLConnection ur
我有这种方法,可以从雅虎财经下载 .csv 文件并将其保存在本地。它是在循环期间访问的,因此它从列表中下载许多文件。然而,有时符号输入不正确、不再存在或连接超时。如何修改此方法,以便重试连接超时并跳过
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import
打开 URLConnection 时,我使用以下代码来获取内容长度,但它返回 -1。 URL url = new URL(sUrl[0]); URLConnection connection = ur
默认情况下,URLConnection 的超时为 0,这是无限制的。 XXXXX 的合理值是多少? URL url = ... URLConnection uCon = url.openConnect
我无法打开具有特定网络资源的 URLConnection。我得到了 “java.net.ConnectException:连接超时:”。是因为该域阻止了直接 URL 连接吗?如果是这样,他们是如何阻止
我看过文本文件示例,但是否以与 URLConnection 相同的方式将音频文件直接保存到服务器? 最佳答案 是的,一样的。尽管确保使用二进制输出流将内容写入磁盘。 类似于: URLConne
我一直在尝试从网页获取信息,特别是此网站:http://www.ncbi.nlm.nih.gov/pubmed?term=%22pulmonary%20disease%2C%20chronic%20o
在我使用 Apache 库 (org.apach.httpclient) 向带有参数 (BasicNameValuePair) 的 Php 脚本发出请求之前, 然后现在我想删除那些库以减小 APK 大
我正在使用 android 应用程序,我正在从 url 下载文件。一切正常,但是当互联网连接介于两者之间(打开连接后)时,下载超时永远不会发生并且连接永远不会结束。 给我一个解决这个问题的方案
我只是想了解一下 JacksonJson 库。为此,我尝试将 Places API 中的 JSON 数据转换为字符串。 我的 key 有效(我在浏览器和另一个应用程序中进行了测试),但出现错误。这是代
我目前正在 Eclipse 上使用 Java 7、Maven、Spring MVC 和 Eclipselink JPA 编写 Web 服务,以访问连接到内部网络的温度/湿度传感器的值。我使用 curl
HttpsURLConnection 有问题 - 未使用代理。 这是代码: //proxy String type = "https"; System.getProperties().put(type
我目前正在 Eclipse 上使用 Java 7、Maven、Spring MVC 和 Eclipselink JPA 编写一个 Web 服务,以访问连接到内部网络的温度/湿度传感器的值。我使用cur
在我的应用程序中,我需要下载一些网页。我是这样做的 URL url = new URL(myUrl); HttpURLConnection conn = (HttpURLConnection) url
我正在尝试使用 URLConnection 获得最低级别的字节计数.我已经用 CountingInputStream 计算了两个流传递的数据和 CountingOutputStream来自 Apach
我正在使用这段代码创建一个常规的 HTTP 连接: URLConnection cn = new URL( "http://...." ).openConnection(); cn.connect()
我是一名优秀的程序员,十分优秀!