使用Google NLP API将实体字符串传递给主要活动(Android)

问题描述 投票:0回答:1

我能够从我的主活动类中将一个字符串(一个句子)传递给Google的NLP API(在一个名为NLPService.java的单独类中配置),但我希望能够从中返回结果(某个实体字符串) NLPService类返回到我的主要活动以进行进一步处理。我可以将实体字符串传递回我的主要活动吗?在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个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.