In this project involves the text messaging and MMS functions, this is an important part of the project, once thought about how this function developed, for example, in our Yonghe system there are many orders, can give users to handle membership cards and other business how to let users know their own card in time consumption situation? How to let users have a sense of security, think of your bank card every time you spend or transfer, turn out every paragraph when there is a text message hint, you inadvertently added a sense of security?
We are thinking about the current types of portals, as well as our commonly used csdn and other forums are provided with SMS login, as well as SMS verification code, SMS registration and other equivalent SMS business content, all these are a thought to improve system flexibility, ease of use, security, user-friendly for users to consider, wholeheartedly serve the people , if the project embedded in the text message function undoubtedly increased the system's credit rating, but also to our system to increase users, learning everywhere for others to consider will make their own benefit, as the saying goes, "self-interest, people who are constant" means to do everything if only think of themselves, to their advantage, then you can live, But the benefit of others will make them long-term benefits.
Start to say: I am responsible for this part is to encapsulate the interface of the third party, the various configuration and other tools required by these interfaces are written as jar files, which part of our project used to send SMS MMS and other functions only need to introduce my jar package can be realized these functions are very convenient.
In turn think of our university platform inside can also join the text message function, student information has student mobile phone number, and so on test results come out to the students to send a message on the line, so it is not changed very simple, of course, SMS fee needs to let students themselves buckle, so many students send a lot of information.
Project manager gave me a text messaging service provider to provide a text API document, which is the bottom of a variety of methods from the Web site to find these documents from the Internet can be found, the author to is not difficult, focus on the logic and no bug, because it is the underlying things need to be very cautious, These are written to be provided to other people in the project to use and call the non-emergence of a little problem, in writing I also pay extra attention to add a lot of judgments and null pointers and other processing.
Design ideas, the spirit of reliable, flexible for the use of others for the purpose of convenience.
Invocation mode
WebService
Servers, and cross-platform, cross-language calls.
Https
The project uses this approach to communicate between the two servers using the URLConnection object, which is interesting to check.
Again, the experience and considerations for designing the underlying interface:
1. SMS server server address can be configured
The interface address of the service provider may change, the development test an interface system on-line is another interface, we in the development of this need to design a variable can not be written in the code, the first design I uninstalled the code inside this is not flexible, in fact, not only this, no matter what everyone is writing if it is easy to change, The amount of possible changes need to be written in the configuration file inside to avoid unnecessary trouble in the future, maintenance convenience no need to change the code.
2. Interface with different parameters for the same method
This has been realized in the university platform, the bottom provides a variety of interfaces is just an update to provide a variety of parameters, almost the update operation can call this method, which is convenient usability, the bottom part of the hair is provided to other, in life is the same as everyone likes to use simple things, too complicated to learn , take the user as a pig to treat.
3. Clear parameters and return values
The more the lower the more stringent, the more the show benefits, do not know that we have no interest in research open source framework, such as struts, SPRINGMVC, hibernate and so on, see how these frameworks bottom method is how to write, stand on the shoulders of giants will learn fast, stand high to see far, They are written in very detailed such as parameters, to see what type of name know what, when you enter the argument is not legitimate will be prompted, this is the benefits of their design, imagine you write your own method when someone else calls, if the parameters are not legal you will be prompted by what information? Think at least not now, especially in the multi-person cooperative development, if you do not do this, our cooperation version of the computer room system, our cooperation in the project, how to cooperate? There is no way to continue to develop, only you encounter what problem in person to find that write interface, writing methods of the people to face the face of the question to continue, this is obviously a bit against the cooperative development, contrary to the idea of OOP.
4. When the parameter is not normalized, immediately return to not be executed in the downward face
The underlying method call frequency may be quite high, do not cause unnecessary waste of content, when a method executes to each position in the middle, suddenly encountered a problem, need to immediately return the problem to the caller, no further to the surface execution, which is also a principle of the underlying programming in many frameworks also do so, We have all learned the principle of dealing with anomalies.
This principle is also the exception handling principle, when encountering the exception to remember processing and to the first level is called the method of the part thrown, there are exceptions need to deal with, do timely and efficient resolution.
Just like life, will inevitably encounter such or such problems, you are choosing to let the problem accumulate more or timely treatment, the truth is the same, the problem accumulated more can only bring more trouble, so that small problems into big problems most regret the bad solution, what problems need to be resolved at the beginning, I think of the Flat Magpie cure, Why is it that the flat magpie is very famous at that time because he can cure the serious disease, we all think that the Flat Magpie medicine is very clever. I wonder if you still know who is better than a flat magpie. At that time his elder brother, his elder brother cures at the beginning, in the sickness is not serious time to give other people cured this is the big wisdom, will solve the problem before the big, therefore, we encountered the problem may need in the initial solution, the timely solution.
5. Be careful about static variables
Do you really understand his principles? If a method uses a static method whose member variable is also a static variable, if the member variable of the underlying method has a value to be extra careful (except for the tool function), one time in my test, I called several times the underlying method, I wonder why the results are always the same, carefully check the underlying is the static method of the disaster, It has a static member variable, static is not modified at a later time is the value of the call, the use of static things at the end of no member variable or the member variable is not the time to use, otherwise it will cause unnecessary trouble.
The above are in the writing interface to pay attention to a bit of a problem, look at a send SMS interface
<span style= "FONT-SIZE:14PX;" >/** * @author Lilongsheng * @deprecated Send SMS method, can be sent periodically * @param mapcorpid: Account number, * PWD: Password, * Mobile: Contact Phone, * content: Send contents, * Cell: Sub-number, can be empty * sendtime: Timed send time, for example: 2006091215243 5 Delegate September 12, 2006 15:24 35 seconds * is empty, which means send immediately * * @return result string */public string Send (String Userurl, String corpid, String Pw D, string mobile,string content, String Cell, String sendtime) {//function returns the default value String result= "SMS send Failed";//SMS Content size if (!isempty ( Prosurl.getproperty ("Contentsize"))) {Contentsize=integer.parseint (Prosurl.getproperty ("contentSize"));} Determine if the user has provided the URL address if ("". Equals (Userurl) | | Null==userurl) {userurl=prosurl.getproperty ("Sendurl");} Store parameter information map<string,object> map=new hashmap<string, object> ();//Determine if the account is empty if (!isempty (corpid)) {Map.put (" Corpid ", corpid);} else {return result= "account cannot be empty";} Determine if the password is empty if (!isempty (pwd)) {map.put ("pwd", PWD);} else {return result= "password cannot be empty";} Determine if the phone format is correct if (!isempty (mobile) && Checkmobile (MOBILE) {Map.put ("mobile", mobile);} else {return result= "send mobile phone number is empty or malformed";} Determine if the text message content is empty, the size is appropriate if (!isempty (content) && content.length () < contentsize) {map.put ("content", content);} else {return result= "Send content is empty or content is too long";} The child number can be empty if (!isempty (cell)) {Map.put ("cell", cell);} The send time can be null if (!isempty (sendtime)) {map.put ("Sendtime", Sendtime);} try {//string strreg= ""; Call the underlying interface to send a text message//spell the parameter to the URL address after string Url_str=getuserurl (Userurl, map); System.out.println ("url_str=" +url_str); String Strreg=sendgetinfo (URL_STR); System.out.println ("strresult=" +strreg); if (!isempty (Strreg)) {//based on the underlying return value, returns the corresponding result if ("0". Equals (Strreg)) {return result= "send succeeded"; } if ("-1". Equals (Strreg)) {return result= "account not registered"; } if ("-2". Equals (Strreg)) {return result= "other error"; } if ("-3". Equals (Strreg)) {return result= "password error"; } if ("-4". Equals (Strreg)) {return result= "cell phone number is not formatted"; } if ("-5". Equals (Strreg)) {return result= "insufficient balance"; } if ("-6". Equals (STRR(eg)) {return result= "timed send time is not a valid time format"; } if ("-7". Equals (Strreg)) {return result= "prohibits sending the same text message to the same phone number within 10 hours"; } if (" -100". Equals (Strreg)) {return result= "restricts this IP access"; } if (" -101". Equals (Strreg)) {return result= "call interface too fast"; }}}catch (Exception e) {e.printstacktrace ();} return result;} </span>
Public methods for encapsulation
<span style= "FONT-SIZE:14PX;" >/** * @author Lilongsheng * @param userurl * @param maps * @return */private string Getuserurl (String userurl,map<s Tring, object> maps) {//Gets the parameters in the map and stitching them into URL parameters StringBuilder sb = new StringBuilder (); Sb.append (Userurl); set<string> keys = Maps.keyset (); for (final String key:keys) {Object value = Maps.get (key);//Determine if the parameter is empty if (! "". Equals (value) && null!=value) {sb.append (key);//cannot contain special characters sb.append (' = '); Sb.append (value); Sb.append (' & ') );}} Sb.deletecharat (Sb.length ()-1); return sb.tostring ();} </span>
<span style= "FONT-SIZE:14PX;" >/** * @author Lilongsheng * @param url_str Send interface * @return * @throws Exception */private string Sendgetinfo (string URL_STR) throws Exception{system.out.println (URL_STR); url url =new url (url_str); URLConnection connection=url.openconnection (); Byte[] Buf=new byte[1024*4]; Bytearrayoutputstream Bos =new Bytearrayoutputstream (); int n; while ((N=connection.getinputstream (). Read (BUF)) >=0) bos.write (buf,0,n); Return bos.tostring ("GBK");} </span>
This is a simple interface to launch text messages, in addition to MMS and so on, MMS format for TMS, MMS, etc. also need MMS development package.
SMS, MMS development is not difficult, in fact, quite simple is also the use of other people's class library, in the development of the need to develop one side of the study, will inevitably meet their own will not, as long as take a good look, check the information, look at the understanding of others and the existing information can be resolved, in addition to the system has not really , writing is not very rigorous, recently the manager looked at me to write the code said much better than before, improved a lot, this is a little satisfaction, writing code is important is the design ideas and flexibility, rigor.