inputstreambufferedreaderbufferedinputstreamnanohttpd

Reading InputStream of NANOHTTPD gives Socket TimeOut Exception


I am trying to read the InputStream from IHTTPSession.getInputStream() using the following code but its gives Socket TimeOut Exception every time.

private String readInStream(InputStream in){

        StringBuffer outBuffer=new StringBuffer();
        BufferedInputStream bis=new BufferedInputStream(in);
        try {
            while(bis.available()>0){
                int ch= bis.read();
                outBuffer.append((char)ch);
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        Log.e("DATA_Length", "outputBuffer :"+outBuffer.toString().length());
        return outBuffer.toString();
    }

i also tried the following Method but the same exception arises

private String readInStream(InputStream in){
        String line="";
        StringBuffer outBuffer=new StringBuffer();

        BufferedReader rd=new BufferedReader(new InputStreamReader(in));

        try {
            while((line=rd.readLine()) != null){
                outBuffer.append(line);
            }
        } catch (IOException e) {
            Log.e("IOException", "IOException in readInStream:");
            e.printStackTrace();
        }

        Log.e("DATA_Length", "outputBuffer :"+outBuffer.toString().length());
        return outBuffer.toString();
    }

Solution

  • Getting the content length from the header and reading up to it solved the problem.