android - twitter:收到回调但未显示

时间:2011-01-24 09:44:45

标签: android twitter-oauth

我正在试图弄清楚为什么我的应用程序无法按照我的预期运行。

我的应用想要在Twitter上使用OAuth(正在运行),并且在回调时应该输入onNewIntent方法。它没有这样做,我无法弄清楚原因。

所以点击twitter按钮会打开一个网页,我可以在其中输入我的凭据。然后我得到一个屏幕,说明它是成功的,我将被重定向回我的应用程序。我回到我的应用程序,但从未调用onNewIntent方法。我在onNewIntend方法的开头放了一个断点并启动了调试器。它并不止于那种方法!它确实在其他方法中停止,因此它不能成为调试器。

在我的日志文件(包含)中,从底部开始的第三行,您可以看到它正在启动一个新意图。那么......这里发生了什么?

有人可以解释一下吗?我很困惑。

这是我点击twitterbutton时触发的代码:

callBackURL = "myapp://twitactivity";
httpOauthConsumer = new CommonsHttpOAuthConsumer(consKey, consSec);
httpOauthProvider = new DefaultOAuthProvider("http://twitter.com/oauth/request_token","http://twitter.com/oauth/access_token", "http://twitter.com/oauth/authorize");

String authUrl = httpOauthProvider.retrieveRequestToken(httpOauthConsumer, callBackURL);
startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse(authUrl)));

这是我的onNewIntent方法的起始代码:

public void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    Log.i("onNewIntent", "Yep success.");
    Uri uri = intent.getData();
    if(uri != null && uri.toString().startsWith(callBackURL)) {
        String verifier = uri.getQueryParameter(oauth.signpost.OAuth.OAUTH_VERIFIER);
    etc......

我的清单文件中还有以下内容:

<activity android:name=".TwitterScreen">
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data android:scheme="myapp" android:host="twitactivity" />
    </intent-filter>
</activity>

这就是我在日志文件中看到的内容:

01-24 10:23:36.064: INFO/ActivityManager(61): Displayed activity nl.gemoro.android.demo/.TwitterScreen: 6660 ms (total 6660 ms)
01-24 10:23:38.614: INFO/global(348): Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required.
01-24 10:23:44.025: INFO/ActivityManager(61): Starting activity: Intent { act=android.intent.action.VIEW dat=http://twitter.com/oauth/authorize?oauth_token=KjCUNMKg13OClyRjQff94QWKfoRBUpNLE2uF9cJkHA cmp=com.android.browser/.BrowserActivity }
01-24 10:23:44.185: INFO/ActivityManager(61): Start proc com.android.browser for activity com.android.browser/.BrowserActivity: pid=355 uid=10034 gids={3003, 1015}
01-24 10:23:44.724: INFO/ActivityThread(355): Publishing provider browser: com.android.browser.BrowserProvider
01-24 10:23:46.704: INFO/ActivityManager(61): Displayed activity com.android.browser/.BrowserActivity: 2544 ms (total 2544 ms)
01-24 10:23:53.624: WARN/KeyCharacterMap(355): No keyboard for id 0
01-24 10:23:53.624: WARN/KeyCharacterMap(355): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
01-24 10:23:54.574: DEBUG/dalvikvm(348): GC_EXPLICIT freed 3965 objects / 267064 bytes in 2364ms
01-24 10:24:01.064: INFO/ActivityManager(61): Starting activity: Intent { act=android.intent.action.VIEW cat=[android.intent.category.BROWSABLE] dat=myapp://twitactivity?oauth_token=KjCUNMKg13OClyRjQff94QWKfoRBUpNLE2uF9cJkHA&oauth_verifier=41owXswx5TsxHhRyiviFRkRvcpdekm7akRa2IFFM cmp=nl.gemoro.android.demo/.TwitterScreen }
01-24 10:24:02.174: INFO/global(348): Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required.
01-24 10:24:03.594: INFO/ActivityManager(61): Displayed activity nl.gemoro.android.demo/.TwitterScreen: 2412 ms (total 2412 ms)

3 个答案:

答案 0 :(得分:4)

您必须使用onResume()方法

从Twitter服务获取数据

  @Override
protected void onResume() {
    super.onResume();
    Uri uri = getIntent().getData();
            Uri CALLBACK_URI = Uri.parse("myapp://twitactivity");
        if (uri != null && CALLBACK_URI.getScheme().equals(uri.getScheme())) {
                String oauth_token=ri.getQueryParameter("oauth_token");
    String oauth_verifier= uri.getQueryParameter("oauth_verifier");
  Intent i = new Intent(this, YourClass.class); // Go to your activity
  startActivity(i); 
                          }

所以你从twitter获得数据,现在你开始了一个新的活动

答案 1 :(得分:1)

要正确致电onNewIntent,您需要为lauchMode="singleInstance"文件中的活动指定"singleTop"AndroidManifest.xml

答案 2 :(得分:0)

我建议你使用WebView而不是在url中打开它。

我已经给出answer描述成功实施Twitter的步骤。