首頁  >  文章  >  Java  >  Java如何為行動端寫介面開發的實例分享

Java如何為行動端寫介面開發的實例分享

黄舟
黄舟原創
2017-08-20 09:30:231657瀏覽

本篇文章主要介紹了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框架,他提供了一套下面是一個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 使用此注解将返回数据类型封装json
   * 3. @PathVariable("username") 截取请求1.value中{username}的值
   * 4. Map<String, Object> 服务端将值放入map中再封装为json,客户端方便通过key取出value
   */
  
  StudentDAO studentDAO = new StudentDAO();//调用登陆判断方法
  
  @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]; //转换为NSString *
    NSLog(@"请输入密码 :");
    scanf("%s", oldPassword);
    NSString *password = [NSString stringWithUTF8String:oldPassword]; //转换为NSString *
    
    //访问springMVC后台并解析返回的json数据
    //定义一个异常
    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]];
    
    //发送请求 将请求的url数据放到NSData对象中
    NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    
    //NSJSONSerialization从response请求中解析出数据放到字典中
    NSDictionary *jsonResult = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error];
    
    NSString *resultValue = [jsonResult objectForKey:@"result"];
    
    NSLog(@"你的url是%@", url);
    NSLog(@"服务端返回值%@", resultValue);
    
    // oc字符串比较方法 resultValue isEqualToString:@"1"] 和java 的equlse类似
    if([resultValue isEqualToString:@"1"]){
      NSLog(@"登录成功!");
    }else{
      NSLog(@"登录失败,用户名或密码错误!");
    }
    
    
  }
  return 0;
}

以上是Java如何為行動端寫介面開發的實例分享的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn