3

当我尝试在 Twitter 上分享帖子时,我收到此错误我将 twitter4j 用于库的 Twitter 类是:

public class TwitterActivity extends Activity {

    private Twitter twitter;

    private RequestToken requestToken = null;
    public static final String PREFS = "MyPrefsFile";
    final public static String CALLBACK_URL = "app://casa";
    private SharedPreferences shared;
    private RequestToken re;
    private int it = 0;
    private String frase = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_twitter);

        it = getIntent().getExtras().getInt("punteggio");
        frase = getIntent().getExtras().getString("frase");
        shared = this.getSharedPreferences(PREFS, Context.MODE_PRIVATE);

        new updateTwitterStatus().execute();

    }

    @Override
    public void onNewIntent(Intent data) {
        super.onNewIntent(data);
        dealWithTwitterResponse(data);

    }

    class updateTwitterStatus extends AsyncTask<Void, Void, String> {

        private Intent i = null;

        @Override
        protected String doInBackground(Void... params) {

            ConfigurationBuilder cb = new ConfigurationBuilder();

            cb.setDebugEnabled(true)
                    .setOAuthConsumerKey("V***********")
                    .setOAuthConsumerSecret(
                            "****************");

            TwitterFactory tf = new TwitterFactory(cb.build());
            twitter = tf.getInstance();

            try {
// the next line throws the error
                requestToken = twitter.getOAuthRequestToken(CALLBACK_URL);
                Log.i("bauu", "miao");

                String authUrl = requestToken.getAuthenticationURL();

                startActivity(new Intent(Intent.ACTION_VIEW,
                        Uri.parse(requestToken.getAuthenticationURL())));

                return authUrl;

            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }

    }

    @Override
    public void onResume() {
        super.onResume();
    }

    private void dealWithTwitterResponse(Intent intent) {
        Log.i("Sonod entro", "vau");
        final Uri uri = intent.getData();
        if (uri == null) {
            Log.i("è null", "null");
        }
        Log.i("callback funziona", "ciao");
        if (uri != null && uri.toString().startsWith(CALLBACK_URL)) {
            final String verifier = uri.getQueryParameter("oauth_verifier");
            final String oauthToken = uri.getQueryParameter("oauth_token");
            new Thread(new Runnable() {
                public void run() {
                    try {
                        Log.i("SOno dentro il run", "asd");
                        AccessToken at = twitter.getOAuthAccessToken(
                                requestToken, verifier);

                        twitter.setOAuthAccessToken(at);
                        twitter.updateStatus("CHI VUOLE ESSERE SCIENZIATO?? Punteggio: "
                                + it
                                + " "
                                + frase
                                + " "
                                + "www.scienze-naturali.com");

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.punteggio, menu);
        return true;
    }

}

和 xml 文件是:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.applicazionescienza"
    android:versionCode="1"
    android:versionName="1.0" >



    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="17" />

    <uses-permission
    android:name="android.permission.INTERNET" />

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.applicazionescienza.MenuPrincipale"
            android:label="@string/app_name"
            android:launchMode="singleInstance" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity
            android:name="com.example.applicazionescienza.Informazioni"
            android:label="@string/title_activity_informazioni" >
        </activity>
        <activity
            android:name="com.example.applicazionescienza.Regolamento"
            android:label="@string/title_activity_regolamento" >
        </activity>
        <activity
            android:name="com.example.applicazionescienza.Gioca"
            android:label="@string/title_activity_gioca" >
        </activity>
        <activity
            android:name="com.example.applicazionescienza.Livello"
            android:label="@string/title_activity_livello" >
        </activity>
        <activity
            android:name="com.example.applicazionescienza.Punteggio"
            android:label="@string/title_activity_punteggio" >
        </activity>
        <activity
            android:name="com.example.applicazionescienza.TwitterActivity"
            android:label="@string/title_activity_twitter"
            android:launchMode="singleInstance" >
            <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:host="casa"
                    android:scheme="app" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.example.applicazionescienza.FacebookActivity"
            android:label="@string/title_activity_facebook" >
        </activity>
    </application>

</manifest>

我在代码中签入错误所在的行任何人都可以帮助我吗?

4

2 回答 2

3

我在平板电脑上遇到了同样的错误。我似乎在设备上连接到 Internet,但实际上存在连接问题。要修复该错误,我只需要在设备上禁用并重新启用 wifi。

于 2014-11-16T16:29:29.950 回答
0

在同一设备上,尝试在浏览器中访问https://api.twitter.com 。如果它连接了,那么它很可能是应用程序 /twitter4j 的问题,而不是 wi-fi。

于 2014-11-21T17:51:49.277 回答