1. Download our Official Android App: Forums for Android!

(NOT SOLVED) java.net.SocketException: Broken pipe

Discussion in 'Android Development' started by RoadhammerGaming, Sep 9, 2017.

  1. RoadhammerGaming

    Thread Starter
    Rank:
    None
    Points:
    15
    Posts:
    15
    Joined:
    Aug 24, 2017

    Aug 24, 2017
    15
    0
    15
    Hello, In my app I'm trying to send a flac file to google for speech recognition. I'm using a thread to do the networking request in the background but after 1 minute it times out with a java.net.SocketException: Broken pipe exception. Can anyone help with what could be causing this? here is an image of my log cat output:
    [​IMG]
    And here is the related code:
    Calling method:
    Code (Java):
    1.    public GoogleResponse getRecognizedDataForFlac(final File flcFile, final int max, final int samp) throws IOException, JSONException {
    2.         /**
    3.          * Get recognized data from a FLAC file.
    4.          *
    5.          * @param flacFile FLAC file to recognize
    6.          * @param maxResults the maximum number of results to return in the response
    7.          * @param sampleRate The sampleRate of the file. Default is 8000.
    8.          * @return GoogleResponse with the response and confidence score
    9.          * @throws IOException if something goes wrong
    10.          */
    11.         final GoogleResponse googleResponse = new GoogleResponse();
    12.         new Thread() {
    13.             public void run() {
    14.                 String [] response = new String[0];
    15.                 try {
    16.                     response = rawRequest(flcFile, max, samp);
    17.                 } catch (IOException e) {
    18.                     e.printStackTrace();
    19.                 }
    20.  
    21.                 try {
    22.                     parseResponse(response, googleResponse);
    23.                 } catch (JSONException e) {
    24.                     e.printStackTrace();
    25.                 }
    26.  
    27.             }
    28.         }.start();
    29.  
    30.         return googleResponse;
    31.     }
    Networking request method:
    Code (Java):
    1.     private String[] rawRequest(File inputFile, int maxResults, int sampleRate) throws IOException{
    2.         URL url;
    3.         URLConnection urlConn;
    4.         OutputStream outputStream;
    5.         BufferedReader br;
    6.  
    7.         StringBuilder sb = new StringBuilder(GOOGLE_RECOGNIZER_URL);
    8.         if( language != null ) {
    9.             sb.append("&lang=");
    10.             sb.append(language);
    11.         }
    12.         else{
    13.             sb.append("&lang=auto");
    14.         }
    15.         if(apikey != null) {
    16.             sb.append("&key=");
    17.             sb.append(apikey);
    18.         }
    19.  
    20.         if( !profanityFilter ) {
    21.             sb.append("&pfilter=0");
    22.         }
    23.         sb.append("&maxresults=");
    24.         sb.append(maxResults);
    25.  
    26.         // URL of Remote Script.
    27.         url = new URL(sb.toString());
    28.         Log.i("+++++++++++++++++++", "Recognizer.rawRequest(): url= "+url);
    29.  
    30.         // Open New URL connection channel.
    31.         urlConn = url.openConnection();
    32.  
    33.         // we want to do output.
    34.         urlConn.setDoOutput(true);
    35.  
    36.         // No caching
    37.         urlConn.setUseCaches(false);
    38.  
    39.         // Specify the header content type.
    40.         urlConn.setRequestProperty("Content-Type", "audio/x-flac; rate=" + sampleRate);
    41.         urlConn.setRequestProperty("User-Agent", "Mozilla/5.0 (X11; Linux x86_64) "
    42.                 + "AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.52 Safari/537.36");
    43.  
    44.         // Send POST output.
    45.         outputStream = urlConn.getOutputStream();
    46.  
    47.         FileInputStream fileInputStream = new FileInputStream(inputFile);
    48.  
    49.         byte[] buffer = new byte[256];
    50.  
    51.         while ((fileInputStream.read(buffer, 0, 256)) != -1) {
    52.             outputStream.write(buffer, 0, 256);
    53.         }
    54.  
    55.         fileInputStream.close();
    56.         outputStream.close();
    57.  
    58.         // Get response data.
    59.         br = new BufferedReader(new InputStreamReader(urlConn.getInputStream(), Charset.forName("UTF-8")));
    60.  
    61.         List<String> completeResponse = new ArrayList<String>();
    62.         String response = br.readLine();
    63.         while(response != null) {
    64.             completeResponse.add(response);
    65.             response = br.readLine();
    66.         }
    67.  
    68.         br.close();
    69.  
    70.         Log.i("+++++++++++++++++++", "Google answers- "+completeResponse);
    71.         return completeResponse.toArray(new String[completeResponse.size()]);
    72.     }
     

    Advertisement

  2. RoadhammerGaming

    Thread Starter
    Rank:
    None
    Points:
    15
    Posts:
    15
    Joined:
    Aug 24, 2017

    Aug 24, 2017
    15
    0
    15
    anyone? anyone at all...
     

Share This Page

Loading...