Android app after several changes receives Sockettimeoutexception

Asked

Viewed 91 times

3

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

The web service returns a JSON for each request and makes the transactions in the database, the same is running in a Tomcat.

The application after a certain time of use can no longer connect to the server, after tests I saw that I receive 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 the Tomcat, I changed the default settings on Connector, but did not resolve.

<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 trying to figure out what might be causing this problem.

  • 1

    Why so little timeout time? 5000 is in milesseconds meaning the timeout is 5 seconds, the default for HTTP requests is 30000 (30 seconds)

  • Before the timeout time was 15 seconds, when the waiter would consult the list of tables, the app opens the progressiDialog asking him to wait, and he would stay there for 15 seconds until giving the timeoutexception, so I decided to decrease why regardless of the time, has a certain moment that the app can no longer communicate with the server.

  • OK @Edson, but the default for Timeout is 30 seconds. Just to clarify. You considered calling your webservice using the REST Client to check if it takes?

  • As this occurs after several requests, it would be good to check if your scripts are opening and closing the connection with the database. Check that processes started by Tomcat end at the end of the request.

  • Hi @Hiagosouza, yes I got to test using Rest and got the same errors.

  • Another detail I forgot to mention, when I developed the app, I did the tests on my bike x there was at no time this type of error, the waiters use a Samsung Galaxy ace duos and with them happens what sitei in the post.

  • Ok, I’ve seen your android class now. It’s missing a Conn.Disconnect();

  • this in Finally the Disconnect();

Show 3 more comments

1 answer

0

Your connection is getting open, this may be why it works and then start giving problem.

Possible causes:

  • Check whether the Conn.Disconnect() is being triggered.

  • Make sure your webservice is closing all connections with the database and whether their process is finalized because after this being triggered because as many connections as are allowed 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;
}
  • this in Finally the Disconnect();

  • forgiveness had not seen.

  • But did you debug? Did you see if it actually disconnects?

  • yes, debugged, it really disconnects.

  • What about your webservice? It’s opening and closing connections in the database as I asked in the question comments?

  • could post your method on WS for me to check?

  • Yes, database connections are opened and closed immediately after the select in the database.

  • And Tomcat processes are also normal?

  • 1

    So that you can see for yourself, I took 2 prints from Tomcat (Tomcat Remote Management and Real-Time Monitoring Tool). https://drive.google.com/file/d/0B1DPcYy_RrsYVjJCSW9vbGttbzQ/view?pli=1 https://drive.google.com/filed/0B1DPcYy_RrsYamdFLUZhVNoekk/view?pli1 the application responds at port 9090, and the ip 192.168.200.28 is my machine, the 172.16.200.12 is the smartphone’s one.

  • ok from what I saw the smartphone process is closed and its time was 00:02:18.347 is it normal the process to have stayed 2 minutes ? Because that’s what happened, it was 2 minutes only on his smartphone when he gave the 5 seconds he timed out because he didn’t get the answer.

  • Do you know why the process stayed so long? Have you debugged the service that is called to see why of the delay ? because if the time is the same, you have to increase the timeout for more time on android and do not decrease it.

  • That’s the time from the moment the process was finalized until the current time, so much so that I made a request for the smartphone and went on the first look, the status appears as Ended and the proc.time 0:00:07.701.

  • Edson you called Webservice in hand using REST ? Call him and look at the average response time there adjusts in your timeout the default is 30 as I had said before, but it may be that the specific service is taking more time to answer you.

Show 8 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.