The analysis of common anomalies in the light network---of giving HttpClient and HttpURLConnection

Source: Internet
Author: User

Reprint Annotated Source:http://blog.csdn.net/codingandroid/article/details/41749581

In the previous article used in the Asyncbaserequest class, which throws a lot of exceptions, of course, there are some comments, then let us analyze these anomalies, as well as the occurrence of abnormal conditions


Connecttimeoutexception This is a connection exception (such as not open the network at all), the data has not reached the server, so it is not related to the Order class, you can resubmit

This situation is better handled, that is, the request is not to the server, if the best way to simulate this error is that you open the LAN service, the phone to 3g mode, or fill in a nonexistent service address. Of course, the actual situation may also occur, the signal is very poor condition, this situation, the prompt timeout, let the user again to OK.


Sockettimeoutexception This timeout exception is to indicate that the request has reached the server, the return data process timed out, if the order class request to pay attention to, need to prevent duplicate submission

This anomaly occurs if your timeout is 1s, when the data arrives at the server, server 1s has not been able to return the data smoothly, this time has reached the client set timeout, then, the request is timed out, but note that your request at this time has mentioned the server, Although your local exception has been thrown, the server is still processing, if you are going to modify a field, the server may have been modified successfully, and you see the exception, this time can be the timeout to zoom a bit, or the server processing data speed need to optimize


TimeoutException This is the first two of the parent class that is, if you are not very strict requirements, you can ignore the above two, directly capture this, of course, as a framework, I would like to be more detailed, or directly capture a exception on the finished son, of course, captured the first two, We're also worried about other timeout situations, so we're going to catch them again, just in case.

Httphostconnectexception This exception is not said, usually you do not have to add permission


Nohttpresponseexception This is defaulthttpclient will appear, belonging to Android under the httpclient a flaw can refer to http://stackoverflow.com/ questions/2052299/httpclient-on-android-nohttpresponseexception-through-umts-3g but

HttpProtocolParams.setUseExpectContinue(httpClient.getParams(), false);
This I added, seemingly no use, and then the above method of retry seems to have no effect, interested friends can study Oh, I met just grab and throw a timeout exception out

Eofexception this anomaly on the headache, is httpurlconnection defect http://bbs.csdn.net/topics/390143086 This is CSDN's post and HTTP// Stackoverflow.com/questions/15411213/android-httpsurlconnection-eofexception almost all of the processing methods I have added, or the probability of the occurrence of this anomaly, that no matter, Throw timeout directly to fool users, but we developers have to understand.


IOException Stream Read the wrong time, then we think it is timed out and other exceptions will not go to the tube


Package Com.clxu.netframe.exception;import Java.io.ioexception;import Java.net.sockettimeoutexception;import Java.util.linkedlist;import Java.util.concurrent.timeoutexception;import Org.apache.http.conn.connecttimeoutexception;import Org.apache.http.conn.httphostconnectexception;import Android.util.log;import Com.clxu.netframe.r;import com.clxu.netframe.constant.constant;/** @ class Name: MyException * @ Function Description: TODO (Custom exception base class) * @ Creator: Clxu * @ created: 2014-5-28 morning 8:24:56 */public class MyException extends Exception {private stat    IC final long serialversionuid = 7803623314130181679L;    /** * ERROR Exception code */protected int errorCode =-1;    /** * Interface Exception message */private int msg;    /** * Developer View Simple exception information */protected int errormsg;        /** * Developer View Simple exception information */protected String errormsginfo;    /** * Raw Exception information */protected Exception oldexception; /** * Exception Chain (captures multiple exceptions thrown in one place) */protected linkedlist<exception> list = new Linkedlist<exceptioN> ();        Public myexception () {};        Public myexception (String message) {super (message);    Errormsginfo=message;        public myexception (int message) {this.setmsg (message);    this.errormsg = message;    };        public myexception (int message, int errorCode) {this.setmsg (message);        this.errormsg = message;    This.errorcode = ErrorCode;        };        public myexception (int message, int errorCode, int errormsg) {this.setmsg (message);        This.errormsg = errormsg;    This.errorcode = ErrorCode;    }; /** * should be new subclass, who uses who definition * @param e */Public myexception (Exception e) {if (e instanceof IOException)            {this.errorcode = Constant.network_request_ioexception_code;            This.setmsg (r.string.network_request_ioexception);        This.errormsg = r.string.network_request_ioexception; } else if (e instanceof sockettimeoutexception) {//timeout This.errorcode = ConStant.            Network_request_socket_exception;            This.setmsg (r.string.network_request_ioexception);        This.errormsg = r.string.network_request_ioexception; } else if (e instanceof connecttimeoutexception) {//timeout This.errorcode = constant.network_request_connect_except            ION;            This.setmsg (r.string.network_request_ioexception);        This.errormsg = r.string.network_request_ioexception;            } else if (e instanceof timeoutexception) {//timeout this.errorcode = constant.network_request_timeout_exception;            This.setmsg (r.string.network_request_ioexception);        This.errormsg = r.string.network_request_ioexception;             }else if (e instanceof httphostconnectexception) {this.errorcode = constant.network_request_perrmit_exception;            This.setmsg (r.string.network_request_ioexception);        This.errormsg = r.string.network_request_ioexception; }else if (e instanceof Exception) {this.erRorcode = constant.network_request_unknown_exception;            This.setmsg (r.string.system_exception);        This.errormsg = r.string.unknown_exception;        } this.oldexception = e;    Printmessage (e);    /** * Add exception to the exception chain * @param e */public void addexception (Exception e) {list.add (e);            /** * Get the lowest exception * @return */public Exception getfirstexception () {if (list.size () > 0)        Return List.get (0);    else return null;    /** * Print exception information */public void Printmessage (Exception e) {e.printstacktrace ();    }/** * Log exception information */public void LogMessage () {LOG.E ("Exception", Getdetailmessage ()); }/** * Get exception code (interface layer directly calls GetMessage or showmessage) * @return */public int geterrorcode () {return E    Rrorcode;    /** * Get simple exception information that developers see * @return */public int geterrormsg () {return errormsg; }    /**     *Get RAW Exception information * @return */public String getdetailmessage () {return oldexception.getmessage ();    }/** * Get the exception chain * @return */public linkedlist<exception> getList () {return list;    } public int getmsg () {return msg;    } public void setmsg (int msg) {this.msg = msg;    Public String Geterrormsginfo () {if (msg==0) {return errormsginfo;    }else{return "resource not found"; }    }}

The details of the article will say why there is nonohttpresponseexception eofexception These two exceptions, these two exceptions I was in the request network directly to deal with, caught after the direct throw Timeout

try {
Response = Client.execute (POST);
} catch (Nohttpresponseexception e) {
throw new TimeoutException ();
}


Oh, well, here's the exception.


Frame Source:http://download.csdn.net/detail/brightshadow11111/8228287

The analysis of common anomalies in the light network---of giving HttpClient and HttpURLConnection

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.