IBM Watson Tone Analyzer提供空响应

时间:2018-01-19 14:36:32

标签: android tone-analyzer

我在我的Android代码中使用IBM Watson的Tone Analyzer,但我一直在java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.Object java.util.List.get(int)' on a null object reference

以下是我的代码

public class MainActivity extends AppCompatActivity {

    final ToneAnalyzer toneAnalyzer =
            new ToneAnalyzer("2018-01-19");

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        JSONObject credentials = null; // Convert the file into a JSON object
        try {
            credentials = new JSONObject(IOUtils.toString(
                    getResources().openRawResource(R.raw.credentials), "UTF-8"

            ));

            String username = credentials.getString("username");
            String password = credentials.getString("password");
            toneAnalyzer.setUsernameAndPassword(username, password);
        } catch (JSONException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        Button analyzeButton = (Button)findViewById(R.id.analyze_button);

        analyzeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                EditText userInput = (EditText)findViewById(R.id.user_input);

                final String textToAnalyze = userInput.getText().toString();
                ToneOptions options = new ToneOptions.Builder()
                        .addTone(Tone.EMOTION)
                        .html(false).build();

                toneAnalyzer.getTone(textToAnalyze, options).enqueue(
                        new ServiceCallback<ToneAnalysis>() {
                            @Override
                            public void onResponse(ToneAnalysis response) {
                                Log.i("Hii", "onResponse: "+response.getDocumentTone());


                                List<ToneScore> scores = response.getDocumentTone()
                                        .getTones()
                                        .get(0)
                                        .getTones();


                                String detectedTones = "";
                                for(ToneScore score:scores) {
                                    if(score.getScore() > 0.5f) {
                                        detectedTones += score.getName() + " ";
                                    }
                                }

                                final String toastMessage =
                                        "The following emotions were detected:\n\n"
                                                + detectedTones.toUpperCase();

                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        Toast.makeText(getBaseContext(),
                                                toastMessage, Toast.LENGTH_LONG).show();
                                    }
                                });
                            }

                            @Override
                            public void onFailure(Exception e) {
                                e.printStackTrace();
                            }
                        });




            }
        });


    }
}

有人可以指出我做错了什么。我将credentials.json文件保存在raw文件夹中。 我尝试在我的Android应用程序中编写每种情绪,但我一直没有得到回应。任何帮助将不胜感激。

0 个答案:

没有答案