最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java為移動(dòng)端寫(xiě)接口開(kāi)發(fā)實(shí)例

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

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

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

restful是一種架構(gòu)思想,是一位博士生在N年前發(fā)表的一篇博士生論文,其核心思想就是前后端分離,前端通過(guò)http請(qǐng)求,如www.xxxx.com/demo/username/password  來(lái)訪(fǎng)問(wèn)后端的接口,然后后端將處理好的數(shù)據(jù)封裝為json返回,這樣,后端只需關(guān)注具體邏輯 提供接口,而前端只關(guān)心界面,提高了程序解耦性。 在移動(dòng)優(yōu)先的時(shí)代,restful極為重要。通常一套后臺(tái)可以讓多種終端訪(fǎng)問(wèn),包括移動(dòng)端,pc端。     通過(guò)restful改進(jìn)的mvc    在java中比較容易實(shí)現(xiàn)restful的是SpringMVC框架,他提供了一套下面是一個(gè)ios訪(fǎng)問(wèn)我的java后臺(tái)demo,java后臺(tái)采用了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 使用此注解將返回?cái)?shù)據(jù)類(lèi)型封裝json
   * 3. @PathVariable("username") 截取請(qǐng)求1.value中{username}的值
   * 4. Map<String, Object> 服務(wù)端將值放入map中再封裝為json,客戶(hù)端方便通過(guò)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("攔截了客戶(hù)端json請(qǐng)求");
    Map<String, Object> map = new HashMap<String, Object>();
    
    if(studentDAO.loginByStudent(username, password)){
      System.out.println("密碼正確");
      map.put("result", "1");
      return map; //封裝為json返回給客戶(hù)端
    }
      
    System.out.println("密碼錯(cuò)誤");
    map.put("result", "0");
    return map; //封裝為json返回給客戶(hù)端
  }

}

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

int main(int argc, const char * argv[]) {
  @autoreleasepool {
  
    char oldUsername[128];
    char oldPassword[128];
    
    NSLog(@"請(qǐng)輸入用戶(hù)名 :");
    scanf("%s", oldUsername);
    NSString *username = [NSString stringWithUTF8String:oldUsername]; //轉(zhuǎn)換為NSString *
    NSLog(@"請(qǐng)輸入密碼 :");
    scanf("%s", oldPassword);
    NSString *password = [NSString stringWithUTF8String:oldPassword]; //轉(zhuǎn)換為NSString *
    
    //訪(fǎng)問(wèn)springMVC后臺(tái)并解析返回的json數(shù)據(jù)
    //定義一個(gè)異常
    NSError *error;
    
    //定義請(qǐng)求action 使用stringWithFormat拼接字符串
    NSString *url = [NSString stringWithFormat:@"http://154212l6t7.imwork.net:27063/partyOS_APP/doLogin/%@/%@", username, password];
    
    //加載一個(gè)NSURL對(duì)象
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
    
    //發(fā)送請(qǐng)求 將請(qǐng)求的url數(shù)據(jù)放到NSData對(duì)象中
    NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    
    //NSJSONSerialization從response請(qǐng)求中解析出數(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類(lèi)似
    if([resultValue isEqualToString:@"1"]){
      NSLog(@"登錄成功!");
    }else{
      NSLog(@"登錄失敗,用戶(hù)名或密碼錯(cuò)誤!");
    }
    
    
  }
  return 0;
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

崇仁县| 抚宁县| 澎湖县| 阿瓦提县| 凤庆县| 绥滨县| 茂名市| 正宁县| 沂源县| 石门县| 罗甸县| 福贡县| 彰化市| 津南区| 克东县| 哈尔滨市| 高安市| 吉安县| 平邑县| 沾化县| 新邵县| 峡江县| 元江| 化隆| 荣成市| 洛阳市| 阿坝| 澄江县| 江北区| 桂林市| 台中市| 潼关县| 梓潼县| 河南省| 淳化县| 辉南县| 九寨沟县| 延津县| 安乡县| 弥勒县| 平利县|