2

I'm having some issues trying to use exchange webservices for java. I'm getting following NoSuchMethodError.

java.lang.NoClassDefFoundError: Failed resolution of: Lorg/apache/http/client/protocol/HttpClientContext;

I've tried reinstalling ews-java-api several times but it hasnt been solving the problem as i believed it would.

I'm currently working in Android Studio.

Full stack:

03-25 15:37:23.551    1875-1875/com.example.XXXXX.test E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.XXXXXX.test, PID: 1875
java.lang.NoClassDefFoundError: Failed resolution of: Lorg/apache/http/client/protocol/HttpClientContext;
        at microsoft.exchange.webservices.data.ExchangeServiceBase.initializeHttpContext(ExchangeServiceBase.java:211)
        at microsoft.exchange.webservices.data.ExchangeServiceBase.setUseDefaultCredentials(ExchangeServiceBase.java:628)
        at microsoft.exchange.webservices.data.ExchangeServiceBase.<init>(ExchangeServiceBase.java:158)
        at microsoft.exchange.webservices.data.ExchangeServiceBase.<init>(ExchangeServiceBase.java:164)
        at microsoft.exchange.webservices.data.ExchangeService.<init>(ExchangeService.java:3663)
        at com.example.XXXXXX.test.MainActivity.onCreate(MainActivity.java:25)
        at android.app.Activity.performCreate(Activity.java:5933)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
        at android.app.ActivityThread.access$800(ActivityThread.java:144)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5221)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
 Caused by: java.lang.ClassNotFoundException: Didn't find class "org.apache.http.client.protocol.HttpClientContext" on path: DexPathList[[zip file "/data/app/com.example.XXXXXXX.test-1/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
at      microsoft.exchange.webservices.data.ExchangeServiceBase.initializeHttpContext(ExchangeServiceBase.java:211)
 at microsoft.exchange.webservices.data.ExchangeServiceBase.setUseDefaultCredentials(ExchangeServiceBase.java:628)
at microsoft.exchange.webservices.data.ExchangeServiceBase.<init>(ExchangeServiceBase.java:158)
at microsoft.exchange.webservices.data.ExchangeServiceBase.<init>(ExchangeServiceBase.java:164)
at microsoft.exchange.webservices.data.ExchangeService.<init>(ExchangeService.java:3663)
at com.example.XXXXXX.test.MainActivity.onCreate(MainActivity.java:25)
at android.app.Activity.performCreate(Activity.java:5933)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Suppressed: java.lang.ClassNotFoundException: org.apache.http.client.protocol.HttpClientContext
        at java.lang.Class.classForName(Native Method)
        at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
        at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
        ... 20 more
 Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available

These are the dependencies in build.gradle:

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:19.0.0'
//compile 'org.apache.httpcomponents:httpclient:4.4'
//compile 'org.apache.httpcomponents:httpcore:4.3.3'
//compile 'commons-logging:commons-logging:1.2'
//compile 'joda-time:joda-time:2.7'
}

And here's the onCreate I'm currently testing:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ExchangeService service = new ExchangeService(); // this is where the error is hitting
    ExchangeCredentials credentials = new WebCredentials("user", "pw");
    service.setCredentials(credentials);

    try {
        service.setUrl(new URI("uri"));
    } catch (URISyntaxException e) {
        e.printStackTrace();
    }

    EmailMessage msg= null;
    try {
        msg = new EmailMessage(service);
        msg.setSubject("Hello world!");
        msg.setBody(MessageBody.getMessageBodyFromText("Sent using the EWS Java API."));
        msg.getToRecipients().add("email");
        msg.send();
    } catch (Exception e) {
        e.printStackTrace();
    }


}
mathletics
  • 271
  • 1
  • 4
  • 15
  • I don't think it's because of `ews-java-api`, have you tried to check on Apache Http Client ? – osayilgan Mar 25 '15 at 19:57
  • @osayilgan added it. But it's still not working. – mathletics Mar 27 '15 at 11:07
  • @adamNature, Did you manage to get a solution to this? I'm getting with the same error. – Tinashe Jun 02 '15 at 13:33
  • @Tinashe, Nope, sorry! I ditched this method and am now using the O365 SDK for Android instead. As long as the app you are creating has the correct permissions in the Azure Active Directory, I was able to do the same thing I was attempting here. – mathletics Jun 02 '15 at 15:51
  • Adding compile group: 'org.apache.httpcomponents' , name: 'httpclient-android' , version: '4.3.5.1' to the build.gradle will solve this but then you get to the next error NoClassDefFoundError: Failed resolution of: Lorg/apache/http/conn/ssl/DefaultHostnameVerifier; very Annoying – Tinashe Jun 03 '15 at 08:07
  • @Tinashe,didnt attempt this. Hopefully it works for you! – mathletics Jun 03 '15 at 08:08

2 Answers2

0

Try adding

compile 'org.apache.httpcomponents:httpclient:4.4'

in your build.gradle file

ksarmalkar
  • 1,884
  • 1
  • 16
  • 21
0

Download HttpClientContext jar, then include it in classpath and then give it a try :)

Abhishek
  • 6,912
  • 14
  • 59
  • 85
  • Downloaded the .jar and added to my /libs/-folder. Now i'm getting `java.lang.NoSuchFieldError: No static field INSTANCE of type Lorg/apache/http/message/BasicLineFormatter; in class Lorg/apache/http/message/BasicLineFormatter; or its superclasses (declaration of 'org.apache.http.message.BasicLineFormatter' appears in /system/framework/ext.jar)` Maybe i installed it wrong? – mathletics Mar 27 '15 at 10:54
  • I guess you have got wrong jar or classpath has to be rearranged correctly. Try resolution at http://stackoverflow.com/questions/21864521/java-lang-nosuchfielderror-org-apache-http-message-basiclineformatter-instance for BasicLineFormatter :) Hope it helps. – Abhishek Mar 28 '15 at 04:44