Android Studio中的AWS COMPREHEND DetectSentimentResult方法错误

时间:2019-04-04 10:25:12

标签: android amazon-web-services amazon aws-comprehend

我正在构建一个聊天应用程序,并且想要检测正在发送的消息中的情绪。我已经能够使用android studio中的getText方法成功提取要发送的文本。 我按照aws入门进行情感分析,并在调试后发现问题出在运行时

DetectSentimentResult detectSentimentResult = comprehendClient.detectSentiment(detectSentimentRequest);

我已经正确导入了所有依赖项,清单文件包含了权限。

这是我当前的代码

public void detectText(String text) {

        AWSCredentials credentials = new BasicAWSCredentials(KEY, SECRET);

        AmazonComprehendClient comprehendClient = new AmazonComprehendClient(credentials);

        // Call detectSentiment API
        showToast("Calling DetectSentiment");
        DetectSentimentRequest detectSentimentRequest = new DetectSentimentRequest().withText(text)
                                                         .withLanguageCode("en");
        DetectSentimentResult detectSentimentResult = comprehendClient.detectSentiment(detectSentimentRequest);
        System.out.println(detectSentimentResult);
        //Log.d("end","End of DetectSentiment\n");
        showToast("Done");
    }

这类似于aws文档中的代码。 同样在文档中,它使用

AmazonComprehend comprehendClient =
            AmazonComprehendClientBuilder.standard()
                                         .withCredentials(awsCreds)
                                         .withRegion("region")
                                         .build();

amazoncomprehendclientbuilder出现错误,所以我无法使用它。

当前我的代码因以下错误而崩溃,请帮助

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.pam.tapp, PID: 30856
    android.os.NetworkOnMainThreadException
        at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1318)
        at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:86)
        at java.net.Inet6AddressImpl.lookupAllHostAddr(Inet6AddressImpl.java:74)
        at java.net.InetAddress.getAllByName(InetAddress.java:757)
        at com.android.okhttp.internal.Network$1.resolveInetAddresses(Network.java:29)
        at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:187)
        at com.android.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:156)
        at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:98)
        at com.android.okhttp.internal.http.HttpEngine.createNextConnection(HttpEngine.java:389)
        at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:372)
        at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:273)
        at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:478)
        at com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:126)
        at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:261)
        at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getOutputStream(DelegatingHttpsURLConnection.java:218)
        at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java)
        at com.amazonaws.http.UrlHttpClient.writeContentToConnection(UrlHttpClient.java:162)
        at com.amazonaws.http.UrlHttpClient.execute(UrlHttpClient.java:75)
        at com.amazonaws.http.AmazonHttpClient.executeHelper(AmazonHttpClient.java:371)
        at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:212)
        at com.amazonaws.services.comprehend.AmazonComprehendClient.invoke(AmazonComprehendClient.java:907)
        at com.amazonaws.services.comprehend.AmazonComprehendClient.detectSentiment(AmazonComprehendClient.java:749)
        at com.example.pam.tapp.ChatActivity.detectText(ChatActivity.java:353)
        at com.example.pam.tapp.ChatActivity.SendMessage(ChatActivity.java:233)
        at com.example.pam.tapp.ChatActivity.access$100(ChatActivity.java:89)
        at com.example.pam.tapp.ChatActivity$1.onClick(ChatActivity.java:154)
        at android.view.View.performClick(View.java:5773)
        at android.view.View$PerformClick.run(View.java:23035)
        at android.os.Handler.handleCallback(Handler.java:836)
        at android.os.Handler.dispatchMessage(Handler.java:103)
        at android.os.Looper.loop(Looper.java:232)
        at android.app.ActivityThread.main(ActivityThread.java:6802)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1103)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:964)

0 个答案:

没有答案