Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse JSON for information #48

Open
raad1masum opened this issue Nov 1, 2021 · 2 comments
Open

Parse JSON for information #48

raad1masum opened this issue Nov 1, 2021 · 2 comments
Assignees

Comments

@raad1masum
Copy link
Owner

No description provided.

@raad1masum raad1masum reopened this Nov 3, 2021
@raad1masum
Copy link
Owner Author

    public String getData(String token, String id) throws Exception {
        URL url = new URL("https://api.spotify.com/v1/playlists/" + id);

        HttpURLConnection connection = (HttpURLConnection) url.openConnection();

        connection.setDoOutput(true);
        connection.setRequestProperty("Accept-Charset", "UTF-8");
        connection.setRequestProperty("Authorization", "Bearer " + token);

        InputStream response = connection.getInputStream();
        String result = new BufferedReader(new InputStreamReader(response)) .lines().collect(Collectors.joining("\n"));
        
        return result;
    }

    public String getInfo(String info, String data) {
        JSONObject obj = new JSONObject(data);

        return obj.getString(info);
    }

@raad1masum
Copy link
Owner Author

        name = getInfo("name", data);
        image = obj.getJSONArray("images").getJSONObject(0).getString("url");
        description = getInfo("description", data);
        followers = Integer.toString(obj.getJSONObject("followers").getInt("total"));
        owner = obj.getJSONObject("owner").getString("display_name");
        songCount = Integer.toString(obj.getJSONObject("tracks").getInt("total"));
        explicitPercentage = Double.toString(getExplicitPercent(obj)) + "%";

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant