【问题标题】:Volley's NetworkImageView with images on FTPVolley 的 NetworkImageView 与 FTP 上的图像
【发布时间】:2015-11-11 05:05:02
【问题描述】:
场景是:“有适用于 HTTP 的适配器,被要求添加 FTP 支持” - 听起来很合理。
有没有办法在同样合理的时间范围内做到这一点?
或者类似的机制/定制的 Volley 库来交换 Volley?
FTP 受密码保护,如果这有什么不同的话。
到目前为止尝试过:
- HttpClientStack:
Scheme 'ftp' not registered
- HurlStack"
libcore.net.url.FtpURLConnection cannot be cast to java.net.HttpURLConnection
- 尝试通过自定义 URLonnection 建立隧道(多个困难,例如 libcore 包未公开)
- 自定义 ImageCache,让 Volley 无需处理 FTP(有什么意义,真的吗?)
【问题讨论】:
标签:
ftp
android-volley
networkimageview
【解决方案1】:
到目前为止,我唯一想到的就是:覆盖 performRequest 为 ftp 链接做一些完全不同的事情。
请注意,这里的代码很糟糕,它几乎没有达到我的目的:
public class HurlStackFtp extends HurlStack {
@Override
public HttpResponse performRequest(Request<?> request, Map<String, String> additionalHeaders)
throws IOException, AuthFailureError {
String urlString = request.getUrl();
if (urlString != null && urlString.startsWith("ftp://")) {
return performFTPRequest(request, additionalHeaders);
} else {
return super.performRequest(request, additionalHeaders);
}
}
public HttpResponse performFTPRequest(Request<?> request, Map<String, String> additionalHeaders)
throws IOException, AuthFailureError {
String url = request.getUrl();
HashMap<String, String> map = new HashMap<String, String>();
map.putAll(request.getHeaders());
map.putAll(additionalHeaders);
// UrlRewriter not supported
InputStream input = getStreamFromFTP(url);
StatusLine responseStatus = new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1),
/*connection.getResponseCode()*/200, /*connection.getResponseMessage()*/"OK");
BasicHttpResponse response = new BasicHttpResponse(responseStatus);
response.setEntity(makeEntity(input));
return response;
}
public static InputStream getStreamFromFTP(String url) throws IOException {
URL parsedUrl = new URL(url);
URLConnection cn = parsedUrl.openConnection();
cn.connect();
return cn.getInputStream();
}
private static HttpEntity makeEntity(InputStream inputStream) throws IOException {
BasicHttpEntity entity = new BasicHttpEntity();
entity.setContent(inputStream);
entity.setContentLength(inputStream.available());
entity.setContentType("binary/octet-stream"); // sigh...
return entity;
}
}