Skip to content

Commit

Permalink
read OpenSpotifyAPI responses in UTF-8 (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
jumpingpxl committed Apr 21, 2023
1 parent d1158ae commit eb641b3
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 3 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Expand Up @@ -4,7 +4,7 @@ plugins {
}

group 'de.labystudio'
version '1.1.11'
version '1.1.12'

compileJava {
sourceCompatibility = '1.8'
Expand Down
Expand Up @@ -12,6 +12,7 @@
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.function.Consumer;
Expand Down Expand Up @@ -288,7 +289,11 @@ public <T> T request(String url, Class<?> clazz, boolean canGenerateNewAccessTok
}

// Read response
JsonReader reader = new JsonReader(new InputStreamReader(connection.getInputStream()));
JsonReader reader = new JsonReader(new InputStreamReader(
connection.getInputStream(),
StandardCharsets.UTF_8
));

return GSON.fromJson(reader, clazz);
}

Expand Down
@@ -1,7 +1,7 @@

package de.labystudio.spotifyapi.open.model.track;

import com.google.gson.annotations.SerializedName;
import de.labystudio.spotifyapi.model.Track;

import java.util.List;

Expand Down Expand Up @@ -49,4 +49,47 @@ public class OpenTrack {

public String uri;

private transient String joinedArtists;

/**
* Joins the artists to a single string.
*
* @return the artists name, split with comma
*/
public String getArtists() {
if (this.joinedArtists == null) {
this.joinedArtists = this.getArtists(", ");
}

return this.joinedArtists;
}

/**
* Joins the artists to a single string.
*
* @param delimiter The delimiter to split the artists with
* @return the artists name, split the provided delimiter
*/
public String getArtists(String delimiter) {
if (this.artists == null || this.artists.isEmpty()) {
return null;
}

StringBuilder builder = new StringBuilder();
for (Artist artist : this.artists) {
builder.append(delimiter);
builder.append(artist.name);
}

return builder.substring(delimiter.length());
}

/**
* Create a new {@link Track} based on the current object.
*
* @return The new {@link Track} object
*/
public Track toTrack() {
return new Track(this.id, this.name, this.getArtists(), this.durationMs);
}
}

0 comments on commit eb641b3

Please sign in to comment.