1

I am Trying to test out the Google API for sentiment analysis and somehow it throws UnknownHostException everytime. I have added proxy as well and set Environment variable for GOOGLE_APPLICATION_CREDENTIALS which points to the service account json file. Any help is appreciated.

Sample code is :

import java.net.URL;
import javax.net.ssl.HttpsURLConnection;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.cloud.language.v1.Document;
import com.google.cloud.language.v1.Document.Type;
import com.google.cloud.language.v1.LanguageServiceClient;
import com.google.cloud.language.v1.Sentiment;

public class QuickstartSample {



    static {
        System.setProperty("http.proxyHost", "abcd");
        System.setProperty("http.proxyPort", "1234");
    }

    public static void main(String... args) throws Exception {
        // Instantiates a client
        try {

            LanguageServiceClient language = LanguageServiceClient.create();
            {

                // The text to analyze
                String text = "Hello, world!";
                Document doc = Document.newBuilder().setContent(text).setType(Type.PLAIN_TEXT).build();

                // Detects the sentiment of the text
                Sentiment sentiment = language.analyzeSentiment(doc).getDocumentSentiment();

                System.out.printf("Text: %s%n", text);
                System.out.printf("Sentiment: %s, %s%n", sentiment.getScore(), sentiment.getMagnitude());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

0 Answers0