When I used to create an Android mobile project, I wanted to add request timeout and read timeout for the httpclient object. I found that the defaulthttpclient object implemented by the httpclient object has no timeout method, so I was always depressed. Why didn't I find it? Does the defaulthttpclient object have encapsulated the request timeout and read time? But this is not a poor expansion. Recently, on the android Forum, I found that the httpclient of Android is actually usable, but it is set in the static method of httpconnectionparams. I wrote a section in detailCodeProvided to you:
Private Static final int request_timeout = 10*1000; // set the request timeout value to 10 seconds. Private Static final int so_timeout = 10*1000; // set the wait data timeout time 10 seconds/*** Add the request timeout time and wait time * @ author spring sky * Email vipa1888@163.com * QQ: 840950105 * my name: shi mingzheng * @ return httpclient object */Public httpclient gethttpclient () {basichttpparams httpparams = new basichttpparams (); httpconnectionparams. setconnectiontimeout (httpparams, request_timeout); httpconnectionparams. setsotimeout (httpparams, so_timeout); httpclient client = new defaulthttpclient (httpparams); Return client ;}
The above method can be shared for each request and will be easy to maintain in the future! I personally suggest writing this method to the parent class so that the subclass can inherit. This is in line with object-oriented programming!
Learning lies in accumulation!