0

Android Gingerbread (OS 2.3.3) でアプリを実行すると、logcat に次のような警告が表示されることがあります。

java.io.IOException: No socket to write to; was a POST cached?
at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:618)
at com.voices.voices.webservice.WebService$WebServiceAsyncTask.downloadUrl(WebService.java:1705)
at com.voices.voices.webservice.WebService$WebServiceAsyncTask.doInBackground(WebService.java:1615)
at com.voices.voices.webservice.WebService$WebServiceAsyncTask.doInBackground(WebService.java:1)
at android.os.AsyncTask$2.call(AsyncTask.java:185)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
at java.util.concurrent.FutureTask.run(FutureTask.java:138)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
at java.lang.Thread.run(Thread.java:1019)

その呼び出しで HttpUrlConnection が失敗します。通常、この呼び出しは正常に機能しますが、失敗することもあります。Android OS > 2.3.3 で実行する場合、この問題なしで動作するようです。

問題は、なぜ失敗するのか、どうすれば修正できるのかということです。

追加情報は次のとおりです。

Webservice.java の 1705 行目

dataStream = new DataOutputStream(conn.getOutputStream());

エラーの前の行:

...
            HttpURLConnection conn = null;
            DataOutputStream dataStream = null;


            //SETS COOKIE This should avoid the "Too many redirects issue" because It's apparently redirecting in an infinite loop because it's not maintain the user session.
            CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL));


            if( webServiceUrl.contains("https") ){
                TrustManager[] trustAllCerts = new TrustManager[]{
                         new X509TrustManager() {
                             public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                                 return null;
                                 }
                             public void checkClientTrusted( java.security.cert.X509Certificate[] certs, String authType) {
                                 }
                             public void checkServerTrusted( java.security.cert.X509Certificate[] certs, String authType) {
                             }
                         }
                 }; // Install the all-trusting trust manager
                 try {
                     SSLContext sc = SSLContext.getInstance("TLS");
                     sc.init(null, trustAllCerts, new java.security.SecureRandom());
                     HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
                     }
                 catch (Exception e) {
                     //L.l("TRUST MANAGER EXCEPTION");
                 }
            }

            try {

                responseInputStream = null;

                System.setProperty("http.keepAlive", "false");
                URL url = new URL(webServiceUrl);

                conn = (HttpURLConnection) url.openConnection();

                conn.setReadTimeout(Constants.CONNECTION_TIMEOUT_MILLISECONDS /* milliseconds */);
                conn.setConnectTimeout(Constants.CONNECTION_TIMEOUT_MILLISECONDS /* milliseconds */);
                conn.setDoOutput(true);
                conn.setDoInput(true);
                conn.setDefaultUseCaches(false);

                if( useMultipart ){
                    conn.setRequestProperty("Content-Type", "multipart/form-data; boundary="+ Constants.MULTIPART_BOUNDARY);
                } else {
                    conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                }
                conn.setRequestProperty("Cache-Control", "no-cache");
                conn.connect();

                // DATA STREAM
                dataStream = new DataOutputStream(conn.getOutputStream());
  ...

これが役立つかどうかはわかりませんが、Android の HttpURLConnectionImpl.java ファイルでは、ここでエラーが発生します

@Override
public OutputStream getOutputStream() throws IOException {
    if (!doOutput) {
        throw new ProtocolException("Does not support output");
    }

    // you can't write after you read
    if (sentRequestHeaders) {
        // TODO: just return 'requestBodyOut' if that's non-null?
        throw new ProtocolException(
                "OutputStream unavailable because request headers have already been sent!");
    }

    if (requestBodyOut != null) {
        return requestBodyOut;
    }

    // they are requesting a stream to write to. This implies a POST method
    if (method == GET) {
        method = POST;
    }

    // If the request method is neither PUT or POST, then you're not writing
    if (method != PUT && method != POST) {
        throw new ProtocolException(method + " does not support writing");
    }

    int contentLength = -1;
    String contentLengthString = requestHeader.get("Content-Length");
    if (contentLengthString != null) {
        contentLength = Integer.parseInt(contentLengthString);
    }

    String encoding = requestHeader.get("Transfer-Encoding");
    if (chunkLength > 0 || "chunked".equalsIgnoreCase(encoding)) {
        sendChunked = true;
        contentLength = -1;
        if (chunkLength == -1) {
            chunkLength = DEFAULT_CHUNK_LENGTH;
        }
    }

    connect();

    if (socketOut == null) {
        // TODO: what should we do if a cached response exists?
        throw new IOException("No socket to write to; was a POST cached?");
    }

したがって、何らかの理由で socketOut == null、非常に奇妙です。

さらに情報が必要な場合はお知らせください。

4

1 に答える 1

0

これは最善の方法ではないかもしれませんが、私にとってはうまくいきます。httpURLConnectionのキャッシュを無効にする必要があります

conn.setDefaultUseCaches(false);
conn.setUseCaches(false);
conn.setRequestProperty("Expires", "-1");
conn.setRequestProperty("Cache-Control", "no-cache");
于 2013-02-20T16:45:53.530 に答える