How do you put timeout on HttpURLConnection?



  • I'm sending data to the server and using it. AsynTask and in the method doInBackgraund() I'm doing a shipment.

    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 do you do if it's connected? I'm realised when the data starts to go on. ProgressDialog() and when the shipment ends, it turns off. But when something with the connection, it keeps spinning and nothing happens.

    Is there any way to put a counter on the fact that if anything is connected, then in five seconds, turn off and show a mistake of departure. ?

    But there's a second point, which might be a bad connection to the Internet, and the data just came out of it for a long time.

    How do you make the error function work exactly if something connects or serves?



  • Use the method:

    urlConnection.setConnectTimeout(connectTimeout);
    

    If the server fails to answer, there will be an exception.

    java.net.SocketTimeoutException
    



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2