我能够从我的主活动类将一个字符串(一个句子)传递给 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;
}
`