首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >安卓设备没有官方SoundCloud app的情况下,如何集成soundcloud和安卓app,获取AccessToken?

安卓设备没有官方SoundCloud app的情况下,如何集成soundcloud和安卓app,获取AccessToken?
EN

Stack Overflow用户
提问于 2014-07-09 21:46:10
回答 1查看 853关注 0票数 0

我需要集成SoundCloud与安卓应用程序,并获得访问令牌,无论安卓设备是否有官方声云应用程序。请建议..。

EN

回答 1

Stack Overflow用户

发布于 2014-07-10 21:52:54

代码语言:javascript
复制
public class SoundCloudActivity extends Activity {

    private WebView mWebView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_soundcloud);
        mWebView = (WebView) findViewById(R.id.webView);

        try {
            InputStream is = getAssets().open("soundcloud.html");

            String data = getResultFromStream(is);

            WebSettings ws = mWebView.getSettings();
            ws.setJavaScriptEnabled(true);
            ws.setJavaScriptCanOpenWindowsAutomatically(true);
            ws.setPluginsEnabled(true);

            MyWebChromeClient chromeClient = new MyWebChromeClient();
            MyWebViewClient webViewClient = new MyWebViewClient();

            mWebView.setWebChromeClient(chromeClient);
            mWebView.setWebViewClient(webViewClient);

            mWebView.loadData(data, "text/html","UTF-8");
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }   
    public class MyWebViewClient extends WebViewClient{     
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {

            Log.e("shouldOverrideUrlLoading","URL: "+url);

            if(url.startsWith("http://connect.soundcloud.com/examples/callback.html") && url.contains("access_token")){
                Toast.makeText(getApplicationContext(), url,Toast.LENGTH_LONG).show();
            }           
            return super.shouldOverrideUrlLoading(view, url);
        }       
    }

    public class MyWebChromeClient extends WebChromeClient{     
        @Override
        public boolean onJsAlert(WebView view, String url, String message,
                JsResult result) {
            return super.onJsAlert(view, url, message, result);
        }       
    }

    private synchronized String getResultFromStream(InputStream stream)
            throws Exception {

        StringBuffer buffer = new StringBuffer();
        int ch = 0;
        while ((ch = stream.read()) != -1)
            buffer.append((char) ch);
        String result = buffer.toString().trim();
        return result;
    }
}

// soundcloud.html文件如下:

代码语言:javascript
复制
<!DOCTYPE html>
<html lang="en">
<head>
    <title>SoundCloud JavaScript SDK Examples</title>
    <link href="http://importer.soundcloudlabs.com/stylesheets/labs.css" media="screen" rel="stylesheet" type="text/css" />
    <link href="examples.css" media="screen" rel="stylesheet" type="text/css" />
    <link href="highlight.css" media="screen" rel="stylesheet" type="text/css" />
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
    <script src="highlight.min.js"></script>
    <script>hljs.initHighlightingOnLoad();</script>
    <script src="examples.js"></script>
  </head>

 <body> 
<script src="http://connect.soundcloud.com/sdk.js"></script>
<script>
  SC.initialize({
    client_id: "c202b469a633a7a5b15c9e10b5272b78",
    redirect_uri: "http://connect.soundcloud.com/examples/callback.html"
  });

  $("#connect").live("click", function(){ 
  alert("Connected");
    SC.connect(function(){
    alert("Connected to fun");
      SC.get("/me", function(me){
        $("#username").text(me.username);
        $("#description").val(me.description);
      });
    });
  });

  $("#update").live("click", function(){
    SC.put("/me", {user: {description: $("#description").val()}}, function(response, error){
      if(error){
        alert("Some error occured: " + error.message);
      }else{
        alert("Profile description updated!");
      }
    });
  });
</script>


<a href="#" class="big button" id="connect">Connect with SoundCloud</a>
<div class="logged-in" style="display: one;">
  <p>
  Logged in as: <span id="username"></span>
  </p>
  Your profile description:
  <input type="text" id="description" class="fullWidth" />
  <button id="update" class="big button">Update your profile description</button>
</div>
 </body> 
 </html>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24655415

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档