public class sun.net.www.protocol.http.HttpURLConnection extends java.net.HttpURLConnection
(который он мне не показывает), а не о abstract public class HttpURLConnection extends URLConnection
(который мне виден и который есть в моей проге)class
, а не с расширением java
. И откуда в моем коде вообще нарисовались классы от sun? В ошибке написано "ИсключениеВводаВывода: сервер вернул код http-статуса 503". Код 503 - это ошибка на стороне сервера "сервис недоступен".Это понятно, что надо изменить код программы, чтобы она заработала. Непонятно другое: почему когда в результате выполнения программы:
java.io.IOException: Server returned HTTP response code: 503 for URL: http://ipv4.google.com/sorry/index?continue=http://translate.google.com/translate_tts%3Fie%3DUTF-8%26tl%3Den%26q%3DHello%2Bdear%2Bfriends&q=EgRbegWvGJ3FldEFIhkA8aeDS952qU94sbK8DAgP55pH8cbMicZdMgFy
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1894)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1492)
at com.example.TTS.GoogleTextToSpeech.say(GoogleTextToSpeech.java:32)
at com.example.TTS.Main.main(Main.java:9)
HttpURLConnection.java:1492
GoogleTextToSpeech.class
или на Main.class
в Navigator в Eclipse, то увидим тоже самое:;;
в конце какой-то переменной в системных переменных? path в system variables
стоит такая строчка (с двумя ;)C:\Program Files\Broadcom\Broadcom 802.11;;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;
Нужно ли убрать одну ;
? C:\Program Files\Java\jdk1.8.0_152
%JAVA_HOME%
%JAVA_HOME%\jre
.;%JAVA_HOME%\lib;%JAVA_HOME%\jre\lib
your-unique-entries;%JAVA_HOME%\bin
C:\ProgramData\Oracle\Java\javapath;
. I anecdotally noticed that the links in that directory didn't get updated during an JDK installation update. So it's best to remove C:\ProgramData\Oracle\Java\javapath;
from the Path system environment variable in order to have a consistent environment. package com.example.TTS; // package of class Main
import com.example.TTS.GoogleTextToSpeech; // import class GoogleTextToSpeech
public class Main {
public static void main(String[] args) {
GoogleTextToSpeech gtts = new GoogleTextToSpeech(); // make instance gtts
gtts.say("Hello dear friends", "en"); // use method say
// gtts.say("Bonjour mon amis!", "fr");
}
}
package com.example.TTS; // package of class GoogleTextToSpeech
import java.io.InputStream; // import classes from Java library
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import javazoom.jl.player.Player; // import class from jl1.0.1.jar
public class GoogleTextToSpeech {
private static String ENCODING = "UTF-8"; //make constants and assign values to them
private static String URL_BEGINNING = "http://translate.google.com/translate_tts?ie=";
private static String URL_QUERY = "&q=";
private static String URL_TL = "&tl=";
private static String USER_AGENT_LITERAL = "User-Agent";
private static String USER_AGENT = "Mozilla/4.7";
public void say( String phrase, String lang ) {
try {
//Make full URL
phrase=URLEncoder.encode(phrase, ENCODING); //assign value to variable with name 'phrase' by use method encode from class URLEncoder
String sURL = URL_BEGINNING + ENCODING + URL_TL + lang + URL_QUERY + phrase; //assign value to variable sURL
URL url = new URL(sURL); // make instance url with constructor
//Create connection
URLConnection urlConn = url.openConnection(); //assign value to variable urlConn
HttpURLConnection httpUrlConn = (HttpURLConnection) urlConn; //Declaring a httpUrlConn variable of type HttpURLConnection and assigning it a value to some variable urlConn (which is previously reduce to HttpURLConnection)
httpUrlConn.addRequestProperty(USER_AGENT_LITERAL, USER_AGENT);// use method
//Create stream
InputStream mp3WebStream = urlConn.getInputStream();//create instance and assign it a value
//Play stream
Player plr = new Player(mp3WebStream); //create instance plr with constructor
plr.play(); //use method
}
//use exception with name ex
catch (Exception ex) {
ex.printStackTrace(); //use method
}
}
}
java.io.IOException: Server returned HTTP response code: 503 for URL: http://ipv4.google.com/sorry/index?continue=http://translate.google.com/translate_tts%3Fie%3DUTF-8%26tl%3Den%26q%3DHello%2Bdear%2Bfriends&q=EgRbegWvGJ3FldEFIhkA8aeDS952qU94sbK8DAgP55pH8cbMicZdMgFy
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1894)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1492)
at com.example.TTS.GoogleTextToSpeech.say(GoogleTextToSpeech.java:32)
at com.example.TTS.Main.main(Main.java:9)
C:\Program Files\Java\jdk1.8.0_152\bin;
и C:\ProgramData\Oracle\Java\javapath;
;
? (Одно из значений идет последним)C:\ProgramFiles\Java\jdk1.8.0_152;
Я тоже должен поставить ;
?C:\Program Files\Java\jdk1.8.0_152\bin;
и C:\ProgramData\Oracle\Java\javapath;
и дополнительно прописать JAVA_HOME в user variables со значением: C:\ProgramFiles\Java\jdk1.8.0_152;
. Так? Скажите, пожалуйста, за что отвечает JAVA_HOME?