Fixing the 417 From Twitter in Java HTTP Components
December 30th, 2008
Count me among the many who got bitten by the 417 response bug from Twitter over the holiday break. What a fabulous present, thanks.
Anyway, all the information on the net seems to cover fixing this problem ASP.NET applications, which isn’t a lot of help for my Android application using Apache HTTP Components.
Here’s how you fix the problem in your org.apache.http based Java application:
mClient.setParams(new BasicHttpParams().setBooleanParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, false));
Where mClient is your DefaultHttpClient.
Displaying Images From the Web in Android Using HTTPComponents
December 15th, 2008
There are lots of places around the web that have good examples for displaying images from the web in Android using java.net. However, the app I’m working on at the moment uses Apache’s HTTPComponents for communicating with the interwebs and I wanted to be consistent in using that throughout. So, in the spirit of giving, here’s how I’m displaying images using HTTPCompoents:
- Execute an HTTPGet for the image URL.
- Get the entity from the HTTPResponse, convert it to an InputStream.
- Run the InputStream through BitmapFactory.decodeStream().
Voila! You have a Bitmap that you can display using an ImageView.
// Initialize me how you like
private DefaultHttpClient mClient;
/**
* @param url the url of the image to attempt to download
* @return the Bitmap of the url, null if anything goes wrong
*/
public Bitmap getImageForUrl(String url) {
Bitmap b = null;
HttpGet getter = new HttpGet(url);
try {
HttpResponse r = mClient.execute(getter);
b = BitmapFactory.decodeStream(r.getEntity().getContent());
} catch (Throwable e){
e.printStackTrace();
}
return b;
}
Quick and easy.
Twitter API Change
December 13th, 2008
It was very helpful of the Twitter folks to change their API so that the verify_credentials methods gives a representation of the user on success.
It was not so helpful to stop returning “authorized”:true in the response because now my login method doesn’t work.
That will teach me to use the response code rather than the payload to verify login.