microsoft / Cognitive-SpeakerRecognition-Android

Android SDK for Microsoft Speaker Recognition API, part of Cognitive Services
https://www.microsoft.com/cognitive-services/en-us/speaker-recognition-api
Other
46 stars 34 forks source link

Cannot retrieve verification phrases #1

Closed cgero-eth closed 8 years ago

cgero-eth commented 8 years ago

I started to test this library for android adding it as dependency in the build.gradle file but I get an error on the getPhrases function.

Here is my code:

private void retrieveSupportedPhrases() {
        SpeakerVerificationRestClient restClient = new SpeakerVerificationRestClient(MY_KEY);
        try {
            List<VerificationPhrase> phrases = restClient.getPhrases("en-US");
            for (int i = 0; i < phrases.size(); i++) {
                Log.i(TAG, phrases.get(i).toString());
            }
        } catch (PhrasesException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
    }

Here is the error:

FATAL EXCEPTION: main
Process: com.mobile.cir.voicerecognition, PID: 1709
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mobile.cir.voicerecognition/com.mobile.cir.voicerecognition.activity.EnableVoiceRecognition}: android.os.NetworkOnMainThreadException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1273)
at java.net.InetAddress.lookupHostByName(InetAddress.java:431)
at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
at java.net.InetAddress.getAllByName(InetAddress.java:215)
at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:142)
at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:169)
at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:124)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:366)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:560)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:492)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:470)
at com.microsoft.cognitive.speakerrecognition.SpeakerVerificationRestClient.getPhrases(SpeakerVerificationRestClient.java:265)
at com.mobile.cir.voicerecognition.activity.EnableVoiceRecognition.retrieveSupportedPhrases(EnableVoiceRecognition.java:85)
at com.mobile.cir.voicerecognition.activity.EnableVoiceRecognition.onCreate(EnableVoiceRecognition.java:77)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
at android.app.ActivityThread.-wrap11(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
momohs commented 8 years ago

Hello @RuggeroCino! By looking at your error log, I can see that the exception is a NetworkOnMainThreadException.

Caused by: android.os.NetworkOnMainThreadException

Android does not allow a user to perform network operations from the main thread. Therefore, you have to call the Speaker Recognition SDK asynchronously. One of the ways to do so is using the AsyncTask. You can wrap the getPhrases call in an AsyncTask to avoid this error.

cgero-eth commented 8 years ago

I tried to use the AsyncTask and it works, thank you.

momohs commented 8 years ago

Great! Thanks for the update @RuggeroCino! I'll close this issue for now, please feel free to re-open if you have any further questions.