Mobile phone app is divided into, online and stand-alone, online is similar to the C/S mode, with the server and other people to share data programs, single-machine is in no network can play around the app.
In the current era of internet prevalence, 99% of all programs are working in a networked environment. So how to develop local apps and upload data to server sharing over the Internet has become the focus of online app development.
First of all, please understand B/s work mode, in the Web program, we just request the address to the HTTP server, the server (whether it is apache,tomcat,nginx these are implemented HTTP socket program) back to our HTML file, browser it, The HTML is interpreted to generate a visual interface.
The above picture is relatively simple, but also hides a lot of packaging components, in fact, the whole process is more complex than our diagram, and as a junior developer, we just need to know these, whether it is javaee,asp.net MVC, or the construction of PHP type Web site has a basic set of routines!
The author has not actually done the development of the app before, even more did not touch Google's Android SDK, although knows, but the configuration of the XML file is deeply disgusted I always do not want to embark on the development of native Android
I have been doing the web development in the past few years, from the lamp environment to the ASP. Java EE, across three platforms, two operating systems, can be said to be web development Nail House, research the development of the app, so with HTML5 complex I naturally more keen to play html5+ development capabilities
So what's the difference between HTML5 app development and web development?
Can say, essentially not! You can use HTML5 to develop an app comparable to the original, in the future of HTML5 's continuous optimization, HTML5 mobile development is inevitably the trend, thanks to its fast and efficient development environment, simple and easy to get started features. Online HTML5 app development routines and see:
Does it feel like it hasn't changed? True, so there is no difference in nature, which is why most Web developers change into app development more simple (less than a day), as long as you know HTML5,JS,CSS3 and a little front-end frame can be done!
Wait, please know one thing first!
Cross-Region requests
That is, not the same domain name for the transfer of data/files, when you use a browser to browse the Web page, you do not encounter this problem, because the browser is from the server under a domain name to obtain HTML, but also to the site to return data files
However, HTML5 development of the app, the file is placed on the local phone, this is to improve the experience, the phone does not request the full HTML file to the server, which creates the problem of exchanging data across domains, that is, the phone is a local domain, the server is another domain!
What's wrong with that? Yes, browsers prevent cross-domain requests by default, don't you think? Why? For Security!
In case someone sent a post, the post link to another site, and this link to the JS implementation of the website you have access to the personal data linked to the function of the site, then you click into the future of your personal data will be access to another site, it is not possible, the browser will use the same origin policy organization this thing happened, It's too dangerous!
The same app requests the site should not send their own domain data to another domain (your phone), the browser thinks the data to others!
Explain clearly, then we need to solve this "bug" implementation of the app and server communication, in fact, the default MUI has solved this problem for us, the app with Hbuilder packaging does not have the same origin policy problem, can request data across domains, this kind of thing will only appear in the browser access
In the early days, when I was developing with jquery mobile, I didn't have a scaffolding like this, and I couldn't even pack the app (the development process was to test the written HTML file in a mobile browser, every step of the way, uploading to the PhoneGap packaged Hbuilder app)
So with the jquery mobile friends, may encounter such problems, there is a small online solution to the mobile browser test app and access to other website data, this is the method I used before:
JS before jquery.js) */$ (document). On ("Mobileinit",function() { // Support Domain name Ajax load $.support.cors=true; $.mobile.allowcrossdomainpages=true; $.mobile.pushstate=false;});
It is not a problem to test the H5 app in a mobile browser, it is not necessary to actually package it into PhoneGap, because PhoneGap uses the file protocol to send the request, and it does not have the same origin policy
Phonegap wiki says: "The Cross-domain security policy does not affect PHONEGAP applications. Since the HTML files is called by WebKit and the FILE://protocol, the security policy does not apply. "
But, according to my experience with the development of the jquery mobile app the previous year, jquery Mobile is not suitable for the development of the app, there is no black jquery mobile, his own characteristics is for WAP, is suitable for mobile phone page development, not suitable for packaging for the app, Because he does not have a complete packaging environment, that is not good to use html5+!
And jquery mobile itself is more bloated! Although powerful, but does not involve 5+, can not use the 5+ app is not complete app! So later I may use the Jqmobile Development WAP page, the suggestion puts in the development
App development Please use MUI, after using MUI, please forget the above mentioned various problems, we get straight to the point, plug the theme, do networking app! O (∩_∩) o
We do the Internet app only need to know two points:
First we use AJAX and the server to communicate, Mui.ajax () can do it all
Second, data exchange using JSON, commercial projects Please specify a fixed JSON format!
Before using Ajax, encapsulate Ajax so that you reduce the amount of code in the actual project, and I do:
Create a new app.js, a common class library for each page
The encapsulation code for Ajax:
Http_domain = "http://192.168.31.130/dashen/public/index.php/app/"; Global variables, domain names, and common paths
//using the global progress bar varProgress = MUI ("Body")). ProgressBar (); //wrap up a generic Ajax requestOwner.request =function(CTL, Act, Dataobj, callback) {//Global progress barCallback = Callback | |$.noop; CTL=ctl; Act=Act; varurl = http_domain + ctl + "/" +Act; Mui.ajax (URL, {data:dataobj, DataType:' JSON ', timeout:5000, type:' Post ', Beforesend:function(XMLHttpRequest) {progress.show (); }, Complete:function(XMLHttpRequest, Textstatus) {progress.hide (); }, Success:function(response) {returncallback (response); }, Error:function(XHR, type, error) {if(Type = = = "Timeout") //You cannot use Plus, you need to use the MUI ToastMui.toast ("Connection timed out, check network"); ElseMui.toast ("Unknown network error, unable to find the server, try it later."); } });
Note that we have found some common points of request, loading bars and network error messages, which are required for every request, and do not have to be re-written on each page, which is the meaning of encapsulation
Where Ajax has some parameters:
Beforesend: Before sending the event, we can open the loading progress bar, using the MUI's own global progress bar
Complete: This is the end of the Ajax event, whether it is a failure or success will eventually trigger, so close the progress bar is the most appropriate
Success: The request succeeds, the success here refers to the access to the server and the server performs a smooth return of the data, we put a back function, the obtained JSON data back, so that we can implement the request after the different requirements of different pages to execute different methods
Error: Unified error here, using MUI comes with the prompt function
URL: This parameter is the requested address, which uses a global variable + controller class name + method name form, in order to access different methods, when needed only to the request where the closure of the class name, the method name can be
Note that the first owner is the name of the closure, and this request method is placed in a JS closure to prevent conflicts with the external method of the same name, implemented like jquery, as follows:
function ($, owner) { /* Various public methods included in the closure */ owner.request=function (){}; function = {}));
You can now call this method of encapsulating Ajax: Example
App.request (' User ', ' Checklogin ', {},function(res) {if(Res.status = = 1 && Res.data.token = =state.token) {//If the detection server exists and the local relative should not operatePlus.navigator.closeSplashscreen (); Unfullscreen (); App.setuserinfo (Res.data); //personal information obtained by the storage serverUpdate_head_info ();//Refresh Header Information}Else{plus.nativeUI.toast (res.info); App.cleartoken (); App.tologin (); } });
The last function is the passed-in callback
Well, such a networked H5 app is so simple to implement, as for this package of Ajax method can also be extended to adapt to different occasions, you can study the changes on their own!
Note: The server returns the JSON format, discard the old XML, give me an example of the THINKPHP5 framework backend that I used:
if ($res$id) return [' Status ' =>1, ' info ' = ' registered success ']; JSON else return [' status ' =>0, ' info ' = ' User information write error, retry later ']; Json
The premise of this code is that the configuration thinkphp Unified return JSON instead of view ()
// the default output of the app is JSON and needs to be exported to the app client according to spec . Default_return_type ' = ' json ', // default AJAX data return format, optional JSON xml ... ' Default_ajax_return ' = ' json ',
Other backend frameworks, other languages use their own JSON to return the configuration, methods, and functions
MUI Development app's networking app transmits data