Sunday, August 30, 2009

Using Apache HttpClient 4 with Google App Engine

The ESXX blog has some handy code for integrating Apache HttpClient 4 with Google App Engine. Since it includes working source code, everything you need to figure it out is there. However, it can be a little confusing at first if you try dropping the code directly into an App Engine project. Your project will fail to compile as it uses several functions that are not supported by the App Engine SDK.

Instead of putting the problematic source code directly in your project, put them in a separate jar file. Then you can include that jar file in your project and everything should work fine.

I did this by creating a simple java project in eclipse, with just the two adapter code files. To get this to build, I also included the apache httpclient jars and the app engine sdk jar. I've included an image of my simple project structure here. Eclipse should automatically build the files once those jars are in place.

Then, at the command line, I simply went to that project's build directory and manually built the jar file using "jar cf GAEHttpClient.jar *". You can of course use your own jar file name. Make sure you build the jar at the root of your build directory structure.

Once your jar file is built, you should be able to add it to your main App Engine project (right click on the project in your Package Explorer and go to Build Path-> Configure Build Path-> Add external JARs . . . then add it to your war/WEB-INF/lib directory using File->Import->General->File System . . .) Once you have that (and the Apache httpclient jars) in place, you should be able to compile your code without error:

HttpParams httpParams = new BasicHttpParams();
ClientConnectionManager connectionManager = new GAEConnectionManager();
HttpClient httpClient = new DefaultHttpClient(connectionManager, httpParams);

4 comments:

Thorsten said...

Thank you for this information, this helps quite much!

Thorsten

stephane said...

It's a very very very useful POST. Do you have any Idea of why it works that way ?

KnowledgeServe said...

Good work

Michael said...

Can you just post the .jar you created, saving some of us the work.

Post a Comment