java為移動端寫介面,java移動端介面

來源:互聯網
上載者:User

java為移動端寫介面,java移動端介面

  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 //登陸servlet27 @Controller28 public class LoginCotroller {    29     /**30      * 1. value="/doLogin/{username}/{password}" 攔截 xxx/doLogin/xx/xx31      * 2. @ResponseBody 使用此註解將返回資料類型封裝json32      * 3. @PathVariable("username") 截取請求1.value中{username}的值33      * 4. Map<String, Object> 服務端將值放入map中再封裝為json,用戶端方便通過key取出value34      */35     36     StudentDAO studentDAO = new StudentDAO();//調用登陸判斷方法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("攔截了用戶端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 }

 

 

 



  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.