Java is the mobile write interface and java Mobile Interface

Source: Internet
Author: User

Java is the mobile write interface and java Mobile Interface

Java, as a back-end language, has a powerful advantage in the web, the well-known network applications that java can do. In this era of mobile first, how can we continue to exert the power of java. Java is usually used as the app server to provide data for the app client and implement business logic. Therefore, we use java to write interfaces. The app client access interface returns json files for parsing, finally, implement the business logic.

This method is usually called restful.

Restful is an architectural concept. It is a doctoral thesis published by a doctoral student N years ago. Its core idea is to separate the front-end and back-end, for example, www.xxxx.com/demo/username/password is used to access the backend interface, and then the backend encapsulates the processed data as a json response. In this way, the backend only needs to focus on the specific logic to provide the interface, while the front end only cares about the interface, this improves program decoupling. In the era of mobile precedence, restful is extremely important. Generally, a set of backend terminals can be accessed by multiple terminals, including mobile terminals and pc terminals. The SpringMVC framework, which is easier to implement restful in java through restful improved mvc, provides a set of annotations for processing json. Return json data through @ ResponseBody and parse json through @ ResquestBody.

 

 

The following is a java background demo for ios access. Spring MVC and Hibernate are used in the java background.

// Java end

1 package cotroller; 2 3 import java. util. hashMap; 4 import java. util. map; 5 import java. util. list; 6 7 import javax. servlet. http. httpServletRequest; 8 9 import jdk. nashorn. api. scripting. JSObject; 10 import model. student; 11 import model. teacher; 12 13 import org. springframework. stereotype. controller; 14 import org. springframework. ui. model; 15 import org. springframework. web. bind. annotation. pathVariable; 16 import org. springframework. web. bind. annotation. requestBody; 17 import org. springframework. web. bind. annotation. requestMapping; 18 import org. springframework. web. bind. annotation. requestMethod; 19 import org. springframework. web. bind. annotation. responseBody; 20 21 22 23 import dao. get; 24 import dao. studentDAO; 25 26 // log on to servlet27 @ Controller28 public class LoginCotroller {29/** 30*1. value = "/doLogin/{username}/{password}" intercept xxx/doLogin/xx/xx31 * 2. @ ResponseBody use this annotation to encapsulate the returned data type json32 * 3. @ PathVariable ("username") Intercepts request 1. value of {username} in value 33*4. map <String, Object> the Server puts the value in map and encapsulates it as json. The client can conveniently retrieve value34 */35 36 StudentDAO studentDAO = new StudentDAO () through the key (); // call the logon Judgment method 37 38 @ RequestMapping (value = "/doLogin/{username}/{password}", method = RequestMethod. GET) 39 @ ResponseBody40 public Map <String, Object> getTeacher (@ PathVariable ("username") Integer username, @ PathVariable ("password") String password) {41 System. out. println ("intercepted client json requests"); 42 Map <String, Object> map = new HashMap <String, Object> (); 43 44 if (studentDAO. loginByStudent (username, password) {45 System. out. println ("correct password"); 46 map. put ("result", "1"); 47 return map; // encapsulate as json and return it to the client 48} 49 50 System. out. println ("Incorrect password"); 51 map. put ("result", "0"); 52 return map; // encapsulate as json and return to client 53} 54 55}


// Ios end

1 # import <Foundation/Foundation. h> 2 # import <stdio. h> 3 4 int main (int argc, const char * argv []) {5 @ autoreleasepool {6 7 char oldUsername [128]; 8 char oldPassword [128]; 9 10 NSLog (@ "Enter the User name:"); 11 scanf ("% s", oldUsername); 12 NSString * username = [NSString stringwithuf8string: oldUsername]; // convert to NSString * 13 NSLog (@ "Enter password:"); 14 scanf ("% s", oldPassword); 15 NSString * password = [NSString stringW IthUTF8String: oldPassword]; // convert to NSString * 16 17 // access the springMVC background and parse the returned json data 18 // define an exception 19 NSError * error; 20 21 // define request action concatenate string 22 NSString * url = [NSString stringWithFormat: @ "http://154212l6t7.imwork.net: 27063/partyOS_APP/doLogin/% @", username, password]; 23 24 // load an NSURL object 25 NSURLRequest * request = [NSURLRequest requestWithURL: [NSURL URLWithString: url]; 26 27 // send a request to send the request url Put the data in the NSData object 28 NSData * response = [NSURLConnection sendSynchronousRequest: request returningResponse: nil error: nil]; 29 30 // NSJSONSerialization parses the data from the response Request and puts it in the dictionary 31 NSDictionary * jsonResult = [NSJSONSerialization JSONObjectWithData: response options: NSJSONReadingMutableLeaves error: & error]; 32 33 NSString * resultValue = [jsonResult objectForKey: @ "result"]; 34 35 NSLog (@ "Your url is % @", url); 36 NSLog (@" Server Return Value % @ ", resultValue); 37 38 // oc string comparison method resultValue is1_tostring: @" 1 "] and java equlse is similar to 39 if ([resultValue is1_tostring: @ "1"]) {40 NSLog (@ "Logon successful! "); 41} else {42 NSLog (@" Logon Failed, incorrect username or password! "); 43} 44 45 46} 47 return 0; 48}

 

 

 



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.