質問

私が望んでいる誰かが私は、断続的な接続で私を助けることができます HttpsURLConnectionはとコードを使用して取得。私が使用しているコードがあります 下記ます:

HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
            conn.setReadTimeout(10 * 1000); 
if conn.getResponseCode() != 200) { 
            Log.v(TAG, "error code:" + conn.getResponseCode()); 
} 
私が引っ張ってそれを使用する場合、接続が初めてのたびに動作します JSONファイル。しかし、私はコマンドを送信するために、再度接続を使用する場合、 それは常に最初に失敗しました。私が送信した場合には、その後、通常動作します すぐにコマンド(5秒以内)が、私はしばらく待っていれば失敗します。 それは最初の時間を接続しているので、私はそのA SSLの問題を考えていません。 正しく、私はここで間違っている可能性があります。私はまた、多くの異なっを試してみました そのような追加などのバリエーション

conn.setUseCaches(false); 
conn.setRequestProperty("Connection","Keep-Alive"); 
conn.getHostnameVerifier(); 
conn.getSSLSocketFactory(); 
conn.setDoOutput(true); 
conn.setDoInput(true); 
conn.setRequestMethod("POST"); 
conn.wait(100); 
しかし、私は運がなかったです。任意の助けいただければ幸いです。

役に立ちましたか?

解決

あなたDO

System.setProperty("http.keepAlive", "false");をお試しください
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();

他のヒント

このコードを試してみてください - それは私のためにかなり確実に動作ます:

public static final String USER_AGENT = "Mozilla/5.0 (Linux; U; Android 1.1; en-us;dream) AppleWebKit/525.10+ (KHTML, like Gecko) Version/3.0.4 Mobile Safari/523.12.2";
private DefaultHttpClient getThreadSafeHttpClient() {
    final HttpParams params = new BasicHttpParams();
    params.setParameter("http.useragent", USER_AGENT);
    HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
    HttpProtocolParams.setContentCharset(params, "UTF-8");
    final SchemeRegistry registry = new SchemeRegistry();
    registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    final SSLSocketFactory sslSocketFactory = SSLSocketFactory.getSocketFactory();
    sslSocketFactory.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
    registry.register(new Scheme("https", sslSocketFactory, 443));
    final ThreadSafeClientConnManager manager = new ThreadSafeClientConnManager(params, registry);
    final DefaultHttpClient httpclient = new DefaultHttpClient(manager, params);
    // how to handle retries
    final HttpRequestRetryHandler myRetryHandler = new HttpRequestRetryHandler() {
        public boolean retryRequest(final IOException exception, final int executionCount, final HttpContext context) {
            if (executionCount >= 5) {
                // Do not retry if over max retry count
                return false;
            }
            if (exception instanceof NoHttpResponseException) {
                // Retry if the server dropped connection on us
                return true;
            }
            if (exception instanceof SSLHandshakeException) {
                // Do not retry on SSL handshake exception
                return false;
            }
            final HttpRequest request = (HttpRequest) context.getAttribute(ExecutionContext.HTTP_REQUEST);
            final boolean idempotent = !(request instanceof HttpEntityEnclosingRequest);
            if (idempotent) {
                // Retry if the request is considered idempotent
                return true;
            }
            return false;
        }

    };
    httpclient.setHttpRequestRetryHandler(myRetryHandler);
    return httpclient;
}

上記m6ttの答えに少しだけ加えます:

private static void disableConnectionReuseIfNecessary() {
    // HTTP connection reuse which was buggy pre-froyo
    if (!Constants.SUPPORTS_FROYO) {
        System.setProperty("http.keepAlive", "false");
    }
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top