1
votes

Here I am posting some text from an Android app to a user's Twitter account, and I have ConsumerKey, Consumer secret key, AccessToken, and AccessToken secret key.

https://api.twitter.com/1.1/statuses/update.json

By using simple a HTTP client request, I want to post data on a Twitter user account. How can I do that?

I tried something like

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://api.twitter.com/1/statuses/update.json");

// HttpPost httppost = new HttpPost("https://graph.facebook.com/" + profile_ID + "/feed");

System.out.println("httppost " + httppost);

try {
    // Add your data
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
    nameValuePairs.add(new BasicNameValuePair("access_token", access_token_value));
    nameValuePairs.add(new BasicNameValuePair("message", compose.getText().toString()));
    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

    // Execute HTTP Post Request
    HttpResponse response = httpclient.execute(httppost);

    System.out.println("response "+response.toString());

}
catch (ClientProtocolException e) {
    // TODO Auto-generated catch block
}
catch (IOException e) {
    // TODO Auto-generated catch block
}

But parameters are wrong in my code, following this, but I didn't get it so what is some suitable solution for this?

2
Please check the answer below, thanks. - Salman Khakwani
Yes ,it is asking for Login .But i want to post app data with out login - user2843350
@SalmanAyub twitter4j is first choice for anyone ,who is working with twitter according to my requirement without login i want to tweet from my app and above info i have ,i didn't get by using above info how to post - user2843350

2 Answers

1
votes

If you want to post what you have entered in text box, I may help you out here. 1. You must have included the twitter4j library in gradle file as follows:

    compile group: 'org.twitter4j', name: 'twitter4j-core', version: '4.0.1'

2. Include following code snippet in your code:

public String sTweet;
EditText et_TweetText;
public static String consumerkey = "xxxxxxxxxxxxxxxxx";
public static String consumersecret = "xxxxxxxxxxxxxxxxx";
public static String accesstoken="xxxxxxxxxxxxxxxxx-xxxxxxxxxxxxxxxxx";
public static String accesstokensecret="xxxxxxxxxxxxxxxxx";

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    et_TweetText = (EditText) view.findViewById(R.id.et_TweetText);

    bt_tweetIt = (Button) view.findViewById(R.id.bt_tweetIt);
    bt_tweetIt.setOnClickListener(this);
}

@Override
public void onClick(View v) {
sTweet = et_TweetText.getText().toString();
Log.i(TAGTweet, "onClick- sTweet: " + sTweet);

if(CheckConnectivity()!=0){
    Log.i(TAGTweet, "Internet Connection ON: ");
    new UseTweeter().execute();
}
else {
    Log.i(TAGTweet, "Internet Connection OFF: ");
    ShowNoInternetConnectionAlert();
}

}

 public class UseTweeter extends AsyncTask<String,String,String>{
     @Override
protected String doInBackground(String... params) {
     TweetThisMessage();
    return null;
}

}

public void TweetThisMessage(){
ConfigurationBuilder builder = new ConfigurationBuilder();
builder.setOAuthAccessToken(accesstoken);
builder.setOAuthAccessTokenSecret(accesstokensecret);
builder.setOAuthConsumerKey(consumerkey);
builder.setOAuthConsumerSecret(consumersecret);
OAuthAuthorization auth = new OAuthAuthorization(builder.build());
Twitter twitter = new TwitterFactory().getInstance(auth);

try {
    Log.i(TAGTweet, "sTweet in TweetThisMessage(): " + sTweet);
    twitter.updateStatus(sTweet);
} catch (TwitterException e) {
    e.printStackTrace();
    return;
}

}

Hope it helps.

0
votes

Since you are asking for How to tweet from an Android app using REST API 1.1, I would recommend you to use the Twitter 4j library, for integrating Twitter in your Android application.

This library encapsulates the authentication functionality (via an OAuth library) and other important functionalities within itself. Here are some of its features:

Twitter4J is featuring:

  • 100% pure Java - works on any Java Platform version 5 or later
  • Android platform and Google App Engine-ready
  • Zero dependency: No additional JAR files required
  • Built-in OAuth support
  • Out-of-the-box gzip support
  • 100% Twitter API 1.1 compatible

You need to call simple functions via this Library to use the specific functionality regarding Twitter. Here is a link that demonstrates the usage of this library: http://twitter4j.org/en/code-examples.html

You can download this library on the link http://twitter4j.org/.

Here is a sample implemented with the library https://github.com/itog/Twitter4j-android-Sample.