1

そのため、ソケットのタイムアウトが機能していません。既存の投稿で与えられたすべての指示に従いましたが、まだ機能していません (ソケット タイムアウト例外は発生しません)。これが私のコードです:

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

したがって、2 秒後にソケット タイムアウト例外が発生するはずですが、クライアントはそれを無視しています。何か助けはありますか?

前もって感謝します


したがって、ここにすべてのコードがあります:

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

3 に答える 3

2

しばらく前、私は同様の問題を抱えていました。少し実験してみましたが、エミュレーターでアプリを実行するとタイムアウトが機能せず、実際のデバイスで実行すると機能することに気付きました。私はそれをでテストしましたDefaultHttpClientHttpUrlConnectionそしてAndroidHttpClient、3つすべてが同じ結果を示しました。設定されたタイムアウトに関係なく、エミュレータで約20秒後のIOexception(UnknownHostException)。

グーグルは、他の人々もタイムアウトの問題を報告していることを明らかにしました:

提案された解決策はどれも私にはうまくいきませんでした。したがって、信頼できる解決策はタイムアウトを自分で管理することだけだと思います。

于 2012-05-30T08:19:15.903 に答える
0

参照: https://stackoverflow.com/a/20031077/2609238

問題は Apache HTTP クライアントにある可能性があります。HTTPCLIENT-1098 を参照してください。4.1.2 で修正されました。

タイムアウト例外は、ログ記録のために、DNS と IP を逆にしようとします。これには、例外が実際に発生するまでさらに時間がかかります。

于 2013-11-17T13:23:50.010 に答える
0

私の例では、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);

于 2012-05-25T07:04:51.353 に答える