需求是:在网上有个图片。需要不断的请求。发现图片改变了就要从新加载图片。
方法: 想到的方法是判断图片的md5值。md5发生改变了。就是图片变了就更新图片、
然后就搜索到:commons-codec-1.13.jar 这个包里有方法可以直接取到网络上图片的md5值。
首先用了这个方法:
//报错的方法
public String getMd5(String path) {
URL url = null;
String md5Hex = "";
try {
url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
InputStream in = conn.getInputStream();
md5Hex = DigestUtils.md5Hex(in);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return md5Hex;
}
运行以后一直在抛出错误:No static method encodeHexString([B)Ljava/lang/String; in class Lorg/apache/commons/codec/binary/Hex; or its super classes (declaration of 'org.apache.commons.codec.binary.Hex' appears in /system/framework/org.apache.http.legacy.boot.jar)
通过查阅:https://blog.csdn.net/diandianxiyu_geek/article/details/79153703
https://cloud.tencent.com/developer/ask/83814
这两篇博客将方法改为:
//正确的方法
public String getMD5(String path) {
URL url = null;
String md5Hex = "";
try {
url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
InputStream in = conn.getInputStream();
byte[] bytes = DigestUtils.md5(in);
md5Hex = new String(Hex.encodeHex(bytes));
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return md5Hex;
}
拿到了图片的md5值。没有报错。具体为什么报错。我看了半天还是没懂。如果有大神能看懂希望在评论里为小生解惑。非常感谢。