-->

Android的 - setSoTimeout不工作(Android - setSoTimeout

2019-06-24 19:06发布

所以,我正在为没有工作套接字超时。 我接着现有职位人员的指示,但它仍然没有工作(我从来没有一个插座超时除外)。 这里是我的代码:

AsyncTask<String, Void, String> task = new AsyncTask<String, Void, String>() {
   @Override
   protected String doInBackground(String... params) {
      String location = params[0];

      try {
         HttpGet httpGet = new HttpGet(location);
         HttpParams httpParameters = new BasicHttpParams();

         // Set the timeout in milliseconds until a connection is established.
         // The default value is zero, that means the timeout is not used.
         int timeoutConnection = 0;
         HttpConnectionParams.setConnectionTimeout(httpParameters,
               timeoutConnection);

         // Set the default socket timeout (SO_TIMEOUT)
         // in milliseconds which is the timeout for waiting for data.
         int timeoutSocket = 2000;
         HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);

         DefaultHttpClient client = new DefaultHttpClient(httpParameters);
         Log.d(this.getClass().getSimpleName(), "STARTING CLIENT!!! ");
         HttpResponse response = client.execute(httpGet);
         Log.d(this.getClass().getSimpleName(), "CLIENT CANCELLED!!! ");

         if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            response.getEntity().writeTo(out);
            return new Scanner(out.toString()).useDelimiter("\\A").next();
         } else {
            return "";
         }
      } catch (IOException e) {
         e.printStackTrace();
         return null;
      } catch (URISyntaxException e) {
         e.printStackTrace();
         return null;
      }
   }

   @Override
   protected void onPostExecute(String result) {
      try {
         // doStuff
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
};
task.execute(location);

所以我应该在两秒钟后获取套接字超时异常,但客户端只是忽略了。 任何帮助吗?

提前致谢


因此,这里是所有的代码:

public void fetch(String location) {    
    AsyncTask<String, Void, String> task = new AsyncTask<String, Void, String>() {
        @Override
        protected String doInBackground(String... params) {
            String location = params[0];

            try {
                HttpGet httpGet = new HttpGet(location);
                HttpParams httpParameters = new BasicHttpParams();
                // Set the timeout in milliseconds until a connection is established.
                // The default value is zero, that means the timeout is not used. 
                int timeoutConnection = 0;
                HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
                // Set the default socket timeout (SO_TIMEOUT) 
                // in milliseconds which is the timeout for waiting for data.
                int timeoutSocket = 2000;
                HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);

                DefaultHttpClient client = new DefaultHttpClient(httpParameters);
                Log.d(this.getClass().getSimpleName(), "STARTING CLIENT!!! ");
                HttpResponse response = client.execute(httpGet);
                Log.d(this.getClass().getSimpleName(), "CLIENT CANCELLED!!! ");

                if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
                    ByteArrayOutputStream out = new ByteArrayOutputStream();
                    response.getEntity().writeTo(out);
                    return new Scanner(out.toString()).useDelimiter("\\A").next();
                } else {
                    return "";
                }
            } catch (IOException e) {
                e.printStackTrace();
                return null;
            } catch (URISyntaxException e) {
                e.printStackTrace();
                return null;
            }
        }

        @Override
        protected void onPostExecute(String result) {
            try {
                //doStuff
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };
    task.execute(location);
}

Answer 1:

A while back I had similar problems. I've experimented a bit and noticed that when I ran my app in the emulator the timeouts didn't work and when I ran it on a real device it did work. I've tested it with the DefaultHttpClient, HttpUrlConnection and AndroidHttpClient, all three showed the same results; an IOexception (UnknownHostException) after about 20 seconds in the emulator regardless of any set timeout.

Googling revealed that other people have also reported problems with timeout:

  • http://code.google.com/p/android/issues/detail?id=2409
  • Http connection timeout on Android not working
  • http connection timeout issues
  • Socket timeout when making HTTPGet requests using DefaultHTTPClient

None of the proposed solutions worked for me, so I guess the only reliable solution is to manage timeouts yourself.



Answer 2:

在我的例如两个超时设置。 连接超时抛出“java.net.SocketTimeoutException:套接字未连接”和套接字超时“java.net.SocketTimeoutException:操作超时”。

HttpGet httpGet = new HttpGet(url);
HttpParams httpParameters = new BasicHttpParams();
// Set the timeout in milliseconds until a connection is established.
// The default value is zero, that means the timeout is not used. 
int timeoutConnection = 3000;
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
// Set the default socket timeout (SO_TIMEOUT) 
// in milliseconds which is the timeout for waiting for data.
int timeoutSocket = 5000;
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);

DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);
HttpResponse response = httpClient.execute(httpGet);

如果你想设置的任何现有了HTTPClient(如DefaultHttpClient或AndroidHttpClient)的参数,你可以使用函数setParams()获取。

httpClient.setParams(httpParameters);



Answer 3:

请参阅: https://stackoverflow.com/a/20031077/2609238

这个问题可能是在Apache HTTP客户端。 参见HttpClient的-1098。 修正了4.1.2。

超时异常尝试反向DNS的IP,用于日志记录。 这需要额外的时间,直到例外,实际上是被解雇。



文章来源: Android - setSoTimeout not working