getOAthAccessToken function misbehaviour during Tw

2019-07-22 15:35发布

问题:

I am just trying to share a text to Twitter from my Android App using twitter4j. First of all what i tried is that i made a new project & in that this particular code was working successfully !

Then i mixed that particular code in my application, login into twitter was done successfully and after that what i found is that this particular code was generating an exception(null pointer).

accessToken = twitter.getOAuthAccessToken(requestToken, verifier);

requestToken before going to browser was got successfully !

Code to login :

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

    }

And code After login :

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>

I didn't understand what did the answer in this question say: Why the method getOAuthAccessToken always fire the exception in the twitter4j api?

So,if anyone can explain better to solve this type of problem then pls help...

回答1:

Delete the line android:targetSdkVersion="19" in AndroidManifest.