Tôi biết vấn đề này nên được khắc phục với System.setProperty ("http.keepAlive", "false"); trước openConnection, nhưng điều đó không hiệu quả với tôi. Trước tiên hãy thử trên mã này hoạt động, thứ hai không thành công. Ngay cả khi tôi thử yêu cầu này sau ít hơn 5 giây, nó cũng hoạt động. Nếu tôi chờ đợi nhiều hơn thế, nó thất bại một lần nữaHttpUrlConnection.openConnection thất bại lần thứ hai
Đây là mã của tôi:
System.setProperty("http.keepAlive", "false");
HttpURLConnection conn = (HttpURLConnection) mURL.openConnection();
conn.setUseCaches(false);
conn.setRequestProperty("Connection","Keep-Alive");
conn.setRequestProperty("User-Agent", useragent);
conn.setConnectTimeout (30000) ;
conn.setDoOutput(true);
conn.setDoInput(true);
consumer.sign(conn);
InputSource is = new InputSource(conn.getInputStream());
tôi nhận được ngoại lệ trên dòng cuối cùng:
java.io.IOException: Write error: I/O error during system call, Broken pipe
W/System.err(2164): at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.nativewrite(Native Method)
W/System.err(2164): at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.access$600(OpenSSLSocketImpl.java:55)
W/System.err(2164): at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl$SSLOutputStream.write(OpenSSLSocketImpl.java:583)
W/System.err(2164): at java.io.OutputStream.write(OutputStream.java:82)
W/System.err(2164): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.sendRequest(HttpURLConnectionImpl.java:1332)
W/System.err(2164): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.doRequestInternal(HttpURLConnectionImpl.java:1656)
W/System.err(2164): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.doRequest(HttpURLConnectionImpl.java:1649)
W/System.err(2164): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:1153)
W/System.err(2164): at org.apache.harmony.luni.internal.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:253)
Liệu ai đó có một ý tưởng về những gì sai ở đây ? Cảm ơn!
google bây giờ đề xuất sử dụng HttpUrlConnection (bắt đầu từ Gingerbread) vì một vài lỗi đó đã được sửa: http: // android-developers.blogspot.com/2011/09/androids-http-clients.html –
Tại sao giải pháp này giải quyết được sự cố? –
Sớm hay muộn, đặc biệt là với Android M đang phát triển, HttpClient đang trở nên không được chấp nhận hiệu quả. URLConnection được đề xuất cho các ứng dụng có thời lượng sử dụng dài. – Josh