• 移动端调用接口


      java作为一门后端语言,其厉害之处在于web,大家比较熟知的各种网络应用,java都能做,那么在这个移动优先的时代,如何继续发挥java的强大呢。通常是让java作为一个app的服务端,为app客户端提供数据,做业务逻辑,所以我们用java来写接口,app客户端访问接口返回json文件进行解析,最后实现业务逻辑。

         而这种方式我们通常叫做restful。

      restful是一种架构思想,是一位博士生在N年前发表的一篇博士生论文,其核心思想就是前后端分离,前端通过http请求,如www.xxxx.com/demo/username/password  来访问后端的接口,然后后端将处理好的数据封装为json返回,这样,后端只需关注具体逻辑 提供接口,而前端只关心界面,提高了程序解耦性。 在移动优先的时代,restful极为重要。通常一套后台可以让多种终端访问,包括移动端,pc端。     通过restful改进的mvc    在java中比较容易实现restful的是SpringMVC框架,他提供了一套处理json的注解。通过@ResponseBody返回json数据,通过@ResquestBody解析json。

    下面是一个ios访问我的java后台demo,java后台采用了springMVC和Hibernate。

    //java端

    复制代码
     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 //登陆servlet
    27 @Controller
    28 public class LoginCotroller {    
    29     /**
    30      * 1. value="/doLogin/{username}/{password}" 拦截 xxx/doLogin/xx/xx
    31      * 2. @ResponseBody 使用此注解将返回数据类型封装json
    32      * 3. @PathVariable("username") 截取请求1.value中{username}的值
    33      * 4. Map<String, Object> 服务端将值放入map中再封装为json,客户端方便通过key取出value
    34      */
    35     
    36     StudentDAO studentDAO = new StudentDAO();//调用登陆判断方法
    37     
    38     @RequestMapping(value="/doLogin/{username}/{password}",method=RequestMethod.GET)
    39     @ResponseBody
    40     public Map<String, Object> getTeacher(@PathVariable("username") Integer username, @PathVariable("password") String password){    
    41         System.out.println("拦截了客户端json请求");
    42         Map<String, Object> map = new HashMap<String, Object>();
    43         
    44         if(studentDAO.loginByStudent(username, password)){
    45             System.out.println("密码正确");
    46             map.put("result", "1");
    47             return map; //封装为json返回给客户端
    48         }
    49             
    50         System.out.println("密码错误");
    51         map.put("result", "0");
    52         return map; //封装为json返回给客户端
    53     }
    54 
    55 }
    复制代码


    //ios端

    复制代码
     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(@"请输入用户名 :");
    11         scanf("%s", oldUsername);
    12         NSString *username = [NSString stringWithUTF8String:oldUsername]; //转换为NSString *
    13         NSLog(@"请输入密码 :");
    14         scanf("%s", oldPassword);
    15         NSString *password = [NSString stringWithUTF8String:oldPassword]; //转换为NSString *
    16         
    17         //访问springMVC后台并解析返回的json数据
    18         //定义一个异常
    19         NSError *error;
    20         
    21         //定义请求action 使用stringWithFormat拼接字符串
    22         NSString *url = [NSString stringWithFormat:@"http://154212l6t7.imwork.net:27063/partyOS_APP/doLogin/%@/%@", username, password];
    23         
    24         //加载一个NSURL对象
    25         NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
    26         
    27         //发送请求 将请求的url数据放到NSData对象中
    28         NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    29         
    30         //NSJSONSerialization从response请求中解析出数据放到字典中
    31         NSDictionary *jsonResult = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error];
    32         
    33         NSString *resultValue = [jsonResult objectForKey:@"result"];
    34         
    35         NSLog(@"你的url是%@", url);
    36         NSLog(@"服务端返回值%@", resultValue);
    37         
    38         // oc字符串比较方法 resultValue isEqualToString:@"1"] 和java 的equlse类似
    39         if([resultValue isEqualToString:@"1"]){
    40             NSLog(@"登录成功!");
    41         }else{
    42             NSLog(@"登录失败,用户名或密码错误!");
    43         }
    44         
    45         
    46     }
    47     return 0;
    48 }
    复制代码
  • 相关阅读:
    [LeetCode每日1题][简单] 169. 多数元素
    [LeetCode每日1题][简单] 1013. 将数组分成和相等的三个部分
    [LeetCode每日1题][中等] 322. 零钱兑换
    [LeetCode每日1题][中等] 面试题59
    软工实践个人总结
    2019 SDN大作业
    2019 SDN上机第7次作业
    第01组 Beta版本演示
    如果有一天我变得很有钱组——alpha冲刺day7
    如果有一天我变得很有钱组——alpha冲刺day6
  • 原文地址:https://www.cnblogs.com/lishuiqing/p/7874939.html
Copyright © 2020-2023  润新知