Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/188.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android-设置otimeout不工作_Android_Android Asynctask_Httpconnection_Socket Timeout Exception - Fatal编程技术网

Android-设置otimeout不工作

Android-设置otimeout不工作,android,android-asynctask,httpconnection,socket-timeout-exception,Android,Android Asynctask,Httpconnection,Socket Timeout Exception,所以我遇到了一个无法工作的套接字超时。我遵循了现有帖子给出的所有说明,但仍然不起作用(我从未收到套接字超时异常)。这是我的密码: AsyncTask<String, Void, String> task = new AsyncTask<String, Void, String>() { @Override protected String doInBackground(String... params) { String location = pa

所以我遇到了一个无法工作的套接字超时。我遵循了现有帖子给出的所有说明,但仍然不起作用(我从未收到套接字超时异常)。这是我的密码:

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);
AsyncTask task=new AsyncTask(){
@凌驾
受保护的字符串doInBackground(字符串…参数){
字符串位置=参数[0];
试一试{
HttpGet HttpGet=新的HttpGet(位置);
HttpParams httpParameters=新的BasicHttpParams();
//以毫秒为单位设置超时,直到建立连接。
//默认值为零,表示不使用超时。
int timeoutConnection=0;
HttpConnectionParams.setConnectionTimeout(httpParameters,
超时连接);
//设置默认套接字超时(SO\U超时)
//以毫秒为单位,这是等待数据的超时。
int timeoutSocket=2000;
HttpConnectionParams.setSoTimeout(httpParameters,timeoutSocket);
DefaultHttpClient客户端=新的DefaultHttpClient(httpParameters);
Log.d(this.getClass().getSimpleName(),“启动客户端!!!”;
HttpResponse response=client.execute(httpGet);
Log.d(this.getClass().getSimpleName(),“客户端已取消!!!”;
if(statusLine.getStatusCode()==HttpStatus.SC\u OK){
ByteArrayOutputStream out=新建ByteArrayOutputStream();
response.getEntity().writeTo(out);
返回新扫描仪(out.toString()).useDelimiter(\\A”).next();
}否则{
返回“”;
}
}捕获(IOE异常){
e、 printStackTrace();
返回null;
}捕获(URISyntaxException e){
e、 printStackTrace();
返回null;
}
}
@凌驾
受保护的void onPostExecute(字符串结果){
试一试{
//多斯塔夫
}捕获(例外e){
e、 printStackTrace();
}
}
};
任务。执行(位置);
所以我应该在两秒钟后得到一个套接字超时异常,但是客户端只是忽略了它。有什么帮助吗

提前谢谢


下面是所有代码:

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);
}
public void fetch(字符串位置){
AsyncTask任务=新建AsyncTask(){
@凌驾
受保护的字符串doInBackground(字符串…参数){
字符串位置=参数[0];
试一试{
HttpGet HttpGet=新的HttpGet(位置);
HttpParams httpParameters=新的BasicHttpParams();
//以毫秒为单位设置超时,直到建立连接。
//默认值为零,表示不使用超时。
int timeoutConnection=0;
HttpConnectionParams.setConnectionTimeout(httpParameters,timeoutConnection);
//设置默认套接字超时(SO\U超时)
//以毫秒为单位,这是等待数据的超时。
int timeoutSocket=2000;
HttpConnectionParams.setSoTimeout(httpParameters,timeoutSocket);
DefaultHttpClient客户端=新的DefaultHttpClient(httpParameters);
Log.d(this.getClass().getSimpleName(),“启动客户端!!!”;
HttpResponse response=client.execute(httpGet);
Log.d(this.getClass().getSimpleName(),“客户端已取消!!!”;
if(statusLine.getStatusCode()==HttpStatus.SC\u OK){
ByteArrayOutputStream out=新建ByteArrayOutputStream();
response.getEntity().writeTo(out);
返回新扫描仪(out.toString()).useDelimiter(\\A”).next();
}否则{
返回“”;
}
}捕获(IOE异常){
e、 printStackTrace();
返回null;
}捕获(URISyntaxException e){
e、 printStackTrace();
返回null;
}
}
@凌驾
受保护的void onPostExecute(字符串结果){
试一试{
//多斯塔夫
}
}捕获(例外e){
e、 printStackTrace();
}
}
};
任务。执行(位置);
}

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

如果要设置任何现有HTTPClient(例如DefaultHttpClient或AndroidHttpClient)的参数,可以使用函数setParams()


httpClient.setParams(httpParameters)

不久前,我也遇到过类似的问题。我做了一些实验,发现当我在模拟器中运行我的应用程序时,超时不起作用,而当我在真实设备上运行它时,超时确实起作用。我已经用
DefaultHttpClient
HttpUrlConnection
AndroidHttpClient
对它进行了测试,这三种方法都显示了相同的结果;在模拟器中大约20秒后发生IOexception(UnknownHosteException),无论设置的超时如何

谷歌透露,其他人也报告了超时问题:

所有建议的解决方案都不适用于我,因此我想唯一可靠的解决方案是自己管理超时。

请参阅:

问题可能在Apache HTTP客户端中。参见HTTPCLIENT-1098。在4.1.2中固定


出于日志记录的目的,超时异常尝试反向DNS IP。这需要额外的时间,直到异常被实际触发。

hi@user1416721我添加了新代码。它对我有用。我两天前用过。很好。使用它并让我知道。不,仍然不起作用。客户端在5秒内开始执行并且从未完成,并且没有抛出SocketTimeoutException。。。也许它与我在doInBackground中使用它作为异步任务有关?
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);