Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*
 * Copyright (c) 2015. Roberto  Prato <https://github.com/robertoprato>
 *
 *  *
 *  *
 *  *    Licensed under the Apache License, Version 2.0 (the "License");
 *  *    you may not use this file except in compliance with the License.
 *  *    You may obtain a copy of the License at
 *  *
 *  *        http://www.apache.org/licenses/LICENSE-2.0
 *  *
 *  *    Unless required by applicable law or agreed to in writing, software
 *  *    distributed under the License is distributed on an "AS IS" BASIS,
 *  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  *    See the License for the specific language governing permissions and
 *  *    limitations under the License.
 *
 */

import android.net.Uri;
import android.text.TextUtils;
import java.util.List;

public class Main {
    /**
     *
     * @param baseURL
     * @param key
     * @return the soundcloud track id
     */
    public static String parseSoundCloud(String baseURL) {

        Uri parsed = Uri.parse(baseURL);
        String authority = parsed.getAuthority();
        String trackURL = null;
        if (TextUtils.isEmpty(authority)) {
            return null;
        }
        if (authority.indexOf("api.soundcloud") >= 0) {
            trackURL = baseURL;
        } else if (authority.indexOf("soundcloud") >= 0) {
            Uri parsedURI = Uri.parse(baseURL);
            try {
                trackURL = parsedURI.getQueryParameter("url");
            } catch (Exception exc) {

            }
        }

        if (TextUtils.isEmpty(trackURL) == true) {
            return null;
        }

        Uri base = Uri.parse(trackURL);

        List<String> segments = base.getPathSegments();
        for (String segment : segments) {
            if (TextUtils.isDigitsOnly(segment) == true) {
                return segment;// String.format("https://api.soundcloud.com/tracks/%1$s/stream?consumer_key=%2$s",segment,key);
            }
        }

        return "";
    }
}