Android App After Multiple Requests Receives SocketTimeoutException

3

I developed a menu application for Android , the waiter by the app sees all the tables, by the application opens the table, makes the request that goes out in the kitchen and so on.

The web service returns a JSON for each request and makes the transactions in the database, even running on a tomcat .

The application after a certain time of use can no longer connect to the server, after testing I saw that I get a java.net.SocketTimeoutException .

private static String sCookie;

public static String acessar(String url){
    HttpURLConnection conn = null;
    String conteudo = "";

        try {               
            conn = (HttpURLConnection) ((new URL(url).openConnection()));                       
            conn.setConnectTimeout(5000);
            conn.setReadTimeout(5000);
            conn.setRequestProperty("Connection","Close");
            if(sCookie != null && !"".equals(sCookie))
                conn.setRequestProperty("Cookie", sCookie);   

            int responseCode = conn.getResponseCode();

            if(responseCode == 200) {
                conteudo = streamToStr(conn.getInputStream());
                 String cookie = conn.getHeaderField("set-cookie");
                    if(cookie != null && cookie.length() > 0)
                        sCookie = cookie;                   
            }

            return conteudo;
        }
        catch(Exception ex){    
            ex.printStackTrace();               
        } finally {
            if(conn != null)
                conn.disconnect();              
        }   
        return conteudo;
}

I thought it was tomcat , I changed the default settings for Connector , but it did not work.

<Connector  port="9090"
                enableLookups="false" 
                protocol = "HTTP/1.1"
                acceptorThreadCount="2"
                maxThreads="300" 
                connectionTimeout="30000"
                compressableMimeTypes="text/*,application/x-javascript,application/javascript"
                compressionMinSise="2048"
                noCompressionUserAgents="gozilla, traviata"
                maxSpareThreads="150" 
                maxRequestsPerChild="20000"
                compression="force"          
                redirectPort="8443"
                keepAlive="true"
                keepAliveTimeOut="20000">
    </Connector>

Thanks in advance for any help in trying to figure out what might be causing this problem.

    
asked by anonymous 30.06.2015 / 14:30

1 answer

0
  

Your connection is getting open, this may be the reason it works and then start to give trouble.

Possible causes:

  • Verify that conn.disconnect () is being triggered.

  • Make sure your webservice is closing all connections to the database and if their process is terminated because after this being triggered because the maximum number of connections allowed in the tomcat is 300 maxThreads="300" .

private static String sCookie;

public static String acessar(String url){
    HttpURLConnection conn = null;
    String conteudo = "";

        try {               
            conn = (HttpURLConnection) ((new URL(url).openConnection()));                       
            conn.setConnectTimeout(5000);
            conn.setReadTimeout(5000);
            conn.setRequestProperty("Connection","Close");
            if(sCookie != null && !"".equals(sCookie))
                conn.setRequestProperty("Cookie", sCookie);   

            int responseCode = conn.getResponseCode();

            if(responseCode == 200) {
                conteudo = streamToStr(conn.getInputStream());
                 String cookie = conn.getHeaderField("set-cookie");
                    if(cookie != null && cookie.length() > 0)
                        sCookie = cookie;                   
            }

            conn.disconnect();

            return conteudo;
        }
        catch(Exception ex){    
            ex.printStackTrace();               
        } finally {
            if(conn != null)
                conn.disconnect();              
        }   
        return conteudo;
}
    
01.07.2015 / 14:50