0

I'm working on alchemy API. The Reference link used Alchemy Ref.

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();

The program terminates during the initialization of the class

 AlchemyLanguage service = new AlchemyLanguage();   

Log :

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.
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
SH7
  • 732
  • 7
  • 20
  • That error seems to hint that your API Key in incorrect – OneCricketeer Oct 15 '16 at 20:04
  • I ref'd this link [other ref](http://www.ibm.com/watson/developercloud/doc/alchemylanguage/tutorials.shtml#curlTutorial-keywords) and i could get results. and also the code doesn't run at `service.setApiKey("xxx");` @cricket_007 – SH7 Oct 15 '16 at 20:12
  • Did you replace `xxx` with your own key? – OneCricketeer Oct 15 '16 at 20:14
  • @cricket_007 - of course i used my actual api key just posted 'xxx' in stack overflow. – SH7 Oct 15 '16 at 20:19
  • Just curious :) Don't really know what to tell you, though. Does this code work in a standard Java app? Making a plain `Thread` object isn't really Android's way of doing things – OneCricketeer Oct 15 '16 at 20:22
  • actually i tried **doInBackground** thread even that returns error. Just got a video link [youtube ref](https://www.youtube.com/watch?v=oiwhs1H9JVg) so followed that steps. – SH7 Oct 15 '16 at 20:25

1 Answers1

0

This was a bug in the Java SDK. Make sure you use a version > 3.5.0 in gradle, use something like 3.7.1

'com.ibm.watson.developer_cloud:alchemy:3.7.1'

Then call AlchemyLanguage sentiment analysis:

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);
German Attanasio
  • 22,217
  • 7
  • 47
  • 63