欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java為移動端寫接口開發(fā)實例

 更新時間:2017年08月18日 16:27:34   作者:廖海的博客  
本篇文章主要介紹了java如何為移動端寫接口,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

java作為一門后端語言,其厲害之處在于web,大家比較熟知的各種網(wǎng)絡(luò)應(yīng)用,java都能做,那么在這個移動優(yōu)先的時代,如何繼續(xù)發(fā)揮java的強大呢。通常是讓java作為一個app的服務(wù)端,為app客戶端提供數(shù)據(jù),做業(yè)務(wù)邏輯,所以我們用java來寫接口,app客戶端訪問接口返回json文件進行解析,最后實現(xiàn)業(yè)務(wù)邏輯。

而這種方式我們通常叫做restful。

restful是一種架構(gòu)思想,是一位博士生在N年前發(fā)表的一篇博士生論文,其核心思想就是前后端分離,前端通過http請求,如www.xxxx.com/demo/username/password  來訪問后端的接口,然后后端將處理好的數(shù)據(jù)封裝為json返回,這樣,后端只需關(guān)注具體邏輯 提供接口,而前端只關(guān)心界面,提高了程序解耦性。 在移動優(yōu)先的時代,restful極為重要。通常一套后臺可以讓多種終端訪問,包括移動端,pc端。     通過restful改進的mvc    在java中比較容易實現(xiàn)restful的是SpringMVC框架,他提供了一套下面是一個ios訪問我的java后臺demo,java后臺采用了springMVC和Hibernate。

//java端

package cotroller;

import java.util.HashMap;
import java.util.Map;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import jdk.nashorn.api.scripting.JSObject;
import model.Student;
import model.Teacher;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;



import dao.Get;
import dao.StudentDAO;

//登陸servlet
@Controller
public class LoginCotroller {  
  /**
   * 1. value="/doLogin/{username}/{password}" 攔截 xxx/doLogin/xx/xx
   * 2. @ResponseBody 使用此注解將返回數(shù)據(jù)類型封裝json
   * 3. @PathVariable("username") 截取請求1.value中{username}的值
   * 4. Map<String, Object> 服務(wù)端將值放入map中再封裝為json,客戶端方便通過key取出value
   */
  
  StudentDAO studentDAO = new StudentDAO();//調(diào)用登陸判斷方法
  
  @RequestMapping(value="/doLogin/{username}/{password}",method=RequestMethod.GET)
  @ResponseBody
  public Map<String, Object> getTeacher(@PathVariable("username") Integer username, @PathVariable("password") String password){  
    System.out.println("攔截了客戶端json請求");
    Map<String, Object> map = new HashMap<String, Object>();
    
    if(studentDAO.loginByStudent(username, password)){
      System.out.println("密碼正確");
      map.put("result", "1");
      return map; //封裝為json返回給客戶端
    }
      
    System.out.println("密碼錯誤");
    map.put("result", "0");
    return map; //封裝為json返回給客戶端
  }

}

//ios端
#import <Foundation/Foundation.h>
#import <stdio.h>

int main(int argc, const char * argv[]) {
  @autoreleasepool {
  
    char oldUsername[128];
    char oldPassword[128];
    
    NSLog(@"請輸入用戶名 :");
    scanf("%s", oldUsername);
    NSString *username = [NSString stringWithUTF8String:oldUsername]; //轉(zhuǎn)換為NSString *
    NSLog(@"請輸入密碼 :");
    scanf("%s", oldPassword);
    NSString *password = [NSString stringWithUTF8String:oldPassword]; //轉(zhuǎn)換為NSString *
    
    //訪問springMVC后臺并解析返回的json數(shù)據(jù)
    //定義一個異常
    NSError *error;
    
    //定義請求action 使用stringWithFormat拼接字符串
    NSString *url = [NSString stringWithFormat:@"http://154212l6t7.imwork.net:27063/partyOS_APP/doLogin/%@/%@", username, password];
    
    //加載一個NSURL對象
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
    
    //發(fā)送請求 將請求的url數(shù)據(jù)放到NSData對象中
    NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    
    //NSJSONSerialization從response請求中解析出數(shù)據(jù)放到字典中
    NSDictionary *jsonResult = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error];
    
    NSString *resultValue = [jsonResult objectForKey:@"result"];
    
    NSLog(@"你的url是%@", url);
    NSLog(@"服務(wù)端返回值%@", resultValue);
    
    // oc字符串比較方法 resultValue isEqualToString:@"1"] 和java 的equlse類似
    if([resultValue isEqualToString:@"1"]){
      NSLog(@"登錄成功!");
    }else{
      NSLog(@"登錄失敗,用戶名或密碼錯誤!");
    }
    
    
  }
  return 0;
}

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論