0

我想爲我的android應用程序使用EWS-Java-API。 應用程序需要登錄才能以編程方式訪問Microsoft Exchange Server(例如日曆會議,電子郵件,會議室等)。EWS-Java-API - EmailMessage.send進入java.lang.NullPointerException:format == null

此時幾乎整個問題都被編輯了,以防萬一以顯示目前的情況。

我試圖讓連接到Exchange版本15.0.11與URL( 「HTTPS://yourserver/EWS/Exchange.asmx」)

的gradle構建文件:

android { 
compileSdkVersion 23 
buildToolsVersion "23.0.1" 
useLibrary 'org.apache.http.legacy' 

defaultConfig {   
    minSdkVersion 18 
    targetSdkVersion 22 
    versionCode 1 
    versionName "1.0" 
    multiDexEnabled true 
} 

packagingOptions {   
    exclude 'META-INF/DEPENDENCIES.txt' 
    exclude 'META-INF/DEPENDENCIES' 
    exclude 'META-INF/dependencies.txt' 
    exclude 'META-INF/LICENSE.txt' 
    exclude 'META-INF/LICENSE' 
    exclude 'META-INF/license.txt' 
    exclude 'META-INF/LGPL2.1' 
    exclude 'META-INF/NOTICE.txt' 
    exclude 'META-INF/NOTICE' 
    exclude 'META-INF/notice.txt' 
} [...] } 

dependencies { 
    compile fileTree(include: ['*.jar'], dir: 'libs') 
    testCompile 'junit:junit:4.12' 
    compile 'com.android.support:appcompat-v7:23.2.1' 
    compile 'com.android.support:design:23.3.0' 
    compile 'com.google.android.gms:play-services:6.5.87' 
    compile 'org.altbeacon:android-beacon-library:2.7.1' 
    compile 'org.greenrobot:eventbus:3.0.0' 
    compile files('libs/jcifs-1.3.15.jar') 
    compile files('libs/commons-logging-1.1.1.jar') 
    compile files('libs/commons-codec-1.4.jar') 
    compile files('libs/commons-httpclient-3.1.jar') 
    compile 'com.microsoft.ews-java-api:ews-java-api:2.0' } 

這會導致這個錯誤信息

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

線:

ExchangeService service = new ExchangeService();  

此時我測試了這個ews庫「github.com/faisons/EWS-For-Android」。

​​3210

這幾乎工作。 Iam能夠啓動應用程序並設置ExchangeService。現在

Thread thread = new Thread(new Runnable() 
    { 
     @Override 
     public void run() 
     { 
      try 
      { 
       ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP2); 
       ExchangeCredentials credentials = new WebCredentials(myuser, mypw); 
       service.setCredentials(credentials); 
       service.setUrl(new URI("https://exchmail.yourserver.com/EWS/Exchange.asmx")); 
       EmailMessage message = new EmailMessage(service); 
       EmailAddress from = new EmailAddress(myemail); 
       message.setSender(from);     
       message.getToRecipients().add(anotherEmail); 
       message.setSubject("Much wow - such message"); 
       MessageBody messageBody = new MessageBody(); 
       messageBody.setText("text here"); 
       message.setBody(messageBody); 
       message.send(); 

      } 
      catch (Exception e) 
      { 
       Log.e(TAG, "message", e); 
      } 
     } 
    }); 

    thread.start(); 

的錯誤是:

java.lang.NullPointerException: format == null 
    at java.lang.String.format(String.java:1799) 
at java.lang.String.format(String.java:1777) 
    at java.lang.String.format(String.java:1799) 
    at microsoft.exchange.webservices.data.ServiceRequestBase.getEwsHttpWebResponse(ServiceRequestBase.java:936) 
at microsoft.exchange.webservices.data.ServiceRequestBase.validateAndEmitRequest(ServiceRequestBase.java:821) 
at microsoft.exchange.webservices.data.SimpleServiceRequestBase.internalExecute(SimpleServiceRequestBase.java:46) 
at microsoft.exchange.webservices.data.MultiResponseServiceRequest.execute(MultiResponseServiceRequest.java:143) 
at microsoft.exchange.webservices.data.ExchangeService.internalCreateItems(ExchangeService.java:463) 
at microsoft.exchange.webservices.data.ExchangeService.createItem(ExchangeService.java:534) 
at microsoft.exchange.webservices.data.Item.internalCreate(Item.java:215) 
at microsoft.exchange.webservices.data.EmailMessage.internalSend(EmailMessage.java:125) 

蔭困惑如何獲得API的使用。如果有人知道另一種方式獲得訪問任何提示是值得歡迎的!

回答

2

問候!我有好消息,它有效!蔭不知道這是一個完美的方式,但我能夠發送電子郵件

首先確保你在manifes文件中聲明的權限:

<uses-permission android:name="android.permission.INTERNET"/> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> 

這是我的構建。 gradle這個文件:

android { 
compileSdkVersion 23 
buildToolsVersion "23.0.1" 
useLibrary 'org.apache.http.legacy'  // <--------------------- 

defaultConfig { 
    applicationId "your application id" 
    minSdkVersion 18 
    targetSdkVersion 22 
    versionCode 1 
    versionName "1.0" 
    multiDexEnabled true 
} 

packagingOptions { 
    exclude 'META-INF/LICENSE.txt' 
    exclude 'META-INF/NOTICE.txt' 
    exclude 'META-INF/DEPENDENCIES.txt' 
    exclude 'META-INF/DEPENDENCIES' 
    exclude 'META-INF/dependencies.txt' 
    exclude 'META-INF/LICENSE.txt' 
    exclude 'META-INF/LICENSE' 
    exclude 'META-INF/license.txt' 
    exclude 'META-INF/LGPL2.1' 
    exclude 'META-INF/NOTICE.txt' 
    exclude 'META-INF/NOTICE' 
    exclude 'META-INF/notice.txt' 
} 

buildTypes { 
    release { 
     minifyEnabled false 
     proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
    } 
}} 

dependencies { 
compile fileTree(include: ['*.jar'], dir: 'libs') 
testCompile 'junit:junit:4.12' 
compile 'com.android.support:appcompat-v7:23.2.1' 
compile 'com.android.support:design:23.3.0 
compile files('libs/ews-android-api.jar') // <--------------------- 
compile 'joda-time:joda-time:2.8'}   // <--------------------- 

您可以將此鏈接下得到EWS-Android的API: https://github.com/alipov/ews-android-api

只要按照建築instructi ons來創建jar文件。

這是發送電子郵件的基本命令行(運行此作爲異步任務或在一個新的線程):

   ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP2); 
       ExchangeCredentials credentials = new WebCredentials("user", "pw"); 
       service.setCredentials(credentials); 
       service.setUrl(new URI("https:// s e r v e r /ews/exchange.asmx"));      
       EmailAddress mEmail = new EmailAddress("your email/test email"); 

       EmailMessage message = new EmailMessage(service); 
       message.getToRecipients().add(mEmail); 
       message.setFrom(mEmail); 
       message.setSubject("Hello world!"); 
       message.setBody(MessageBody.getMessageBodyFromText("Sent using the EWS Android API.")); 
       message.send(); 

您可以用EWSEditor檢查您的憑證。 大聲喊出DevilGeek和Alipov。

如果有什麼不清楚的地方請隨時詢問。

親切的問候!編輯: 它似乎與幾乎所有的工作。特別是您不需要Office 365。例如,iam能夠調用「service.getRoomLists();」以及。

+0

我得到了」java .lang.NoClassDefFoundError:無法解析:Ljava/applet/Applet「,當我調用AutodiscoverService.getUserSettings時,已經在https://github.com/alipov/ews-android-api/issues/3上有記錄。你是如何解決這個問題的? – NotAgain

0

您是否將apache遺留行添加到您的gradle構建中?

android { 
    compileSdkVersion 22 
    buildToolsVersion "22.0.1" 
    useLibrary 'org.apache.http.legacy' 
    ... 
} 

更多信息請參閱本:http://developer.android.com/about/versions/marshmallow/android-6.0-changes.html#behavior-apache-http-client

+0

感謝您的快速答案Benoit! 我添加了這個,並得到了警告: 警告:無法找到可選庫:org.apache.http.legacy – Lugo

+0

當我嘗試運行該應用程序時,我得到: 警告:依賴項commons-logging:commons-logging:調試時忽略1.2,因爲它可能與Android提供的內部版本衝突。如果出現問題,請用jarjar重新打包以更改類包 錯誤:配置項目':app'時發生問題。 >無法找到可選庫:org.apache.http.legacy – Lugo

+0

我認爲你應該刪除你的依賴'編譯文件('libs/org.apache.http.legacy.jar')'因爲遺留將被導入SDK。 至於你的錯誤,你應該看看那篇文章:http://stackoverflow.com/questions/33898857/why-warningunable-to-find-optional-library-org-apache-http-legacy-occurs它似乎您的SDK安裝不包含可選項。重新安裝它可能會解決問題。 – Benoit