【问题标题】:Passing entities string using Google NLP API to Main Activity (Android)使用 Google NLP API 将实体字符串传递给 Main Activity (Android)
【发布时间】:2017-11-25 14:31:45
【问题描述】:

我能够从我的主活动类将一个字符串(一个句子)传递给 Google 的 NLP API(配置在一个名为 NLPService.java 的单独类中),但我希望能够返回结果(某个实体字符串) 从 NLPService 类返回到我的 Main Activity 以进行进一步处理。我可以将实体字符串传递回我的主要活动吗?在 Android Studio 中,我使用以下代码创建了一个 NLPService.java:

//New NLP Model
public void analyzeText(String textToAnalyze) {

    Document doc = new Document();
        doc.setContent(textToAnalyze)
            .setType("PLAIN_TEXT");

    final String[] result = new String[1];
    if (textToAnalyze != null && !doc.isEmpty()) {
        doc.setContent(textToAnalyze);
        //Config request to be sent to Google NLP
        Features features = new Features();
        features.setExtractEntities(true);

        final AnnotateTextRequest request = new AnnotateTextRequest();
        request.setDocument(doc);
        request.setFeatures(features);

        AsyncTask.execute(new Runnable() {
            public void run() {
                try {
                    returnResponse(NLPService.documents().annotateText(request).execute());
                    result[0] = returnResponse(NLPService.documents().annotateText(request).execute());
                    Log.i("getAsyncResponse", "RESULT: " + result[0]);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }

}


public String returnResponse(AnnotateTextResponse response) {
    final List<Entity> entityList = response.getEntities();

    String entities = "";
    for (Entity entity : entityList) {
        entities += "\n" + entity.getName().toUpperCase() + " " + entity.getType();

    }
    return entities;

}

`

【问题讨论】:

    标签: android api nlp google-cloud-nl


    【解决方案1】:

    常用方法是使用广播 (LocalBroadcastManager) 将您打算从服务发送的数据传递给任何活动。 Example of Previous post 或者您可以使用不太可能的 SharedPreferences。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-08-02
      • 2013-02-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-17
      相关资源
      最近更新 更多