I send data to the server and for this I use AsynTask and in the doInBackgraund() method doInBackgraund() send

 urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.setRequestProperty("Content-Type", newValue); urlConnection.setRequestMethod("POST"); urlConnection.setDoInput(true); urlConnection.setDoOutput(true); urlConnection.connect(); bos = new BufferedOutputStream(urlConnection.getOutputStream()); bos.write(data); bos.flush(); 

But what to do if something with the connection? I have implemented it so that when data starts sending, ProgressDialog() turns on and when sending completes, it turns off. But when something is connected, it keeps spinning and nothing happens.

Is it possible to somehow put the counter on the fact that if something is with the connection, then after 5 seconds, turn off and show the sending error.?

But here is the second point that there may be a bad Internet connection and the data is simply being sent for a long time.

How to make the error function work exactly if something is connected to the server?

    1 answer 1

    Use method:

     urlConnection.setConnectTimeout(connectTimeout); 

    If the server does not have time to respond, there will be an exception

     java.net.SocketTimeoutException 
    • If I understand you correctly, this is Timeout first sending data ... That is, in a situation, if the data volume is large or weak Internet, and the transfer time is 2 minutes, and Timeout 1 minute, then the sending process will break, right? - Aleksey Timoshchenko
    • No, this is exactly the connection setup. If the connection was established within the specified time, there will be no exception. To set the data reading timeout there is: urlConnection.setReadTimeout (socketTimeout); - Galtran
    • I still wanted to ask) Do you think I set the waiting time of 20 seconds = 20,000, is this normal? - Aleksey Timoshchenko 2:49 pm
    • 20 seconds, it seems to me - this is a bit too much .. I would put something around 10. But here we have to look at the applied task, it is likely that 20 in your case will be normal) - Galtran