Android的 - setSoTimeout不工作 [英] Android - setSoTimeout not working

查看:258
本文介绍了Android的 - setSoTimeout不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我遇到一个不工作套接字超时。我跟着现有职位人员的指示,但它仍然没有工作(我从来没有得到一个套接字超时除外)。这是我的code:

 的AsyncTask<字符串,太虚,字符串>任务=新的AsyncTask<字符串,太虚,字符串>(){
   @覆盖
   保护字符串doInBackground(字符串... PARAMS){
      字符串位置= PARAMS [0];

      尝试 {
         HTTPGET HTTPGET =新HTTPGET(位置);
         的HttpParams httpParameters =新BasicHttpParams();

         //毫秒设置超时,直到建立了连接。
         //默认值是零,这意味着在超时不被使用。
         INT timeoutConnection = 0;
         HttpConnectionParams.setConnectionTimeout(httpParameters,
               timeoutConnection);

         //设置默认套接字超时(SO_TIMEOUT)
         //毫秒这是超时等待数据。
         INT timeoutSocket = 2000;
         HttpConnectionParams.setSoTimeout(httpParameters,timeoutSocket);

         DefaultHttpClient客户端=新DefaultHttpClient(httpParameters);
         Log.d(this.getClass()getSimpleName(),启动客户端!!!);
         HTT presponse响应= client.execute(HTTPGET);
         Log.d(this.getClass()getSimpleName(),客户端CANCELLED !!!);

         如果(statusLine.getStatus code()== HttpStatus.SC_OK){
            ByteArrayOutputStream OUT =新ByteArrayOutputStream();
            。response.getEntity()的writeTo(出);
            返回新的扫描仪(out.toString())useDelimiter(\\ A)下一个()。
         } 其他 {
            返回 ;
         }
      }赶上(IOException异常E){
         e.printStackTrace();
         返回null;
      }赶上(的URISyntaxException E){
         e.printStackTrace();
         返回null;
      }
   }

   @覆盖
   保护无效onPostExecute(字符串结果){
      尝试 {
         // doStuff
      }赶上(例外五){
         e.printStackTrace();
      }
   }
};
task.execute(位置);
 

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

在此先感谢


因此​​,这里是所有的code:

 公共无效获取(字符串位置){
    AsyncTask的<字符串,太虚,字符串>任务=新的AsyncTask<字符串,太虚,字符串>(){
        @覆盖
        保护字符串doInBackground(字符串... PARAMS){
            字符串位置= PARAMS [0];

            尝试 {
                HTTPGET HTTPGET =新HTTPGET(位置);
                的HttpParams httpParameters =新BasicHttpParams();
                //毫秒设置超时,直到建立了连接。
                //默认值是零,这意味着在超时不被使用。
                INT timeoutConnection = 0;
                HttpConnectionParams.setConnectionTimeout(httpParameters,timeoutConnection);
                //设置默认套接字超时(SO_TIMEOUT)
                //毫秒这是超时等待数据。
                INT timeoutSocket = 2000;
                HttpConnectionParams.setSoTimeout(httpParameters,timeoutSocket);

                DefaultHttpClient客户端=新DefaultHttpClient(httpParameters);
                Log.d(this.getClass()getSimpleName(),启动客户端!!!);
                HTT presponse响应= client.execute(HTTPGET);
                Log.d(this.getClass()getSimpleName(),客户端CANCELLED !!!);

                如果(statusLine.getStatus code()== HttpStatus.SC_OK){
                    ByteArrayOutputStream OUT =新ByteArrayOutputStream();
                    。response.getEntity()的writeTo(出);
                    返回新的扫描仪(out.toString())useDelimiter(\\ A)下一个()。
                } 其他 {
                    返回 ;
                }
            }赶上(IOException异常E){
                e.printStackTrace();
                返回null;
            }赶上(的URISyntaxException E){
                e.printStackTrace();
                返回null;
            }
        }

        @覆盖
        保护无效onPostExecute(字符串结果){
            尝试 {
                // doStuff
                }
            }赶上(例外五){
                e.printStackTrace();
            }
        }
    };
    task.execute(位置);
}
 

解决方案

前阵子我有类似的问题。我已经试验了一下,发现我跑了模拟器我的应用程序超时没有工作,当我跑了一个真实的设备就可以做的工作。我与 DefaultHttpClient 的HttpURLConnection AndroidHttpClient ,所有三个显示出相同的结果;一个IOException异常(UnknownHostException异常)后,在模拟器中大约20秒,无论任何设置超时时间。

谷歌搜索显示,其他人也报告的问题与超时:

没有一个提出的解决方案为我工作,所以我想的唯一可靠的解决办法是自己管理超时。

So I'm running into a not working socket timeout. I followed all instructions given by existing posts, but its still not working (I never get a socket timeout exception). Here is my code:

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);

So I should get a socket timeout exception after two seconds, but the client is just ignoring that. Any help?

Thanks in advance


So here is all of the code:

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);
}

解决方案

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:

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

这篇关于Android的 - setSoTimeout不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆