Technical Summary of the android project: network connection Summary

Source: Internet
Author: User

This project uses httpclient to connect the client to the server over the network. I will summarize the network of the client sending requests.

 

 

For example.

Note:

1. Various requests here represent logon requests, task requests, and other URLs.

 

We can see that the most important part of the entire network is the serverutil and httputil.CodeAs follows:

Serverutil:

/*** Core network communication class * @ author guxuede **/public class serverutil {Private Static string hosturl/* = "http: // 192.168.1.56: 8080/crmserver "*/; private Static defaulthttpclient httpclient; Private Static int connectiontimeout = 5; // connection timeout Private Static int readtimeout = 5; // read timeout/*** initialize ipport * @ Param IP * @ Param port */public static void initipport (string IP, string port) {hosturl = "http: // "+ IP +": "+ port +"/crmserver "; clientservicefactory. inituri ();}/*** initialize httpclient */public static void inithttpclient () {httpparams Params = new basichttpparams (); httpprotocolparams. setcontentcharset (Params, "UTF-8"); httpprotocolparams. sethttpelementcharset (Params, "UTF-8"); httpprotocolparams. setversion (Params, httpversion. http_1_1); httpprotocolparams. setuseragent (Params, "httpcomponents/1.1"); httpconnectionparams. setconnectiontimeout (Params, connectiontimeout * 1000); httpconnectionparams. setsotimeout (Params, readtimeout * 1000); schemeregistry = new schemeregistry (); schemeregistry. register (New Scheme ("HTTP", plainsocketfactory. getsocketfactory (), 80); schemeregistry. register (New Scheme ("HTTPS", sslsocketfactory. getsocketfactory (), 443); clientconnectionmanager connectionmanager = new callback (Params, schemeregistry); httpclient = new defaulthttpclient (connectionmanager, Params);} public static httpclient gethttpclient () {return httpclient;} public static string gethosturl () {return hosturl ;}}

httputil:

Public class httputil {Private Static final string tag = activityutil. gettag (httputil. class);/*** this method can convert all attributes and their values of an object into key-value pairs. * Use the attribute name key and the attribute value as value, and store it in namevaluepair. * If the value is null, it will not be converted, and the first attribute of a class will not be converted (because many classes inherit serializable and do not want to be converted to serialversionuid ); * therefore, the class to be converted must implement serializable and set the first position of serialversionuid in the class attribute. * Parameter L. Refers to the number of parent classes that need to be recursion up. * L = 1: only convert yourself * l = 2: Convert yourself and the parent class. * @ Param OBJ the object to be converted * @ Param L = 1: only convert itself L = 2: convert itself and its parent class. Similarly, */public static void objecttonamevaluepairs (serializable OBJ, int L, list <namevaluepair> Params) {If (OBJ = NULL) return; Class <?> Clss = obj. getclass (); For (INT I = 0; I <L; I ++) {if (I> 0) {clss = (class <?>) Clss. getgenericsuperclass ();} field [] fields = clss. getdeclaredfields (); If (fields. length <2) {continue;} For (Int J = 1; j <fields. length; j ++) {fields [J]. setaccessible (true); object o = NULL; try {o = Fields [J]. get (OBJ);} catch (illegalargumentexception E1) {log. W (TAG, E1);} catch (illegalaccessexception E2) {log. W (TAG, E2);} If (o! = NULL) {Params. add (New basicnamevaluepair (fields [J]. getname (), O. tostring ();} fields [J]. setaccessible (false) ;}}/ *** sends the POST method to the specified Uri. Converts the returned information to an object. * An interactionexception is thrown. * msg_res_id = 1 request refused * msg_res_id = 2 Request timeout * msg_res_id = 3 Replies can not be resolved * msg_res_id = 100> HTTP access server exception * msg_res_id = 1001 No Logon or session failure * msg_res_id = 1002 Incorrect Parameter causes server exception (SQL exception, null Pointer) * @ Param urihosturi * @ Param Params additional parameter * @ return converted object * @ throws interactionexception */public static object executepost (string Uri, list <namevaluepair> Pa Rams) throws interactionexception {httpclient httpclinet = serverutil. gethttpclient (); httppost post = new httppost (URI); try {If (Params! = NULL) {post. setentity (New urlencodedformentity (Params, HTTP. utf_8);} catch (unsupportedencodingexception E1) {log. W (TAG, E1) ;}httpresponse res = NULL; try {long start = system. currenttimemillis (); log. V ("Post", "Start: [URI:" + URI + "] [Params:" + Params + "]"); Res = httpclinet.exe cute (post); log. V ("Post", "Over: [URI:" + URI + "] [cost time:" + (system. currenttimemillis ()-Start) + "]");} catch (exception e) {log. W (tag, E); // very likely timeout if (E instanceof httphostconnectexception) {Throw new interactionexception ("Request refused", E, 1 );} else {Throw new interactionexception ("request timeout", E, 2) ;}} if (res. getstatusline (). getstatuscode () = httpstatus. SC _ OK) {header headercode = res. getfirstheader ("resultcode"); If (headercode! = NULL & headercode. getvalue ()! = NULL) {int mresultcode = integer. parseint (headercode. getvalue (); header reason = res. getfirstheader ("reason"); If (reason! = NULL & reason. getvalue ()! = NULL) {// obtain the exception actively thrown by the server from the return header. Throw new interactionexception (reason. getvalue (), mresultcode) ;}} objectinputstream OIS = NULL; try {OIS = new objectinputstream (res. getentity (). getcontent (); object OBJ = Ois. readobject (); Return OBJ;} catch (exception e) {log. W (TAG, e); // The server returns the incomprehensible content throw new interactionexception ("replies can not be resolved", E, 3 );} finally {If (OIS! = NULL) Try {Ois. close ();} catch (ioexception e) {log. W (TAG, e) ;}}else {// server exception throw new interactionexception ("request failure, server exception", Res. getstatusline (). getstatuscode ());}}}

not complete...

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.