java实现手机端开发实例_java为移动端写接口开发实例
发布日期:2021-10-27 11:20:25 浏览次数:12 分类:技术文章

本文共 3311 字,大约阅读时间需要 11 分钟。

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 服务端将值放入map中再封装为json,客户端方便通过key取出value

*/

studentdao studentdao = new studentdao();//调用登陆判断方法

@requestmapping(value="/dologin/{username}/{password}",method=requestmethod.get)

@responsebody

public map getteacher(@pathvariable("username") integer username, @pathvariable("password") string password){

system.out.println("拦截了客户端json请求");

map map = new hashmap();

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

#import

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;

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持萬仟网。

希望与广大网友互动??

点此进行留言吧!

转载地址:https://blog.csdn.net/weixin_34518190/article/details/114948302 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:java制作九行九列表格_用编程实现Excel表格的九个基本操作
下一篇:mysql简介分析_(一)MySQL的架构介绍

发表评论

最新留言

很好
[***.229.124.182]2024年03月29日 20时51分29秒