按钮事件在这里:
btnUpdateStatus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Call update status function
// Get the status from EditText
String status = txtUpdate.getText().toString();
// Check for blank text
if (status.trim().length() > 0) {
// update status
new updateTwitterStatus().execute(status);
} else {
// EditText is empty
Toast.makeText(getApplicationContext(),
"Please enter status message", Toast.LENGTH_SHORT)
.show();
}
}
});
这里是更新dmethod class updateTwitterStatus扩展了AsyncTask {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Updating to twitter...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
/**
* getting Places JSON
* */
protected String doInBackground(String... args) {
Log.d("Tweet Text", "> " + args[0]);
String status = args[0];
try {
ConfigurationBuilder builder = new ConfigurationBuilder();
builder.setOAuthConsumerKey(TWITTER_CONSUMER_KEY);
builder.setOAuthConsumerSecret(TWITTER_CONSUMER_SECRET);
// Access Token
String access_token = mSharedPreferences.getString(PREF_KEY_OAUTH_TOKEN, "");
// Access Token Secret
String access_token_secret = mSharedPreferences.getString(PREF_KEY_OAUTH_SECRET, "");
AccessToken accessToken = new AccessToken(access_token, access_token_secret);
Twitter twitter = new TwitterFactory(builder.build()).getInstance(accessToken);
// Update status
twitter4j.Status response = twitter.updateStatus(status);
Log.i(""+response, "value");
Log.d("Status", "> " + response.getText());
} catch (TwitterException e) {
// Error in updating status
Log.d("Twitter Update Error", e.getMessage());
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) {
// dismiss the dialog after getting all products
pDialog.dismiss();
// updating UI from Background Thread
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
"Status tweeted successfully", Toast.LENGTH_SHORT)
.show();
// Clearing EditText field
txtUpdate.setText("");
}
});
}
}
这是XML布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<!-- Twitter Login Button -->
<Button android:id="@+id/btnLoginTwitter"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Login with Twitter"
android:layout_marginLeft="10dip"
android:layout_marginRight="10dip"
android:layout_marginTop="30dip"/>
<!-- user name label -->
<TextView android:id="@+id/lblUserName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dip"
android:layout_marginTop="30dip"/>
<!-- label update status -->
<TextView android:id="@+id/lblUpdate"
android:text="Update Status"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:layout_marginRight="10dip"
android:visibility="visible"/>
<!-- Tweet EditText -->
<EditText android:id="@+id/txtUpdateStatus"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="10dip"
android:visibility="visible"/>
<!-- Tweet Button -->
<Button android:id="@+id/btnUpdateStatus"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Tweet"
android:layout_marginLeft="10dip"
android:layout_marginRight="10dip"
android:visibility="visible"/>
<!-- Twitter Logout button -->
<Button android:id="@+id/btnLogoutTwitter"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Logout from Twitter"
android:layout_marginLeft="10dip"
android:layout_marginRight="10dip"
android:layout_marginTop="50dip"
android:visibility="visible"/>
</LinearLayout>
我正在使用twitter SDK和twiiter 4jcore库来发送消息并使用twitter登录。我正在成功登录,但当我在Twitter上写任何消息进行更新时,我的消息没有显示在我的Twitter帐户上。我做错了什么?我按照本教程here,但我的信息没有显示。