Solution to "socketexception: broken pipe" appears when Android uses httpclient

Source: Internet
Author: User
Cause analysis:

1. The connection between the client and the server has been closed (it may be the client or the server, usually the client closes it), and the client continues to write data to the server;

2. It is easy to use threadsafeconnectionmanager or poolconnectionmanger of httpclient, because we set the timeout time for the connection to obtain data;

Solution:

1. Add retry handler for your httpclient, and the code is as follows:

         HttpRequestRetryHandler retryHandler = new HttpRequestRetryHandler() {            @Override            public boolean retryRequest(IOException arg0, int arg1, HttpContext arg2) {                // retry a max of 5 times                if (arg1 >= 3) {                    return false;                }                if (arg0 instanceof ch.boye.httpclientandroidlib.NoHttpResponseException) {                    return true;                } else if (arg0 instanceof ch.boye.httpclientandroidlib.client.ClientProtocolException) {                    return true;                }                return false;            }        };        sHttpClient.setHttpRequestRetryHandler(retryHandler);

2. Handle socketexception:

    InputStream in = null;    try {        final HttpResponse response = HttpManager.execute(context, post);        final int statusCode = response.getStatusLine().getStatusCode();        if (statusCode == HttpStatus.SC_OK) {            HttpEntity entity = response.getEntity();            if (entity != null) {                in = entity.getContent();                return IOUtils.stream2String(in);            }        } else {            post.abort();            mLog.error("http code: " + response.getStatusLine().getStatusCode());       }    } catch (IOException ex) {        post.abort();    } catch (RuntimeException ex) {        post.abort();        throw ex;    } finally {        IOUtils.closeStream(in);    }

Socketexcption is a subclass of ioexception. When I/O exceptions are found, the connection is closed and httpclient is used to retry the connection;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.