如何设置来自 HTTP protect URL 的图像在不同的 listView 中用 jsoup 解析?

How can I set images from HTTP protect URL parsed with jsoup in different listView?

执行上面的代码我在 objImages

中有一个包含 url 个图像的数组列表
Element table2 = document.select("TABLE").get(1); 
                Elements asWithName  = table2.select("tr>td a[name]");
                for (Element aWithName : asWithName) {
                    String name = aWithName.attr("name");
                    hostName.add(name);
                    Element tr = aWithName.parent().parent();
                    for (Element td : tr.select("td")){
                        Element img = td.select("img").first();
                        if (img == null){
                            continue;
                        }
                    String imgRelPath = img.attr("src");
                    images.add("http://hostname.com"+imgRelPath);
                    }
                    objImages = images.toArray(); 
                }
                objHostName = hostName.toArray();

好的,我有网址。现在我必须从这些 URL 中获取图像并将其放入 imageView 中,每个图像在不同的 imageView 中。

我正在做:

for {int i=0; i<objImages.length; i++}
    InputStream input = new java.net.URL(objImages[i]).openStream();
    bitmap = BitmapFactory.decodeStream(input);
    ...
}

问题是 http://hostname.com/hobbit/gifs/static/green.gif 受 user/password 保护(带有 .htaccess 文件)。

但是没用。有什么想法吗?

提前致谢。

检查 this for basic authentication and this 使用 jsoup 下载图像

最后你会得到这样的结果

String username = "foo";
String password = "bar";
String login = username + ":" + password;
String base64login = new String(Base64.encodeBase64(login.getBytes()));

Response resultImageResponse = Jsoup.header("Authorization", "Basic " + base64login)
                                    .connect(imageLocation).ignoreContentType(true).execute();

// output here
FileOutputStream out = (new FileOutputStream(new java.io.File(outputFolder + name)));
out.write(resultImageResponse.bodyAsBytes());  // resultImageResponse.body() is where the image's contents are.
out.close();