当Android设备没有官方的SoundCloud应用程序时,如何将SoundCloud与Android应用程序集成并获取AccessToken?

当Android设备没有官方的SoundCloud应用程序时,如何将SoundCloud与Android应用程序集成并获取AccessToken?,android,soundcloud,Android,Soundcloud,我需要将SoundCloud与Android应用程序集成,并获取访问令牌,无论Android设备是否有官方SoundCloud应用程序。请建议 public class SoundCloudActivity extends Activity { private WebView mWebView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(s

我需要将SoundCloud与Android应用程序集成,并获取访问令牌,无论Android设备是否有官方SoundCloud应用程序。请建议

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文件如下:

<!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>

SoundCloudJavaScript SDK示例
hljs.initHighlightingOnLoad();
SC.initialize({
客户id:“C202B469A63A7A5B15C9E10B5272B78”,
重定向\u uri:“http://connect.soundcloud.com/examples/callback.html"
});
$(“#连接”).live(“单击”,函数(){
警报(“已连接”);
SC.connect(函数(){
警报(“连接到娱乐”);
SC.get(“/me”,函数(me){
$(“#用户名”).text(me.username);
$(“#说明”).val(我的说明);
});
});
});
$(“#更新”).live(“单击”,函数(){
SC.put(“/me”,{user:{description:$(“#description”).val()},函数(响应,错误){
如果(错误){
警报(“发生了一些错误:+错误。消息”);
}否则{
警报(“配置文件描述已更新!”);
}
});
});

以以下身份登录:

您的个人资料描述: 更新您的个人资料描述