Skip to content

Commit eb641b3

Browse files
authored
read OpenSpotifyAPI responses in UTF-8 (#1)
1 parent d1158ae commit eb641b3

File tree

3 files changed

+51
-3
lines changed

3 files changed

+51
-3
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ plugins {
44
}
55

66
group 'de.labystudio'
7-
version '1.1.11'
7+
version '1.1.12'
88

99
compileJava {
1010
sourceCompatibility = '1.8'

src/main/java/de/labystudio/spotifyapi/open/OpenSpotifyAPI.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import java.io.IOException;
1313
import java.io.InputStreamReader;
1414
import java.net.URL;
15+
import java.nio.charset.StandardCharsets;
1516
import java.util.concurrent.Executor;
1617
import java.util.concurrent.Executors;
1718
import java.util.function.Consumer;
@@ -288,7 +289,11 @@ public <T> T request(String url, Class<?> clazz, boolean canGenerateNewAccessTok
288289
}
289290

290291
// Read response
291-
JsonReader reader = new JsonReader(new InputStreamReader(connection.getInputStream()));
292+
JsonReader reader = new JsonReader(new InputStreamReader(
293+
connection.getInputStream(),
294+
StandardCharsets.UTF_8
295+
));
296+
292297
return GSON.fromJson(reader, clazz);
293298
}
294299

src/main/java/de/labystudio/spotifyapi/open/model/track/OpenTrack.java

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
21
package de.labystudio.spotifyapi.open.model.track;
32

43
import com.google.gson.annotations.SerializedName;
4+
import de.labystudio.spotifyapi.model.Track;
55

66
import java.util.List;
77

@@ -49,4 +49,47 @@ public class OpenTrack {
4949

5050
public String uri;
5151

52+
private transient String joinedArtists;
53+
54+
/**
55+
* Joins the artists to a single string.
56+
*
57+
* @return the artists name, split with comma
58+
*/
59+
public String getArtists() {
60+
if (this.joinedArtists == null) {
61+
this.joinedArtists = this.getArtists(", ");
62+
}
63+
64+
return this.joinedArtists;
65+
}
66+
67+
/**
68+
* Joins the artists to a single string.
69+
*
70+
* @param delimiter The delimiter to split the artists with
71+
* @return the artists name, split the provided delimiter
72+
*/
73+
public String getArtists(String delimiter) {
74+
if (this.artists == null || this.artists.isEmpty()) {
75+
return null;
76+
}
77+
78+
StringBuilder builder = new StringBuilder();
79+
for (Artist artist : this.artists) {
80+
builder.append(delimiter);
81+
builder.append(artist.name);
82+
}
83+
84+
return builder.substring(delimiter.length());
85+
}
86+
87+
/**
88+
* Create a new {@link Track} based on the current object.
89+
*
90+
* @return The new {@link Track} object
91+
*/
92+
public Track toTrack() {
93+
return new Track(this.id, this.name, this.getArtists(), this.durationMs);
94+
}
5295
}

0 commit comments

Comments
 (0)