Posting to twitter from Android (commons httpclient 4+)

While developing a helper for the Twitter API to be used in the Android application which I’m writing I ran into a nasty problem. While trying to post to Twitter Twitter kept sending “417 – Expectation Failed” responses. I solved this by specifically setting the UseExpectContinue flag to false.

The following snippets are part of my helper class to talk to twitter, it runs fine on my ADP1 using Android 1+ :

[java]
private static final DefaultHttpClient httpclient = new DefaultHttpClient();
private static final AuthScope AUTH_SCOPE = new AuthScope(“twitter.com”, 80, AuthScope.ANY_REALM);

private static JSONObject post(final String url, final Map fields, final UsernamePasswordCredentials credentials) throws Exception {
final HttpParams params = createParamsForPosting();

// Tell twitter who we are
httpclient.getCredentialsProvider().setCredentials(AUTH_SCOPE, credentials);
httpclient.setParams(params);

final HttpPost post = new HttpPost(url);

// Setup the form data to post
final UrlEncodedFormEntity requestEntity = mapToFormEntity(fields);
post.setEntity(requestEntity);

final HttpResponse response = httpclient.execute(post);
final HttpEntity responseEntity = response.getEntity();
final String data = IOUtils.convertStreamToString(responseEntity.getContent());

return new JSONObject(data);
}

private static HttpParams createParamsForPosting() {
final HttpParams params = new BasicHttpParams();
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params, UTF_8);
HttpProtocolParams.setUseExpectContinue(params, false); // solves the ’417′ issue
return params;
}

private static UrlEncodedFormEntity mapToFormEntity(final Map fields) throws UnsupportedEncodingException {
final ArrayList values = new ArrayList(fields.size());
for (final Entry entry : fields.entrySet()) {
values.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}

final UrlEncodedFormEntity entity = new UrlEncodedFormEntity(values, UTF_8);
return entity;
}
[/java]

When complete I’ll probably opensource the twitter API wrapper in full, at the moment it is not complete enough to do so.

This entry was posted in android. Bookmark the permalink.

6 Responses to Posting to twitter from Android (commons httpclient 4+)

  1. toppest says:

    Google leads me to here. It seems that you are doing some cool stuff for people who want to play with Android and
    twitter. I believe it will be a great work! Good luck!!

  2. peter says:

    @toppest: thanks! you might want to have a peek at http://www.tw33t0r.com I have some screenshots up of the client running!

  3. Illidane says:

    What do I need to put into “post” method?

  4. Jamsey says:

    I’ve just tried clicking on the link http://www.tw33t0r.com and get a Squirrel Mail login.

  5. @k15 says:

    hey!

    thanks for the code
    but i wanted to run a simple twitter api query in android
    and i have no idea how to start.pl help!

  6. peter says:

    Sorry; completely stopped development on that.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>