setConnection超时无效

时间:2017-10-05 07:32:51

标签: java android

网址连接代码

   URL urlToRequest = new URL(URL);
    urlConnection = (HttpURLConnection)urlToRequest.openConnection();
    urlConnection.setRequestProperty("Accept", "application/xml");
    urlConnection.setRequestProperty("Content-Type", "application/xml");
    urlConnection.setRequestProperty ("Authorization",getB64Auth("admin","admin"));
    urlConnection.setConnectTimeout(CONNECTION_TIMEOUT);
  //  urlConnection.setReadTimeout(CONNECTION_TIMEOUT_SOCKET);
    InputStream in = new BufferedInputStream(urlConnection.getInputStream());
    String response = readStream(in);
    return response;

setConnectTimeout()功能不起作用。我需要解决方案,如何设置connection TimeOut

1 个答案:

答案 0 :(得分:0)

尝试读取超时。

urlConnection.setReadTimeout(READ_TIMEOUT);

您是否正在捕获SocketTimeOut和ConnectionTimout异常?