Reputation: 763
I need to integrate SoundCloud with Android app and get Access Token whether android device does have official soundcloud app or not. Plese suggest...
Upvotes: 0
Views: 865
Reputation: 763
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;
}
}
// and soundcloud.html file is below:
<!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>
Upvotes: 1