[hỏi] Sử Dụng Google Translate Api

D.A.N_3002

Active Member
14/11/17
177
49
28
17
Chào mọi người, mình cần xây dựng 1 ứng dụng có text to speech, mình nghĩ rằng sẽ sử dụng Google Translate API
Mk có tham khảo ở link sau : https://cloud.google.com/translate/docs/translating-text
Nhưng khi code thử thì lại xuất hiện lỗi này :
PHP:
Exception in thread "main" java.net.ConnectException: Connection timed out: connect
    at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at sun.net.NetworkClient.doConnect(Unknown Source)
    at sun.net.www.http.HttpClient.openServer(Unknown Source)
    at sun.net.www.http.HttpClient.openServer(Unknown Source)
    at sun.net.www.http.HttpClient.<init>(Unknown Source)
    at sun.net.www.http.HttpClient.New(Unknown Source)
    at sun.net.www.http.HttpClient.New(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.connect(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
    at com.gtranslate.Audio.getAudio(Audio.java:34)
    at Translate_API.Test_API.main(Test_API.java:14)
C:\Users\D.A.N_3002\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 21 seconds)
Đây là code mình sử dụng:
PHP:
package Translate_API;


import com.gtranslate.Audio;
import java.io.IOException;
import java.io.InputStream;
import javazoom.jl.decoder.JavaLayerException;

public class Test_API {

    public static void main(String[] args) throws IOException, JavaLayerException {
        Audio audio = Audio.getInstance();
        InputStream sound = audio.getAudio("I am a bus", "en");
        audio.play(sound);
    }
}
Mọi người cho mình hỏi nên xử lý như thế nào ạ ?? Mk đã thử gg nhiều cách nhưng vẫn không run dc :( Thanks All !
 

Attachments

hoithegiantinhlagi

Active Member
13/3/13
334
105
43
BR-VT
Chào mọi người, mình cần xây dựng 1 ứng dụng có text to speech, mình nghĩ rằng sẽ sử dụng Google Translate API
Mk có tham khảo ở link sau : https://cloud.google.com/translate/docs/translating-text
Nhưng khi code thử thì lại xuất hiện lỗi này :
PHP:
Exception in thread "main" java.net.ConnectException: Connection timed out: connect
    at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at sun.net.NetworkClient.doConnect(Unknown Source)
    at sun.net.www.http.HttpClient.openServer(Unknown Source)
    at sun.net.www.http.HttpClient.openServer(Unknown Source)
    at sun.net.www.http.HttpClient.<init>(Unknown Source)
    at sun.net.www.http.HttpClient.New(Unknown Source)
    at sun.net.www.http.HttpClient.New(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.connect(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
    at com.gtranslate.Audio.getAudio(Audio.java:34)
    at Translate_API.Test_API.main(Test_API.java:14)
C:\Users\D.A.N_3002\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 21 seconds)
Đây là code mình sử dụng:
PHP:
package Translate_API;


import com.gtranslate.Audio;
import java.io.IOException;
import java.io.InputStream;
import javazoom.jl.decoder.JavaLayerException;

public class Test_API {

    public static void main(String[] args) throws IOException, JavaLayerException {
        Audio audio = Audio.getInstance();
        InputStream sound = audio.getAudio("I am a bus", "en");
        audio.play(sound);
    }
}
Mọi người cho mình hỏi nên xử lý như thế nào ạ ?? Mk đã thử gg nhiều cách nhưng vẫn không run dc :( Thanks All !
Lỗi rõ là Connection time out đó bạn, có thể là connect đến server lâu quá nên time out, check trước network, mà bạn show code chỗ tạo connection đến server google đi chớ code này đâu liên quan.
 
  • Like
Reactions: D.A.N_3002