问题
I have a runtime error when i try to run my android application, due to the importation of this package that allows me to use Watson Services.
'com.ibm.watson.developer_cloud:java-sdk:2.9.0'
In fact, when I use something related to this package, the error that appears is:
E/dalvikvm: Could not find class 'javax.naming.InitialContext', referenced from method com.ibm.watson.developer_cloud.util.CredentialUtils.getKeyUsingJNDI
and the corresponding stacktrace is:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: semantic.myapplication, PID: 29120
java.lang.VerifyError: com/ibm/watson/developer_cloud/util/CredentialUtils
at….
I’ve looked for the real problem of this error and I’ve found that in the file “CredentialUtils.java” where the error appears, these three importations are not recognized (Cannot resolve symbols):
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
I use Android Studio and the latest version of jdk (1.8.0.77). These java files exist, I can see them but for some reason are not accessible: the importation of every file in javax.naming (and also other folders) is not possible.
How can I solve this problem? I’ve tried to import a .jar file including the javax.naming classes, but doesn’t work. Is there a way to make visible that classes?
Any help would be appreciated. Thanks!
回答1:
I think you are using a version older than 2.7.1
. I'm unable to reproduce your problem and I tried with different configurations including yours.
Make sure you clean your workspace and get the latest version of the library.
In your build.gradle
you should have:
compile 'com.ibm.watson.developer_cloud:java-sdk:2.9.0'
Here is the commit that fixed your issue. The fix was released in the 2.7.1
version.
This is what I did and that worked for me:
Steps to use the Watson APIs in Android you should:
- Download and install Android Studio
- Open Android Studio
File > New > New Project
.- Pick a name and click
next
,next
,next
andFinish
In
<project-name>/app/build.gradle
add:compile 'com.ibm.watson.developer_cloud:java-sdk:2.9.0'
In
MainActiviy.java
at the end ofonCreate(...)
add:Thread thread = new Thread(new Runnable(){ @Override public void run() { try { SpeechToText service = new SpeechToText(); service.setUsernameAndPassword("<username>", "<password>"); System.out.println(service.getModels()); } catch (Exception e) { e.printStackTrace(); } } }); thread.start();
Start the simulator and look at the console. You should see a message like
I/CredentialUtils: JNDI string lookups is not available.
and the Speech to Text model list.
I/System.out: [{ I/System.out: "name": "en-US_NarrowbandModel", I/System.out: "rate": 8000 I/System.out: }, { I/System.out: "name": "pt-BR_BroadbandModel", I/System.out: "rate": 16000 I/System.out: }, { I/System.out: .... more models here I/System.out: }, { I/System.out: "name": "en-US_BroadbandModel", I/System.out: "rate": 16000 I/System.out: }]
来源:https://stackoverflow.com/questions/36217233/javax-content-not-accessible