Twitter集成期间getOAuthAccessToken函数错误行为

时间:2014-01-20 13:24:48

标签: android twitter twitter-oauth twitter4j

我正在尝试使用twitter4j从我的Android应用程序中分享文本到Twitter。首先我尝试的是我做了一个新项目&因为这个特殊的代码工作成功了!

然后我在我的应用程序中混合了该特定代码,登录到twitter成功完成,之后我发现这个特定代码生成异常(空指针)。

accessToken = twitter.getOAuthAccessToken(requestToken, verifier);
去浏览器之前

requestToken成功了!

登录代码:

     class logintwt extends AsyncTask<String, String, String> {
     ProgressDialog pDialog;
        /**
         * Before starting background thread Show Progress Dialog
         * */
        @Override
        protected void onPreExecute() {
            super.onPreExecute();


            pDialog = new ProgressDialog(Result.this);
            pDialog.setMessage("Loading...");
            pDialog.setCancelable(true);
            pDialog.show();
        }

        /**
         * getting Places JSON
         * */
        protected String doInBackground(String... args) {


                ConfigurationBuilder builder = new ConfigurationBuilder();
                builder.setOAuthConsumerKey(TWITTER_CONSUMER_KEY);
                builder.setOAuthConsumerSecret(TWITTER_CONSUMER_SECRET);
                Configuration configuration = builder.build();

                TwitterFactory factory = new TwitterFactory(configuration);
                twitter = factory.getInstance();

                try {
                    requestToken = twitter.getOAuthRequestToken(TWITTER_CALLBACK_URL);

                } catch (TwitterException e) {
                    e.printStackTrace();
                }

            return null;    
        }   

        /**
         * After completing background task Dismiss the progress dialog and show
         * the data in UI Always use runOnUiThread(new Runnable()) to update UI
         * from background thread, otherwise you will get error
         * **/
        protected void onPostExecute(String file_url) {
            pDialog.dismiss();
            // dismiss the dialog after getting all products
            Intent nint=new Intent(Intent.ACTION_VIEW, Uri.parse(requestToken.getAuthenticationURL()));
            nint.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            getApplicationContext().startActivity(nint);
        }

    }

登录后的代码:

class afterlog extends AsyncTask<String, String, String> {

        ProgressDialog pDialog;
        /**
         * Before starting background thread Show Progress Dialog
         * */
        @Override
        protected void onPreExecute() {
            super.onPreExecute();

            pDialog = new ProgressDialog(Result.this);
            pDialog.setMessage("Loading...");
            pDialog.setCancelable(true);
            pDialog.show();

        }

        /**
         * getting Places JSON
         * */
        protected String doInBackground(String... args) {
              try {
                accessToken = twitter.getOAuthAccessToken(
                        requestToken, verifier); //ERROR this line
                Log.i("accessToken value",accessToken.toString());

                twitter.updateStatus(" good afernoon !!!! djfkdf jdk !!!!!!!!!");                       
                } catch (TwitterException e) {
                e.printStackTrace();
            }
              Log.v("message",""+twitter.toString());
            return twitter.toString();
        }

        /**
         * After completing background task Dismiss the progress dialog and show
         * the data in UI Always use runOnUiThread(new Runnable()) to update UI
         * from background thread, otherwise you will get error
         * **/
        protected void onPostExecute(String file_url) {
        pDialog.dismiss();
        }

    }

Manifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="in.WsCubeTech.cupid"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.USE_CREDENTIALS" />
<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:launchMode="singleInstance"
    android:theme="@android:style/Theme.NoTitleBar.Fullscreen"  >
    <activity
        android:name="in.WsCubeTech.cupid.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
         </intent-filter>
                  <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="oauth" android:host="t4jsample"/>
        </intent-filter>

    </activity>
    <activity android:name="in.WsCubeTech.cupid.Lovername"></activity>
    <activity android:name="in.WsCubeTech.cupid.Birthdate"></activity>
    <activity android:name="in.WsCubeTech.cupid.Result"
        android:launchMode="singleInstance">></activity>
    <activity android:name="in.WsCubeTech.cupid.Love_cast"></activity>

    <meta-data 
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />    
</application>
</manifest>

我不明白这个问题的答案是什么:Why the method getOAuthAccessToken always fire the exception in the twitter4j api?

所以,如果有人能够更好地解释这类问题,那么请帮忙......

1 个答案:

答案 0 :(得分:0)

在AndroidManifest中删除android:targetSdkVersion =“19”行。