我正在制作炼金术API。参考链接使用炼金术参考
My Android code
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try
{
AlchemyLanguage service = new AlchemyLanguage();
service.setApiKey("xxx - My API Key");
Map<String,Object> param = new HashMap<String, Object>();
param.put(AlchemyLanguage.TEXT, TextValue);
DocumentSentiment sentiment = service.getSentiment(param).execute();
SentimentValue = sentiment.getText();
System.out.println(sentiment);
SentimentType = sentiment.getSentiment().getType().name();
}
catch (Exception ex)
{
Log.d("Exception : " , ex.toString());
}
}
});
thread.start();
Toast.makeText(MainActivity.this, SentimentType+" ", Toast.LENGTH_SHORT).show();
程序在类
初始化期间终止。 AlchemyLanguage service = new AlchemyLanguage();
日志:E/dalvikvm: Could not find class 'javax.naming.InitialContext', referenced from method com.ibm.watson.developer_cloud.util.CredentialUtils.getKeyUsingJNDI
W/dalvikvm: VFY: unable to resolve new-instance 2848 (Ljavax/naming/InitialContext;) in Lcom/ibm/watson/developer_cloud/util/CredentialUtils;
D/dalvikvm: VFY: replacing opcode 0x22 at 0x0007
W/dalvikvm: VFY: unable to resolve exception class 2849 (Ljavax/naming/NamingException;)
W/dalvikvm: VFY: unable to find exception handler at addr 0x2e
W/dalvikvm: VFY: rejected Lcom/ibm/watson/developer_cloud/util/CredentialUtils;.getKeyUsingJNDI (Ljava/lang/String;)Ljava/lang/String;
W/dalvikvm: VFY: rejecting opcode 0x0d at 0x002e
W/dalvikvm: VFY: rejected Lcom/ibm/watson/developer_cloud/util/CredentialUtils;.getKeyUsingJNDI (Ljava/lang/String;)Ljava/lang/String;
W/dalvikvm: Verifier rejected class Lcom/ibm/watson/developer_cloud/util/CredentialUtils;
W/dalvikvm: threadid=12: thread exiting with uncaught exception (group=0x40d9e468)
E/AndroidRuntime: FATAL EXCEPTION: Thread-754
java.lang.VerifyError: com/ibm/watson/developer_cloud/util/CredentialUtils
at com.ibm.watson.developer_cloud.service.WatsonService.<init>(WatsonService.java:102)
at com.ibm.watson.developer_cloud.service.AlchemyService.<init>(AlchemyService.java:55)
at com.ibm.watson.developer_cloud.alchemy.v1.AlchemyLanguage.<init>(AlchemyLanguage.java:54)
at com.numarga.alchemyinbuildtest.MainActivity$1$1.run(MainActivity.java:53)
at java.lang.Thread.run(Thread.java:856)
I/Process: Sending signal. PID: 23131 SIG: 9
Application terminated.
这是Java SDK中的一个bug。确保你在gradle中使用的是> 3.5.0的版本,比如3.7.1
'com.ibm.watson.developer_cloud:alchemy:3.7.1'
然后调用AlchemyLanguage情感分析:
AlchemyLanguage service = new AlchemyLanguage();
service.setApiKey("xxx - My API Key");
Map<String,Object> param = new HashMap<String, Object>();
param.put(AlchemyLanguage.TEXT, TextValue);
DocumentSentiment sentiment = service.getSentiment(param).execute();
System.out.println(sentiment);