0

我必须将 twitter 集成到我自己的 android 应用程序中。

我已经运行了该应用程序并显示了登录页面,如果我输入了正确的登录详细信息,它必须编写状态页面。我已经写了一些代码。单击提交按钮会弹出评论已发布的 toast 消息,但是当我检查推特墙时,该消息不存在。我的代码有什么问题?请检查一下,我该如何解决这个问题?

我有以下代码:

public class TestPost extends Activity {
private TwitterApp mTwitter;
private CheckBox mTwitterBtn;
private String username = "";
String review;
private boolean postToTwitter = false;
private static final String twitter_consumer_key = "xxx";
private static final String twitter_secret_key = "xxx";

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

    setContentView(R.layout.post);

    Button postBtn              = (Button) findViewById(R.id.button1);
    final EditText reviewEdit   = (EditText) findViewById(R.id.revieew);

    postBtn.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            String review = reviewEdit.getText().toString();

            if (review.equals("")) return;

            postReview(review);

            if (postToTwitter) postToTwitter(review);

        }
    });

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

    mTwitter.setListener(mTwLoginDialogListener);

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

    mTwitterBtn.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (mTwitter.hasAccessToken()) {
                postToTwitter = mTwitterBtn.isChecked();
            } else {
                mTwitterBtn.setChecked(false);
                mTwitter.authorize();
            } 
        }
    });

    if (mTwitter.hasAccessToken()) {
        username    = mTwitter.getUsername();
        username    = (username.equals("")) ? "No Name" : username;

        mTwitterBtn.setText("  Twitter  (" + username + ")");
    }

}

private void postReview(String review) {
    //post to server

    Toast.makeText(this, "Review posted", Toast.LENGTH_SHORT).show();
}

private void postToTwitter(final String review) {
    new Thread() {
        @Override
        public void run() {
            int what = 0;

            try {
                mTwitter.updateStatus(review);
            } catch (Exception e) {
                what = 1;
            }

            mHandler.sendMessage(mHandler.obtainMessage(what));
        }
    }.start();
}

private Handler mHandler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        String text = (msg.what == 0) ? "Posted to Twitter" : "Post to Twitter failed";
        //String text = "Ve a Good Day";
        Toast.makeText(TestPost.this, text, Toast.LENGTH_SHORT).show();
    }
};
private final TwDialogListener mTwLoginDialogListener = new TwDialogListener() {
    @Override
    public void onComplete(String value) {
        username    = mTwitter.getUsername();
        username    = (username.equals("")) ? "No Name" : username;

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

        postToTwitter = true;

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

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

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

}

4

0 回答 0