0
以下の匿名の内部クラスからaccessTokenを取得するにはどうすればよいですか?クラスの外でaccessTokenを使用しようとすると、nullとして表示されます。私は内部クラスを閉じた直後にaccessTokenでトーストを表示しようとしましたが、それはnullとして表示されます。私はいくつかのデータを取得するasynctaskでaccessTokenを使用できるようにしたい。これをやり遂げるにはどうすればいいですか?Android Foursquareアクセストークン:内部クラスから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);
}
}
私はnullとして表示されるクラスの外でaccessTokenを使用しようとします。私は内部クラスを閉じた直後にaccessTokenでトーストを表示しようとしましたが、それはnullとして表示されます。 – mergesort
webviewは非同期に読み込まれます。 onCreateの直後にaccessTokenを使用することはできません。初期化が完了したことを知らせるために完了したら、内部クラスが呼び出すメソッド 'setAccessToken'を作成する必要があります。トークンを必要とする他のコードはそこから開始できます。 – Thilo
それは働いた!どうもありがとうございます。 – mergesort