找不到 MalfomedURLException 协议
MalfomedURLException Protocol not found
我正在尝试将图像视图设置为从 url 获取的位图,但是在我 运行 以下代码之后出现以下堆栈跟踪:
public Bitmap getAlbumCover(Context context, String song, String artist) {
this.context = context;
song = song.replace(" ", "%20");
artist = artist.replace(" ", "%20");
final String finalSong = song;
final String finalArtist = artist;
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
ObjectMapper mapper = new ObjectMapper();
Document doc = Jsoup.connect("http://api.spotify.com/v1/search?q=track:" + finalSong + "%20artist:" + finalArtist+"%20" + "&type=track").ignoreContentType(true).get();
String body = String.valueOf(doc.body().text());
JsonNode readTree = mapper.readTree(body);
albumArt = readTree.findPath("url");
try {
URL url = new URL(albumArt.toString());
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
albumImage = BitmapFactory.decodeStream(input);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
} catch (JsonGenerationException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
thread.start();
Log.i("icon", "i"+ albumImage);
return albumImage;
}
这是堆栈跟踪:
03-23 20:18:43.242 6917-6991/.xyz.php_request W/nAnnotationIntrospector: Unable to load JDK7 annotation types; will have to skip
03-23 20:18:43.943 6917-6991/.xyz.php_request E/Error: Protocol not found: "https://i.scdn.co/image/9c2c4a9ac9726bfd996ff96383178bbb5efc59ab"
03-23 20:18:43.943 6917-6991/.xyz.php_request W/System.err: java.net.MalformedURLException: Protocol not found: "https://i.scdn.co/image/9c2c4a9ac9726bfd996ff96383178bbb5efc59ab"
03-23 20:18:43.943 6917-6991/.xyz.php_request W/System.err: at java.net.URL.<init>(URL.java:176)
03-23 20:18:43.943 6917-6991/.xyz.php_request W/System.err: at java.net.URL.<init>(URL.java:125)
03-23 20:18:43.943 6917-6991/.xyz.php_request W/System.err: at pillo.xyz.php_request.songlistadapter.run(songlistadapter.java:124)
03-23 20:18:43.943 6917-6991/.xyz.php_request W/System.err: at java.lang.Thread.run(Thread.java:818)
link 在这里:https://i.scdn.co/image/9c2c4a9ac9726bfd996ff96383178bbb5efc59ab
如果你点击它,它会拉出一张图片。我究竟做错了什么?我也已经尝试用 UTF-8 编码 URL。
编辑:
记录专辑封面 returns:I/album 艺术:“https://i.scdn.co/image/bf44daf8c3f35de83e5875bc49791c1347ef36f0”
What am I doing wrong?
您在 URL url = new URL(albumArt.toString());
这一行调用了错误的 albumArt
方法。您应该调用 albumArt.getTextValue()
而不是 albumArt.toString()
。
为什么:
toString()
给出了 JsonNode
的字符串格式,在您的例子中是字符串 "\"https://i.scdn.co/image/9c2c4a9ac9726bfd996ff96383178bbb5efc59ab\""。
相反,getTextValue()
给出字符串 "https://i.scdn.co/image/9c2c4a9ac9726bfd996ff96383178bbb5efc59ab",这就是 URL
想要。
看出区别了吗?
我正在尝试将图像视图设置为从 url 获取的位图,但是在我 运行 以下代码之后出现以下堆栈跟踪:
public Bitmap getAlbumCover(Context context, String song, String artist) {
this.context = context;
song = song.replace(" ", "%20");
artist = artist.replace(" ", "%20");
final String finalSong = song;
final String finalArtist = artist;
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
ObjectMapper mapper = new ObjectMapper();
Document doc = Jsoup.connect("http://api.spotify.com/v1/search?q=track:" + finalSong + "%20artist:" + finalArtist+"%20" + "&type=track").ignoreContentType(true).get();
String body = String.valueOf(doc.body().text());
JsonNode readTree = mapper.readTree(body);
albumArt = readTree.findPath("url");
try {
URL url = new URL(albumArt.toString());
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
albumImage = BitmapFactory.decodeStream(input);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
} catch (JsonGenerationException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
thread.start();
Log.i("icon", "i"+ albumImage);
return albumImage;
}
这是堆栈跟踪:
03-23 20:18:43.242 6917-6991/.xyz.php_request W/nAnnotationIntrospector: Unable to load JDK7 annotation types; will have to skip
03-23 20:18:43.943 6917-6991/.xyz.php_request E/Error: Protocol not found: "https://i.scdn.co/image/9c2c4a9ac9726bfd996ff96383178bbb5efc59ab"
03-23 20:18:43.943 6917-6991/.xyz.php_request W/System.err: java.net.MalformedURLException: Protocol not found: "https://i.scdn.co/image/9c2c4a9ac9726bfd996ff96383178bbb5efc59ab"
03-23 20:18:43.943 6917-6991/.xyz.php_request W/System.err: at java.net.URL.<init>(URL.java:176)
03-23 20:18:43.943 6917-6991/.xyz.php_request W/System.err: at java.net.URL.<init>(URL.java:125)
03-23 20:18:43.943 6917-6991/.xyz.php_request W/System.err: at pillo.xyz.php_request.songlistadapter.run(songlistadapter.java:124)
03-23 20:18:43.943 6917-6991/.xyz.php_request W/System.err: at java.lang.Thread.run(Thread.java:818)
link 在这里:https://i.scdn.co/image/9c2c4a9ac9726bfd996ff96383178bbb5efc59ab 如果你点击它,它会拉出一张图片。我究竟做错了什么?我也已经尝试用 UTF-8 编码 URL。
编辑:
记录专辑封面 returns:I/album 艺术:“https://i.scdn.co/image/bf44daf8c3f35de83e5875bc49791c1347ef36f0”
What am I doing wrong?
您在 URL url = new URL(albumArt.toString());
这一行调用了错误的 albumArt
方法。您应该调用 albumArt.getTextValue()
而不是 albumArt.toString()
。
为什么:
toString()
给出了 JsonNode
的字符串格式,在您的例子中是字符串 "\"https://i.scdn.co/image/9c2c4a9ac9726bfd996ff96383178bbb5efc59ab\""。
相反,getTextValue()
给出字符串 "https://i.scdn.co/image/9c2c4a9ac9726bfd996ff96383178bbb5efc59ab",这就是 URL
想要。
看出区别了吗?