Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: POST a Speex/Flac file to Google Voice Search API via Java

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default POST a Speex/Flac file to Google Voice Search API via Java

    I am trying to build an Java Application ,which connects to the Google Voice Search API. I have developed a C# programm, which is working fine.

    C#:
    try
    {
       FileStream FS_Audiofile = new FileStream("temp.flac", FileMode.Open, FileAccess.Read);
       BinaryReader BR_Audiofile = new BinaryReader(FS_Audiofile);
       byte[] BA_AudioFile = BR_Audiofile.ReadBytes((Int32)FS_Audiofile.Length);
       FS_Audiofile.Close();
       BR_Audiofile.Close();
     
       HttpWebRequest _HWR_SpeechToText = null;
     
       _HWR_SpeechToText = (HttpWebRequest)WebRequest.Create("http://www.google.com/speech-api/v1/recognize?xjerr=1&client=chromium&lang=de-DE&maxresults=1&pfilter=0");
     
       _HWR_SpeechToText.Method = "POST";
       _HWR_SpeechToText.ContentType = "audio/x-flac; rate=44100";
       _HWR_SpeechToText.ContentLength = BA_AudioFile.Length;
       _HWR_SpeechToText.GetRequestStream().Write(BA_AudioFile, 0, BA_AudioFile.Length);
     
       HttpWebResponse HWR_Response = (HttpWebResponse)_HWR_SpeechToText.GetResponse();
       if (HWR_Response.StatusCode == HttpStatusCode.OK)
       {
          StreamReader SR_Response = new StreamReader(HWR_Response.GetResponseStream());
       }
     
    }
    catch (Exception e)
    {
     
    }
    And here the Code Java, which doesn't work:

    RandomAccessFile accessFile = new RandomAccessFile("temp.flac", "r");
    byte[] bs = new byte[(int)accessFile.length()];
    accessFile.read(bs);
     
            try
            {
                HttpURLConnection httpURLConnection = null;
                URL url = new URL("http", "www.google.com", "/speech-api/v1/recognize?xjerr=1&client=chromium&lang=de-DE&maxresults=1&pfilter=0");
                httpURLConnection = (HttpURLConnection)url.openConnection();
     
                int contentLength = bs.length;
     
                httpURLConnection.setDoOutput(true);
                httpURLConnection.setDoInput(true);
                httpURLConnection.setInstanceFollowRedirects(false); 
                httpURLConnection.setUseCaches (false);
     
                httpURLConnection.setRequestMethod("POST");
                httpURLConnection.setRequestProperty("Content-Type", "audio/x-flac; rate=44100");
                httpURLConnection.setRequestProperty("Content-Length", Integer.toString(contentLength));
     
                DataOutputStream dataOutputStream = new DataOutputStream(httpURLConnection.getOutputStream());
                dataOutputStream.write(bs);
                dataOutputStream.flush();
     
                Scanner scanner = new Scanner(httpURLConnection.getInputStream()).useDelimiter("\\A");
                System.out.println(scanner.toString());
     
            }
            catch ( Exception e)
            {
                  System.out.println(e.toString());
            }

    Does anyone see, which kind of error I am doing? Thanks for your help in advance.

    I am getting the exception "java.io.IOException: Error writing to server".

    Thx and Regards Linoge


  2. #2
    Junior Member
    Join Date
    Feb 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: POST a Speex/Flac file to Google Voice Search API via Java

    I am trying to build an Java Application ,which connects to the Google Voice Search API. I have developed a C# programm, which is working fine.

    C#:
    try
    {
       FileStream FS_Audiofile = new FileStream("temp.flac", FileMode.Open, FileAccess.Read);
       BinaryReader BR_Audiofile = new BinaryReader(FS_Audiofile);
       byte[] BA_AudioFile = BR_Audiofile.ReadBytes((Int32)FS_Audiofile.Length);
       FS_Audiofile.Close();
       BR_Audiofile.Close();
     
       HttpWebRequest _HWR_SpeechToText = null;
     
       _HWR_SpeechToText = (HttpWebRequest)WebRequest.Create("http://www.google.com/speech-api/v1/recognize?xjerr=1&client=chromium&lang=de-DE&maxresults=1&pfilter=0");
     
       _HWR_SpeechToText.Method = "POST";
       _HWR_SpeechToText.ContentType = "audio/x-flac; rate=44100";
       _HWR_SpeechToText.ContentLength = BA_AudioFile.Length;
       _HWR_SpeechToText.GetRequestStream().Write(BA_AudioFile, 0, BA_AudioFile.Length);
     
       HttpWebResponse HWR_Response = (HttpWebResponse)_HWR_SpeechToText.GetResponse();
       if (HWR_Response.StatusCode == HttpStatusCode.OK)
       {
          StreamReader SR_Response = new StreamReader(HWR_Response.GetResponseStream());
       }
     
    }
    catch (Exception e)
    {
     
    }
    And here the Code Java, which doesn't work:
    RandomAccessFile accessFile = new RandomAccessFile("temp.flac", "r");
    byte[] bs = new byte[(int)accessFile.length()];
    accessFile.read(bs);
     
            try
            {
                HttpURLConnection httpURLConnection = null;
                URL url = new URL("http", "www.google.com", "/speech-api/v1/recognize?xjerr=1&client=chromium&lang=de-DE&maxresults=1&pfilter=0");
                httpURLConnection = (HttpURLConnection)url.openConnection();
     
                int contentLength = bs.length;
     
                httpURLConnection.setDoOutput(true);
                httpURLConnection.setDoInput(true);
                httpURLConnection.setInstanceFollowRedirects(false); 
                httpURLConnection.setUseCaches (false);
     
                httpURLConnection.setRequestMethod("POST");
                httpURLConnection.setRequestProperty("Content-Type", "audio/x-flac; rate=44100");
                httpURLConnection.setRequestProperty("Content-Length", Integer.toString(contentLength));
     
                DataOutputStream dataOutputStream = new DataOutputStream(httpURLConnection.getOutputStream());
                dataOutputStream.write(bs);
                dataOutputStream.flush();
     
                Scanner scanner = new Scanner(httpURLConnection.getInputStream()).useDelimiter("\\A");
                System.out.println(scanner.toString());
     
            }
            catch ( Exception e)
            {
               System.out.println(e.toString());
            }
    I am getting the exception "java.io.IOException: Error writing to server".

    Does anyone see, which kind of error I am doing? Thanks for your help in advance.

    Thx and Regards Linoge

  3. #3
    Junior Member
    Join Date
    Feb 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: POST a Speex/Flac file to Google Voice Search API via Java

    I am trying to build an Java Application ,which connects to the Google Voice Search API. I have developed a C# programm, which is working fine.

    C#:
    try
    {
       FileStream FS_Audiofile = new FileStream("temp.flac", FileMode.Open, FileAccess.Read);
       BinaryReader BR_Audiofile = new BinaryReader(FS_Audiofile);
       byte[] BA_AudioFile = BR_Audiofile.ReadBytes((Int32)FS_Audiofile.Length);
       FS_Audiofile.Close();
       BR_Audiofile.Close();
     
       HttpWebRequest _HWR_SpeechToText = null;
     
       _HWR_SpeechToText = (HttpWebRequest)WebRequest.Create("http://www.google.com/speech-api/v1/recognize?xjerr=1&client=chromium&lang=de-DE&maxresults=1&pfilter=0");
     
       _HWR_SpeechToText.Method = "POST";
       _HWR_SpeechToText.ContentType = "audio/x-flac; rate=44100";
       _HWR_SpeechToText.ContentLength = BA_AudioFile.Length;
       _HWR_SpeechToText.GetRequestStream().Write(BA_AudioFile, 0, BA_AudioFile.Length);
     
       HttpWebResponse HWR_Response = (HttpWebResponse)_HWR_SpeechToText.GetResponse();
       if (HWR_Response.StatusCode == HttpStatusCode.OK)
       {
          StreamReader SR_Response = new StreamReader(HWR_Response.GetResponseStream());
       }
     
    }
    catch (Exception e)
    {
     
    }
    And here the Code Java, which doesn't work:
    RandomAccessFile accessFile = new RandomAccessFile("temp.flac", "r");
    byte[] bs = new byte[(int)accessFile.length()];
    accessFile.read(bs);
     
            try
            {
                HttpURLConnection httpURLConnection = null;
                URL url = new URL("http", "www.google.com", "/speech-api/v1/recognize?xjerr=1&client=chromium&lang=de-DE&maxresults=1&pfilter=0");
                httpURLConnection = (HttpURLConnection)url.openConnection();
     
                int contentLength = bs.length;
     
                httpURLConnection.setDoOutput(true);
                httpURLConnection.setDoInput(true);
                httpURLConnection.setInstanceFollowRedirects(false); 
                httpURLConnection.setUseCaches (false);
     
                httpURLConnection.setRequestMethod("POST");
                httpURLConnection.setRequestProperty("Content-Type", "audio/x-flac; rate=44100");
                httpURLConnection.setRequestProperty("Content-Length", Integer.toString(contentLength));
     
                DataOutputStream dataOutputStream = new DataOutputStream(httpURLConnection.getOutputStream());
                dataOutputStream.write(bs);
                dataOutputStream.flush();
     
                Scanner scanner = new Scanner(httpURLConnection.getInputStream()).useDelimiter("\\A");
                System.out.println(scanner.toString());
     
            }
            catch ( Exception e)
            {
               System.out.println(e.toString());
            }
    I am getting the exception "java.io.IOException: Error writing to server".

    Does anyone see, which kind of error I am doing? Thanks for your help in advance.

    Thx and Regards Linoge

  4. #4
    Administrator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    24,848
    Thanks
    64
    Thanked 2,645 Times in 2,615 Posts

    Default Re: POST a Speex/Flac file to Google Voice Search API via Java

    What statement is causing the error?

Similar Threads

  1. Frustrating File>save as search function in google script
    By segreto in forum Other Programming Languages
    Replies: 2
    Last Post: August 16th, 2012, 12:40
  2. Using Google Calendar API
    By anis_huq in forum Android Development
    Replies: 0
    Last Post: February 11th, 2012, 13:00
  3. [SOLVED] Google translate API
    By vozmen in forum Java ME (Mobile Edition)
    Replies: 8
    Last Post: December 14th, 2011, 06:06
  4. [SOLVED] Google translate api
    By vozmen in forum Java ME (Mobile Edition)
    Replies: 2
    Last Post: December 14th, 2011, 04:02

Tags for this Thread