我一直在尝试实现Google的Natural Language API。当我去验证我的凭据时,发现netty有问题。有没有关于如何解决这个问题的线索?我使用的是Java8 btw。我在其他一些线程上看到是Java版本导致了这个问题。
String text = "My name is Teneala. I ran last week.";
InputStream is = getResources().openRawResource(R.raw.credentials);
try {
GoogleCredential credential = null;
credential = GoogleCredential.fromStream(is);
Collection<String> scopes = Collections.singleton("https://www.googleapis.com/auth/cloud-language");
if (credential.createScopedRequired()) {
credential = credential.createScoped(scopes);
}
// copy over key values, note the additional "s", set some expiry
// com.google.auth.oauth2.GoogleCredentials
GoogleCredentials sac = ServiceAccountCredentials.newBuilder()
.setPrivateKey(credential.getServiceAccountPrivateKey())
.setPrivateKeyId(credential.getServiceAccountPrivateKeyId())
.setClientEmail(credential.getServiceAccountId())
.setScopes(scopes)
.setAccessToken(new AccessToken(credential.getAccessToken(), null))
.build();
// Latest generation Google libs, GoogleCredentials extends Credentials
CredentialsProvider cp = FixedCredentialsProvider.create(sac);
LanguageServiceSettings settings = (LanguageServiceSettings) LanguageServiceSettings.newBuilder().setCredentialsProvider(cp).build();
LanguageServiceClient language = LanguageServiceClient.create(settings);
Document doc = Document.newBuilder()
.setContent(text)
.setType(Type.PLAIN_TEXT)
.build();
AnalyzeSyntaxRequest request = AnalyzeSyntaxRequest.newBuilder()
.setDocument(doc)
.setEncodingType(EncodingType.UTF16)
.build();
// analyze the syntax in the given text
AnalyzeSyntaxResponse response = language.analyzeSyntax(request);
// print the response
for (Token token : response.getTokensList()) {
System.out.printf("\tText: %s\n", token.getText().getContent());
System.out.printf("\tBeginOffset: %d\n", token.getText().getBeginOffset());
System.out.printf("Lemma: %s\n", token.getLemma());
System.out.printf("PartOfSpeechTag: %s\n", token.getPartOfSpeech().getTag());
System.out.printf("\tAspect: %s\n", token.getPartOfSpeech().getAspect());
System.out.printf("\tCase: %s\n", token.getPartOfSpeech().getCase());
System.out.printf("\tForm: %s\n", token.getPartOfSpeech().getForm());
System.out.printf("\tGender: %s\n", token.getPartOfSpeech().getGender());
System.out.printf("\tMood: %s\n", token.getPartOfSpeech().getMood());
System.out.printf("\tNumber: %s\n", token.getPartOfSpeech().getNumber());
System.out.printf("\tPerson: %s\n", token.getPartOfSpeech().getPerson());
System.out.printf("\tProper: %s\n", token.getPartOfSpeech().getProper());
System.out.printf("\tReciprocity: %s\n", token.getPartOfSpeech().getReciprocity());
System.out.printf("\tTense: %s\n", token.getPartOfSpeech().getTense());
System.out.printf("\tVoice: %s\n", token.getPartOfSpeech().getVoice());
System.out.println("DependencyEdge");
System.out.printf("\tHeadTokenIndex: %d\n", token.getDependencyEdge().getHeadTokenIndex());
System.out.printf("\tLabel: %s\n\n", token.getDependencyEdge().getLabel());
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}Here is my gradle file. I've tried excluding it, I've looked into troubleshooting documentation, I've tried implementing it, ect.
```javascriptdependencies {// Google APIsimplementation('com.google.cloud:google-cloud-translate:1.12.0') { exclude group: 'org.apache.httpcomponents' exclude group: 'org.google', module: 'google' exclude group: 'com.google.auto.value', module: 'auto-value' //exclude 'io.netty:netty-tcnative:2.0.20.Final:'}annotationProcessor 'com.google.cloud:google-cloud-translate:1.12.0'implementation ('com.google.cloud:google-cloud-language:1.14.0'){ exclude group: 'com.google.auto.value', module: 'auto-value' //exclude 'io.netty:netty-tcnative:2.0.20.Final:'}}
发布于 2019-11-14 02:38:25
不要紧。我在这里找到了解决方案:https://github.com/grpc/grpc-java/blob/master/SECURITY.md#troubleshooting。我不得不将其添加到应用程序build gradle中。关于Android设备不支持netty的问题。
implementation 'io.grpc:grpc-okhttp:1.7.0'
implementation 'com.squareup.okhttp3:okhttp:3.0.1'https://stackoverflow.com/questions/58829468
复制相似问题