作为后端语言的java的厉害之处在于web。 各种众所周知的网络APP都可以用java完成。 那么,在这个移动优先的时代,如何继续发挥java的优势呢? 通常以java作为app的服务端,为app客户端提供数据和编写业务逻辑,所以用java编写接口,由app客户端访问接口返回json文件进行分析,最后进行业务逻辑
这种方式通常称为rest风格。
rest风格是一种架构思想,博士生n年前发表的博士论文,其核心思想是前后端分离,前端通过http请求,如www.xxxx.com/demo/username/password 因此,在后端优先考虑移动的时代,rest风格非常重要。 通常,在后台可以通过移动端、pc端等各种终端进行访问。 通过rest风格改进的mvc更容易通过java实现rest风格的是SpringMVC框架,他提供了ios访问我的java后台demo。 java后XndnzzTTQ台采用SpringMVC和Hibernate。
//java端
包控制器;
import java.util.HashMap;
import java.util.Map;
import java.util.List;
import javax.servlet.http.http servlet request;
import JDK.nashorn.API.scripting.js object;
import model.Student;
import model.Teacher;
importorg.spring framework.stereotype.controller;
importorg.spring framework.ui.model;
importorg.spring framework.web.bind.annotation.path variable;
importorg.spring framework.web.bind.annotation.requestbody;
importorg.spring framework.web.bind.annotation.request mapping;
importorg.spring framework.web.bind.annotation.request method;
importorg.spring framework.web.bind.annotation.response body;
import dao.Get;
import dao.StudentDAO;
//登录到servlet
@Controller
公共类日志控制器{
//*
*1.屏蔽value='/dolog in/{ username }/{ password } ' XXX/dolog in/xx/xx
* 2. @ResponseBody使用此注释返回数据类型封装json
*3.@pathvariable('username ) )剪切请求1.value的{username}值
* 4. Map服务端在map中输入值并封装在json中,客户端很容易通过key取出value
*/
studentdaostudentdao=new student Dao (; //调用登录判断方法
@ request mapping (value='/dolo http://www.CPP cns.com et ) ) ) ) ) ) ) )。
@ResponseBody
publicmapgetteacher (@pathvariable (username ) (Integer username,@ path variable ) password ) ) String password
System.out.println;
映射图=new hashmap (;
student Dao.loginbystudent (username,password ) }
System.out.println (密码正确);
map.put(result ),)1);
返回地图; 封装到json中并返回给客户端
}
System.out.println (密码错误);
map.put(result ),)0);
返回地图; 封装到json中并返回给客户端
}
}
//ios侧
#import
#import
intmain(intargc,const char * argv[] ) () ) )
@autoreleasepool {
char oldUsername[128];
char oldPassword[128];
请输入nslog(@ '用户名: ';
scanf('%s ',oldUsername );
ns string * username=[ nsstringstringwithutf8string : old username ]//ns string转换*
nslog(@ '请输入密码:');
Scanf('%s ',oldPassword );
ns string * password=[ nsstringstringwithutf8string : old password ]; 转换为//ns字符串*
访问springMVC后台并分析返回的json数据
//定义例外
NSError *error;
//定义要求action使用stringWithFormat连接字符串
ns string * URL=[ nsstringstringstringwithformat : @ ' http://154212 l6t7. im work.net 336027063/party OS _ app/dolog in
加载NSURL对象
nsurlrequest * request=[ nsurlrequestrequestwithurl : [ nsurlurlwithstring : URL ];
//当发送请求时,所请求的url数据被放置在NSData对象中
sdat a* response=[ nsurlconnectionsendsynchronousrequest 3360 requestreturningresponse 3360 nilerror 3360 nil ];
//NSJSONSerialization根据响应请求分析数据并放入词典
ns dictionary * JSON result=[ nsjsonserializationjsonobjectwithdata : responseoptionwww.CPP cns.coms 3360 nsjsonreadingmutition
ns string * result value=[ jsonresultobjectforkey : @ ' result ' ];
nslog(@ '您的url是%@ ',url );
nslog(@ '服务器端返回值(@ ',resultValue );
//oc字符串比较方法resultvalueisequaltostring : @ '1' ]与java的equlse相似
以上是本文的全部内容,希望对大家的学习有帮助。 另外,请支持我们。
本文的标题: java是移动端写入接口的开发示例
本文地址:3358 www.CPP cns.com/ruanjian/Java/200420.html