无法在Android应用程序中集成twitter

时间:2012-07-16 12:24:45

标签: android twitter oauth

  

可能重复:
  Twitter API not accepting callback URL for Android App

当我尝试在我的Android应用程序中集成twitter时,它会显示此错误...

为什么会显示此错误?

此错误的解决方案是什么?

任何人都可以帮助我?

07-16 17:44:05.875: W/System.err(395): oauth.signpost.exception.OAuthCommunicationException: Communication with the service provider failed: http://twitter.com/oauth/request_token
07-16 17:44:05.905: W/System.err(395):  at oauth.signpost.AbstractOAuthProvider.retrieveToken(AbstractOAuthProvider.java:214)
07-16 17:44:05.905: W/System.err(395):  at oauth.signpost.AbstractOAuthProvider.retrieveRequestToken(AbstractOAuthProvider.java:69)
07-16 17:44:05.905: W/System.err(395):  at net.londatiga.android.TwitterApp$2.run(TwitterApp.java:117)
07-16 17:44:05.905: W/System.err(395): Caused by: java.io.FileNotFoundException: http://twitter.com/oauth/request_token
07-16 17:44:05.905: W/System.err(395):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1061)
07-16 17:44:05.915: W/System.err(395):  at oauth.signpost.basic.HttpURLConnectionResponseAdapter.getContent(HttpURLConnectionResponseAdapter.java:18)
07-16 17:44:05.925: W/System.err(395):  at oauth.signpost.AbstractOAuthProvider.handleUnexpectedResponse(AbstractOAuthProvider.java:228)
07-16 17:44:05.925: W/System.err(395):  at oauth.signpost.AbstractOAuthProvider.retrieveToken(AbstractOAuthProvider.java:189)
5:44 PM

代码....

public class TestConnect extends Activity {
    private TwitterApp mTwitter;
    private CheckBox mTwitterBtn;

    private static final String twitter_consumer_key = "xxxxxxxxxxxx";
    private static final String twitter_secret_key = "xxxxxxxxxxxxx";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        mTwitterBtn = (CheckBox) findViewById(R.id.twitterCheck);

        mTwitterBtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                onTwitterClick();
            }
        });

        mTwitter    = new TwitterApp(this, twitter_consumer_key,twitter_secret_key);

        mTwitter.setListener(mTwLoginDialogListener);

        if (mTwitter.hasAccessToken()) {
            mTwitterBtn.setChecked(true);

            String username = mTwitter.getUsername();
            username        = (username.equals("")) ? "Unknown" : username;

            mTwitterBtn.setText("  Twitter (" + username + ")");
            mTwitterBtn.setTextColor(Color.WHITE);
        }

        Button goBtn = (Button) findViewById(R.id.button1);

        goBtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(TestConnect.this, TestPost.class));
            }
        });
    }

    private void onTwitterClick() {
        if (mTwitter.hasAccessToken()) {
            final AlertDialog.Builder builder = new AlertDialog.Builder(this);

            builder.setMessage("Delete current Twitter connection?")
                   .setCancelable(false)
                   .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                       public void onClick(DialogInterface dialog, int id) {
                           mTwitter.resetAccessToken();

                           mTwitterBtn.setChecked(false);
                           mTwitterBtn.setText("  Twitter (Not connected)");
                           mTwitterBtn.setTextColor(Color.GRAY);
                       }
                   })
                   .setNegativeButton("No", new DialogInterface.OnClickListener() {
                       public void onClick(DialogInterface dialog, int id) {
                            dialog.cancel();

                            mTwitterBtn.setChecked(true);
                       }
                   });
            final AlertDialog alert = builder.create();

            alert.show();
        } else {
            mTwitterBtn.setChecked(false);

            mTwitter.authorize();
        }
    }

    private final TwDialogListener mTwLoginDialogListener = new TwDialogListener() {
        @Override
        public void onComplete(String value) {
            String username = mTwitter.getUsername();
            username        = (username.equals("")) ? "No Name" : username;

            mTwitterBtn.setText("  Twitter  (" + username + ")");
            mTwitterBtn.setChecked(true);
            mTwitterBtn.setTextColor(Color.WHITE);

            Toast.makeText(TestConnect.this, "Connected to Twitter as " + username, Toast.LENGTH_LONG).show();
        }

        @Override
        public void onError(String value) {
            mTwitterBtn.setChecked(false);

            Toast.makeText(TestConnect.this, "Twitter connection failed", Toast.LENGTH_LONG).show();
        }
    };
} 

1 个答案:

答案 0 :(得分:0)

检查这些条件 1.在twitter中注册应用以获取twitter_consumer_key和twitter_secret_key。 2.检查应用是否具有互联网权限