![](/img/trans.png)
[英]Using Java, how can I reference a non-standard type xml object in the Android architecture?
[英]How to Create non-Standard or Malformed Java URL object deliberately
我正在尝试使用Glide Android库从网址中加载图片,例如
String urlString = "https://images.example.com/is/image/example/EXMPL1234_021347_1?$cdp_thumb$";
当我在Glide中输入上述网址字符串时,
Glide.with(mContext).load(urlString).into(view);
它实际命中的地址是:
https://images.example.com/is/image/example/EXMPL1234_021347_1?%24cdp_thumb%24
这对我来说是不正确的。 $必须保留。
然后,我尝试传入java.net.URL对象,但是我再次似乎无法构造一个在查询中保留$字符的对象。
这是我尝试过的示例:
URL url = null;
try
{
url = new URL(urlStr);
URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath()+"?$hp_wn_thumb_app$", "", url
.getRef()
+"?$hp_wn_thumb_app$");
url = uri.toURL();
}
catch (MalformedURLException e)
{
e.printStackTrace();
}
catch (URISyntaxException e)
{
e.printStackTrace();
}
如果有人可以提出解决方案,我将不胜感激。 制作非Urlencoded的Java URL对象。
这解决了这个原始问题。
package com.techventus;
import com.bumptech.glide.load.model.GlideUrl;
import com.bumptech.glide.load.model.Headers;
import java.net.MalformedURLException;
import java.net.URL;
/*
// Necessary to help make correct calls to the API which does not apply and //URL special Character correction.
*/
public class CorrectGlideUrl extends GlideUrl
{
String urlString;
public CorrectGlideUrl(URL url)
{
super(url);
}
public CorrectGlideUrl(String url)
{
super(url);
urlString = url;
}
public CorrectGlideUrl(URL url, Headers headers)
{
super(url, headers);
}
public CorrectGlideUrl(String url, Headers headers)
{
super(url, headers);
}
/**
*
* Returns a properly escaped {@link String} url that can be used to make http/https requests.
*
* @see #toURL()
* @see #getCacheKey()
*/
@Override
public String toStringUrl() {
return urlString.replace("%24","$");
// return getSafeStringUrl();
}
/**
* Returns a properly escaped {@link java.net.URL} that can be used to make http/https requests.
*
* @see #toStringUrl()
* @see #getCacheKey()
* @throws MalformedURLException
*/
@Override
public URL toURL() throws MalformedURLException {
return getSafeUrl();
}
// See http://stackoverflow.com/questions/3286067/url-encoding-in-android. Although the answer using URI would work,
// using it would require both decoding and encoding each string which is more complicated, slower and generates
// more objects than the solution below. See also issue #133.
private URL getSafeUrl() throws MalformedURLException {
return null;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.