не может решить java. net .MalformedURLException - PullRequest
0 голосов
/ 12 марта 2020
    public class AnalyticsDetectLanguage {
    static String subscription_key_var;
    static String subscription_key;
    static String endpoint_var;
    static String endpoint;

    public static void Initialize () throws Exception {
        subscription_key = "xxxxxx";
        endpoint = "xxxxxxx";
    }
 static String path = "https://northeurope.api.cognitive.microsoft.com/text/analytics/v2.1/languages";

    public static String GetLanguage (Documents documents) throws Exception {
        String text = new Gson().toJson(documents);
        byte[] encoded_text = text.getBytes("UTF-8");

        URL url = new URL(endpoint+path);
        HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Content-Type", "text/json");
        connection.setRequestProperty("Ocp-Apim-Subscription-Key", subscription_key);
        connection.setDoOutput(true);

        DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
        wr.write(encoded_text, 0, encoded_text.length);
        wr.flush();
        wr.close();

        StringBuilder response = new StringBuilder ();
        BufferedReader in = new BufferedReader(
                new InputStreamReader(connection.getInputStream()));
        String line;
        while ((line = in.readLine()) != null) {
            response.append(line);
        }
        in.close();

        return response.toString();
    }

    public static String prettify(String json_text) {
        JsonParser parser = new JsonParser();
        JsonObject json = parser.parse(json_text).getAsJsonObject();
        Gson gson = new GsonBuilder().setPrettyPrinting().create();
        return gson.toJson(json);
    }

    public static void main (String[] args) {
        try {
            Initialize();

            Documents documents = new Documents ();
            documents.add ("1", "This is a document written in English.");
            documents.add ("2", "Este es un document escrito en Español.");
            documents.add ("3", "这是一个用中文写的文件");

            String response = GetLanguage (documents);
            System.out.println (prettify (response));
        }
        catch (Exception e) {
            System.out.println (e);
        }
    }
}

Я пытаюсь работать с API текстовой аналитики Microsoft azure, но когда я запускаю этот код с правильными ключами, я получаю исключение java. net .MalformedURLException для ключа конечной точки, которое само по себе является правильным , но ошибка возвращает ключ как xxxxxxxhttps. Как получить код для запуска?

1 Ответ

1 голос
/ 12 марта 2020
endpoint = "xxxxxxx";
URL url = new URL(endpoint+path);

URL, который вы в итоге получите, это "xxxxxxx https://northeurope.api.cognitive.microsoft.com/text/analytics/v2.1/languages".

Неверный URL.

...