Problems using the EWS Java API on Android

Raul Agrait picture Raul Agrait · Feb 9, 2012 · Viewed 7.3k times · Source

I am trying to use the EWS Java API v1.1.5 (http://archive.msdn.microsoft.com/ewsjavaapi) in an Android application, and have run into a number of issues. I downloaded the source, and followed the instructions provided to compile the EWS Java API in Eclipse. In those instructions you are told to download and add the following pre-requiste jar file dependencies:

  • commons-codec-1.4.jar
  • commons-httpclient-3.1.jar
  • commons-logging-1.1.1.jar
  • jcifs-1.3.15.jar

I did this, and followed the build instructions with produced the following jar files:

  • EWSAPI-1.1.0.jar
  • EWSAPIWithJars-1.1.0

Next, I built a brand new Android application, added the appropriate permissions to the manifest, and then added the following source to the primary activity's OnCreate:

ExchangeService service = new ExchangeService();
ExchangeCredentials credentials = new WebCredentials("emailaddress", "password");       
service.setCredentials(credentials);

try
{
    service.autodiscoverUrl("emailaddress", this);
}
catch (Exception e)
{       
    e.printStackTrace();
}

I first tried running this application with the EWSAPI-1.1.0.jar file as a dependency. When I did that, I obtained the following fatal error:

Uncaught handler: thread main exiting due to uncaught exception java.lang.NoClassDefFoundError: org.apache.commons.httpclient.MultiThreadedHttpConnectionManager at microsoft.exchange.webservices.data.ExchangeServiceBase.(Unknown Source) at microsoft.exchange.webservices.data.ExchangeServiceBase.(Unknown Source) at microsoft.exchange.webservices.data.ExchangeService.(Unknown Source) at com.meshin.exchange.ExchangeDiscoveryActivity.onCreate(ExchangeDiscoveryActivity.java:40)

From what I've researched, it seems like this error is being generated because Android comes with the Apache HttpClient 4.0, which doesn't appear to have the MultiThreadedHttpConnectionManager class anymore.

If I instead remove the EWSAPI-1.1.0.jar, and instead use the EWSAPIWithJARS-1.1.0.jar file as a dependency, I get the following error:

VFY: unable to resolve static method 908: Ljavax/xml/stream/XMLOutputFactory; newInstance()Ljavax/xml/stream/XMLOutputFactory; VFY: dead code 0x0008-006a in L microsoft/exchange/webservices/data/EwsUtilities;.formatLogMessage (Ljava/lang/String;Ljava/lang/String;) Ljava/lang/String; VFY: unable to find class referenced in signature (Ljavax/xml/stream/XMLStreamWriter;)

And then eventually...

FATAL EXCEPTION: main java.lang.VerifyError: microsoft.exchange.webservices.data.AutodiscoverService at microsoft.exchange.webservices.data.ExchangeService.getAutodiscoverUrl(Unknown Source) at microsoft.exchange.webservices.data.ExchangeService.autodiscoverUrl(Unknown Source) at com.meshin.exchange.ExchangeDiscoveryActivity.onCreate(ExchangeDiscoveryActivity.java:41)

I am assuming because now I am including the HttpClient 3.1 jar and it is conflicting with the HttpClient 4.0 jar included with the Android libraries.

My question is if there is a way for me to use the EWS Java API in an Android project without having to re-write the parts of it which reference HttpClient 3.1-specific things which are no longer in 4.0.

Answer

user1106480 picture user1106480 · Sep 12, 2012

You can use microsoft's EWS api for android by doing the following steps,

  1. download the source code available in the URL, http://archive.msdn.microsoft.com/ewsjavaapi EWSJavaAPI_1.1.5.zip

  2. Make the changes to above api to work for JDK 1.4 in eclipse like remove override annotations e.t.c

  3. Download source code of javax.* package available in below URL, http://www.java2s.com/Code/JarDownload/jsr173/jsr173_1.0_src.jar.zip

  4. Download source code of stax api available in below URL, http://dist.codehaus.org/stax/distributions/stax-src-1.2.0.zip

  5. Keep all the sources under the single java project in eclipse

  6. Open the project explorer and select the package which are starts with "javax" and rename to your company name eg: com. Note: Eclipse will ask for all the naming contexts will change then click OK.

  7. Export all the java sources to one single jar file.

  8. Then You good to go to use the jar in Android application with out any problems.

I used the same way explained above and it worked in android application 100% perfectly.