2017-02-26 1 views
0

Le code ci-dessous est utilisé pour se connecter à l'api de speechmatics pour la conversion de la parole en texte en java. Mais incapable de se connecter et renvoie une erreur. https://app.speechmatics.com/api-details Voici le lien qui contient l'ensemble des informations et im en utilisant ceci:Java Api Speechmatics

boucle -F [email protected]_audio_file.mp3 -F modèle = en-US "https://api.speechmatics.com/v1.0/user/ $ MY_API_USER_ID/emploi/auth_token = $ MY_API_AUTH_TOKEN?" # transcription

try { 
     URL url = new URL("https://api.speechmatics.com/v1.0/user/17879/jobs/?auth_token=ZmQzODNiMGUtMzQwYS00MzUxLWJkZDEtZTBlYzUxMTg2YWVm"); 
     HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 

     conn.setRequestMethod("POST"); 
     conn.setRequestProperty("model", "en-US"); 
     conn.setRequestProperty("data_file", "open('RecordAudio.wav', 'rb')"); 
     conn.setRequestProperty("Ocp-Apim-Subscription-Key", "2:5a703eadae214b0bbe91344e546eac4a"); 
     conn.setDoOutput(true); 
     if (conn.getResponseCode() != 200) { 
      throw new RuntimeException("Failed : HTTP error code : " 
        + conn.getResponseCode()); 
     } 

     BufferedReader br = new BufferedReader(new InputStreamReader(
      (conn.getInputStream()))); 

     String output; 
     System.out.println("Output from Server .... \n"); 
     while ((output = br.readLine()) != null) { 
      System.out.println(output); 
      // s= s.concat(output); 
     } 

     conn.disconnect(); 

     } catch (MalformedURLException e) { 

     e.printStackTrace(); 

     } catch (IOException e) { 

     e.printStackTrace(); 

     } 

} 

}

Répondre

0

Le code ci-dessous demandera une transcription en utilisant une demande de poste.

import org.apache.http.HttpEntity; 
import org.apache.http.HttpResponse; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.client.utils.URIBuilder; 
import org.apache.http.entity.ContentType; 
import org.apache.http.impl.client.CloseableHttpClient; 
import org.apache.http.impl.client.HttpClients; 

import java.io.FileInputStream; 
import java.io.File; 

import java.net.URI; 
import java.net.URISyntaxException; 
import org.apache.http.entity.mime.*; 

public class Test { 
    public static void main(String[] args){ 
     System.out.println("Hello"); 
     try { 
      URI uri = new URIBuilder("https://api.speechmatics.com/v1.0/user/$user_id/jobs/") 
        .addParameter("auth_token", "$auth_token") 
        .build(); 

      CloseableHttpClient httpclient = HttpClients.createDefault(); 

      HttpPost httppost = new HttpPost(uri); 
      httppost.setHeader("Accept", "application/json"); 

      File imgFile = new File("/filepath/test_short.aac"); 

      HttpEntity entity = MultipartEntityBuilder.create() 
       .addTextBody("model", "en-US") 
       .addTextBody("notification", "none") 
       .addBinaryBody("data_file", imgFile, ContentType.DEFAULT_BINARY ,imgFile.getName()) 
       .build(); 

      httppost.setEntity(entity); 

      //System.out.println(entity.getContent()); 
      System.out.println("Executing request " + httppost.getRequestLine()); 

      HttpResponse response = httpclient.execute(httppost); 
      System.out.println(response); 
      httpclient.close(); 
     } catch (java.io.IOException e) { 
      e.printStackTrace(); 
     } catch (URISyntaxException e) { 
      e.printStackTrace(); 
     } 
    } 
}