gpt4 book ai didi

java - Android Foursquare accessToken : How to get accessToken from inner class?

转载 作者:行者123 更新时间:2023-12-01 15:48:40 29 4
gpt4 key购买 nike

如何从下面的匿名内部类获取accessToken?当我尝试在类外部使用 accessToken 时,它显示为 null。我试图在关闭内部类后立即使用 accessToken 显示一个 toast,但它只是显示为 null。我希望能够在异步任务中使用 accessToken 来获取一些数据。我该怎么做呢?

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());

这是您的访问 token 。

您可以将其传递到程序中的其他位置。您需要修改示例中给出的内部类以满足您的需要。截至目前,它仅在对话框中显示 token 。

关于java - Android Foursquare accessToken : How to get accessToken from inner class?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6577024/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com