移動端調用接口


  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 }
復制代碼


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM