Android app gets SocketTimeoutException after several requests

Question:

I developed a menu app for Android , the waiter through the app sees all the tables, opens the table through the app, places the order 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, it is running in 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 of the Connector , but it didn't 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>

I appreciate any help in trying to find out what may be causing this problem.

Answer:

Your connection is getting open, this may be the reason it works and then it starts to give a problem.

Possible causes:

  • Check if conn.disconnect() is being activated.

  • Check if your webservice is closing all the connections with the database and if their process is finished because after it is being activated because the maximum connections that are authorized in 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;
}
Scroll to Top
AllEscort