• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

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

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:
crowderror3.png

And here is the related code:
Calling method:
Java:
   public GoogleResponse getRecognizedDataForFlac(final File flcFile, final int max, final int samp) throws IOException, JSONException {
        /**
         * Get recognized data from a FLAC file.
         *
         * @param flacFile FLAC file to recognize
         * @param maxResults the maximum number of results to return in the response
         * @param sampleRate The sampleRate of the file. Default is 8000.
         * @return GoogleResponse with the response and confidence score
         * @throws IOException if something goes wrong
         */
        final GoogleResponse googleResponse = new GoogleResponse();
        new Thread() {
            public void run() {
                String [] response = new String[0];
                try {
                    response = rawRequest(flcFile, max, samp);
                } catch (IOException e) {
                    e.printStackTrace();
                }

                try {
                    parseResponse(response, googleResponse);
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        }.start();

        return googleResponse;
    }
Networking request method:
Java:
    private String[] rawRequest(File inputFile, int maxResults, int sampleRate) throws IOException{
        URL url;
        URLConnection urlConn;
        OutputStream outputStream;
        BufferedReader br;

        StringBuilder sb = new StringBuilder(GOOGLE_RECOGNIZER_URL);
        if( language != null ) {
            sb.append("&lang=");
            sb.append(language);
        }
        else{
            sb.append("&lang=auto");
        }
        if(apikey != null) {
            sb.append("&key=");
            sb.append(apikey);
        }

        if( !profanityFilter ) {
            sb.append("&pfilter=0");
        }
        sb.append("&maxresults=");
        sb.append(maxResults);

        // URL of Remote Script.
        url = new URL(sb.toString());
        Log.i("+++++++++++++++++++", "Recognizer.rawRequest(): url= "+url);

        // Open New URL connection channel.
        urlConn = url.openConnection();

        // we want to do output.
        urlConn.setDoOutput(true);

        // No caching
        urlConn.setUseCaches(false);

        // Specify the header content type.
        urlConn.setRequestProperty("Content-Type", "audio/x-flac; rate=" + sampleRate);
        urlConn.setRequestProperty("User-Agent", "Mozilla/5.0 (X11; Linux x86_64) "
                + "AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.52 Safari/537.36");

        // Send POST output.
        outputStream = urlConn.getOutputStream();

        FileInputStream fileInputStream = new FileInputStream(inputFile);

        byte[] buffer = new byte[256];

        while ((fileInputStream.read(buffer, 0, 256)) != -1) {
            outputStream.write(buffer, 0, 256);
        }

        fileInputStream.close();
        outputStream.close();

        // Get response data.
        br = new BufferedReader(new InputStreamReader(urlConn.getInputStream(), Charset.forName("UTF-8")));

        List<String> completeResponse = new ArrayList<String>();
        String response = br.readLine();
        while(response != null) {
            completeResponse.add(response);
            response = br.readLine();
        }

        br.close();

        Log.i("+++++++++++++++++++", "Google answers- "+completeResponse);
        return completeResponse.toArray(new String[completeResponse.size()]);
    }
 

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones