Android Foursquare accessToken: How to get accessToken from inner class?
How can I get the accessToken from the anonymous inner class below? when I try to use the accessToken outside the class it shows up as null. I tried to display a toast with the accessToken right after I closed the inner class and it just shows up as null. I want to be able to use the accessToken in an asynctask to fetch some data. How can I go about doing this?
public class main extends Activity {
public static final String CALLBACK_URL = "url";
public static final String CLIENT_ID = "id";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String url =
"https://foursquare.com/oauth2/authenticate" +
"?client_id=" + CLIENT_ID +
"&response_type=token" +
"&redirect_uri=" + CALLBACK_URL;
// If authentication works, we'll get redirected to a url with a pattern like:
//
// http://YOUR_REGISTERED_REDIRECT_URI/#access_token=ACCESS_TOKEN
//
// We can override onPageStarted() in the web client and grab the token out.
WebView webview = (WebView)findViewById(R.id.webView);
webview.getSettings().setJavaScriptEnabled(true);
webview.setWebViewClient( new WebViewClient() {
public void onPageStarted(WebView view, String url, Bitmap favicon) {
String fragment = "#access_token=";
int start = url.indexOf(fragment);
if (start > -1) {
// You can use the accessToken for api calls now.
String accessToken = url.substring(start + fragment.length(), url.length());
Toast.makeText(main.this, "Token: " + accessToken, Toast.LENGTH_SHORT).show();
}
}
});
webview.loadUrl(url);
开发者_高级运维 }
}
String accessToken =
url.substring(start + fragment.length(), url.length());
There is your access token.
You can pass it to somewhere else in your program. You need to modify the inner class given in the example to fit your needs. As of now, it just displays the token in a dialog box.
精彩评论