我可以使用volley(http url)
成功获取图像但我想要的是从本地服务器(Windows PC)获取图像。它的url就像file:// PCname / ....
错误是ftpurlconnection类无法强制转换为httpurlconnection类。
以下是http:
的课程
public class HurlStack implements HttpStack {
private static final String HEADER_CONTENT_TYPE = "Content-Type";
private final HurlStack.UrlRewriter mUrlRewriter;
private final SSLSocketFactory mSslSocketFactory;
public HurlStack() {
this((HurlStack.UrlRewriter)null);
}
public HurlStack(HurlStack.UrlRewriter urlRewriter) {
this(urlRewriter, (SSLSocketFactory)null);
}
public HurlStack(HurlStack.UrlRewriter urlRewriter, SSLSocketFactory sslSocketFactory) {
this.mUrlRewriter = urlRewriter;
this.mSslSocketFactory = sslSocketFactory;
}
public HttpResponse performRequest(Request<?> request, Map<String, String> additionalHeaders) throws IOException, AuthFailureError {
String url = request.getUrl();
HashMap map = new HashMap();
map.putAll(request.getHeaders());
map.putAll(additionalHeaders);
if(this.mUrlRewriter != null) {
String parsedUrl = this.mUrlRewriter.rewriteUrl(url);
if(parsedUrl == null) {
throw new IOException("URL blocked by rewriter: " + url);
}
url = parsedUrl;
}
URL parsedUrl1 = new URL(url);
HttpURLConnection connection = this.openConnection(parsedUrl1, request);
Iterator protocolVersion = map.keySet().iterator();
while(protocolVersion.hasNext()) {
String responseCode = (String)protocolVersion.next();
connection.addRequestProperty(responseCode, (String)map.get(responseCode));
}
setConnectionParametersForRequest(connection, request);
ProtocolVersion protocolVersion1 = new ProtocolVersion("HTTP", 1, 1);
int responseCode1 = connection.getResponseCode();
if(responseCode1 == -1) {
throw new IOException("Could not retrieve response code from HttpUrlConnection.");
} else {
BasicStatusLine responseStatus = new BasicStatusLine(protocolVersion1, connection.getResponseCode(), connection.getResponseMessage());
BasicHttpResponse response = new BasicHttpResponse(responseStatus);
response.setEntity(entityFromConnection(connection));
Iterator i$ = connection.getHeaderFields().entrySet().iterator();
while(i$.hasNext()) {
Entry header = (Entry)i$.next();
if(header.getKey() != null) {
BasicHeader h = new BasicHeader((String)header.getKey(), (String)((List)header.getValue()).get(0));
response.addHeader(h);
}
}
return response;
}
}
private static HttpEntity entityFromConnection(HttpURLConnection connection) {
BasicHttpEntity entity = new BasicHttpEntity();
InputStream inputStream;
try {
inputStream = connection.getInputStream();
} catch (IOException var4) {
inputStream = connection.getErrorStream();
}
entity.setContent(inputStream);
entity.setContentLength((long)connection.getContentLength());
entity.setContentEncoding(connection.getContentEncoding());
entity.setContentType(connection.getContentType());
return entity;
}
protected HttpURLConnection createConnection(URL url) throws IOException {
return (HttpURLConnection)url.openConnection();
}
private HttpURLConnection openConnection(URL url, Request<?> request) throws IOException {
HttpURLConnection connection = this.createConnection(url);
int timeoutMs = request.getTimeoutMs();
connection.setConnectTimeout(timeoutMs);
connection.setReadTimeout(timeoutMs);
connection.setUseCaches(false);
connection.setDoInput(true);
if("https".equals(url.getProtocol()) && this.mSslSocketFactory != null) {
((HttpsURLConnection)connection).setSSLSocketFactory(this.mSslSocketFactory);
}
return connection;
}
static void setConnectionParametersForRequest(HttpURLConnection connection, Request<?> request) throws IOException, AuthFailureError {
switch(request.getMethod()) {
case -1:
byte[] postBody = request.getPostBody();
if(postBody != null) {
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.addRequestProperty("Content-Type", request.getPostBodyContentType());
DataOutputStream out = new DataOutputStream(connection.getOutputStream());
out.write(postBody);
out.close();
}
break;
case 0:
connection.setRequestMethod("GET");
break;
case 1:
connection.setRequestMethod("POST");
addBodyIfExists(connection, request);
break;
case 2:
connection.setRequestMethod("PUT");
addBodyIfExists(connection, request);
break;
case 3:
connection.setRequestMethod("DELETE");
break;
case 4:
connection.setRequestMethod("HEAD");
break;
case 5:
connection.setRequestMethod("OPTIONS");
break;
case 6:
connection.setRequestMethod("TRACE");
break;
case 7:
addBodyIfExists(connection, request);
connection.setRequestMethod("PATCH");
break;
default:
throw new IllegalStateException("Unknown method type.");
}
}
private static void addBodyIfExists(HttpURLConnection connection, Request<?> request) throws IOException, AuthFailureError {
byte[] body = request.getBody();
if(body != null) {
connection.setDoOutput(true);
connection.addRequestProperty("Content-Type", request.getBodyContentType());
DataOutputStream out = new DataOutputStream(connection.getOutputStream());
out.write(body);
out.close();
}
}
public interface UrlRewriter {
String rewriteUrl(String var1);
}
}
我改变了什么,但失败了:
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("file://")) {
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;
}
}
答案 0 :(得分:0)
使用适用于Android的PICASSO库。
http://square.github.io/picasso/
示例:
1)如果它的URL(服务器图像)那么
Picasso.with(上下文).load。(&#34; HTTP:/myserver.com/DvpvklR.png")代入(ImageView的);
2)如果它的URI(本地图像)那么
文件f =新文件(uri)
Picasso.with(getActivity())负荷(F).into(ImageView的);
答案 1 :(得分:0)
您使用的网址是文件:/myserver.com/DvpvklR.png 您将无法在模拟器或设备中访问此功能,因为在操作系统不同的情况下,模拟器(VM的类型),设备(单独的机器),因此无论如何都不可能。 为了实现这一点,您需要在本地计算机上安装一些服务器(Tomcat,Apache或Windows IIS),并将您的图像放在相应的服务器中,但是当您从Android访问时,您必须使用本地IP机器 http://l192.168.100.6 ,指的是设备本身。
您可以在命令行中输入ipconfig来了解您的IP。