|
| 1 | +/* |
| 2 | + * Copyright 2018 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.cloud.examples.language; |
| 18 | + |
| 19 | +import android.os.AsyncTask; |
| 20 | +import android.os.Bundle; |
| 21 | +import android.support.v7.app.AppCompatActivity; |
| 22 | +import android.view.View; |
| 23 | +import android.widget.TextView; |
| 24 | + |
| 25 | +import com.google.auth.oauth2.GoogleCredentials; |
| 26 | +import com.google.cloud.language.v1.AnalyzeEntitiesRequest; |
| 27 | +import com.google.cloud.language.v1.AnalyzeEntitiesResponse; |
| 28 | +import com.google.cloud.language.v1.Document; |
| 29 | +import com.google.cloud.language.v1.LanguageServiceClient; |
| 30 | +import com.google.cloud.language.v1.LanguageServiceSettings; |
| 31 | + |
| 32 | +import java.io.IOException; |
| 33 | + |
| 34 | +/** |
| 35 | + * This example demonstrates using the Cloud Natural Language client library to |
| 36 | + * perform text analysis. |
| 37 | + */ |
| 38 | +public class MainActivity extends AppCompatActivity { |
| 39 | + |
| 40 | + private LanguageServiceClient mLanguageClient; |
| 41 | + private TextView mResultText; |
| 42 | + private TextView mWaitText; |
| 43 | + private View mProgressView; |
| 44 | + |
| 45 | + @Override |
| 46 | + protected void onCreate(Bundle savedInstanceState) { |
| 47 | + super.onCreate(savedInstanceState); |
| 48 | + setContentView(R.layout.activity_main); |
| 49 | + |
| 50 | + mResultText = findViewById(R.id.result_text); |
| 51 | + mWaitText = findViewById(R.id.wait_text); |
| 52 | + mProgressView = findViewById(R.id.progress); |
| 53 | + |
| 54 | + // create the language client |
| 55 | + try { |
| 56 | + // NOTE: The line below uses an embedded credential (res/raw/credential.json). |
| 57 | + // You should not package a credential with real application. |
| 58 | + // Instead, you should get a credential securely from a server. |
| 59 | + mLanguageClient = LanguageServiceClient.create( |
| 60 | + LanguageServiceSettings.newBuilder() |
| 61 | + .setCredentialsProvider(() -> |
| 62 | + GoogleCredentials.fromStream(getApplicationContext() |
| 63 | + .getResources() |
| 64 | + .openRawResource(R.raw.credential))) |
| 65 | + .build()); |
| 66 | + } catch (IOException e) { |
| 67 | + throw new IllegalStateException("Unable to create a language client", e); |
| 68 | + } |
| 69 | + |
| 70 | + // get the text to analyze |
| 71 | + String text = "Hi there Sunnyvale California!"; |
| 72 | + mWaitText.setText(getString(R.string.waiting_for_response, text)); |
| 73 | + |
| 74 | + // call the API |
| 75 | + new AnalyzeTask().execute(text); |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + protected void onDestroy() { |
| 80 | + super.onDestroy(); |
| 81 | + |
| 82 | + // shutdown the connection |
| 83 | + mLanguageClient.shutdown(); |
| 84 | + } |
| 85 | + |
| 86 | + private class AnalyzeTask extends AsyncTask<String, Void, AnalyzeEntitiesResponse> { |
| 87 | + |
| 88 | + @Override |
| 89 | + protected AnalyzeEntitiesResponse doInBackground(String... args) { |
| 90 | + AnalyzeEntitiesRequest request = AnalyzeEntitiesRequest.newBuilder() |
| 91 | + .setDocument(Document.newBuilder() |
| 92 | + .setContent(args[0]) |
| 93 | + .setType(Document.Type.PLAIN_TEXT) |
| 94 | + .build()) |
| 95 | + .build(); |
| 96 | + return mLanguageClient.analyzeEntities(request); |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + protected void onPostExecute(AnalyzeEntitiesResponse analyzeEntitiesResponse) { |
| 101 | + // update UI with results |
| 102 | + mProgressView.setVisibility(View.GONE); |
| 103 | + mResultText.setText(analyzeEntitiesResponse.toString()); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | +} |
0 commit comments