作为一名普通的Android开发者,想要独立开发一个App,最头疼的地方在于后台接口没人写,正常来说会有以下两种解决方案:
用第三方服务自己搭建服务器
第一种方案我之前试过,使用的是bmob后端云,还别说,当时用的还挺顺手,以致于最后成功的把我的App上架到应用市场,然鹅,好景不长,慢慢的,bmob开始各种收费了,一个是短信没法正常使用,后来我换成了mob的短信SDK,觉得用着不顺手,又换回了bmob的邮件登陆,再后来邮件又收费了,直到最后片服务无法使用才导致我不想再用第三方服务,改为自己搭建服务以上是背景介绍,下面进入实战
实战演练
这是一个javawebapp项目,以下是开发环境开发工具:intellijideaultimate2011数据库:mysql19服务器:tomcat0.75开发框架:springmvc+mybatis以下只放出一些关键代码,这些代码都是经过本人实际测试成功的,详细源码可移步文末的github链接查看
POST请求
代码:
/**
* 用户登陆,Post方式
*
* @param param
* @return
*/
@RequestMapping(value = 'login', method = RequestMethod.POST)
@ResponseBody
public Map login(@RequestParam('param') String param) {
out.println('welcome to login on post,param=' + param);
ObjectMapper objectMapper = new ObjectMapper(); //转换器
Map map = new HashMap<>();
try {
LoginBean loginBean = objectMapper.readValue(param, LoginBean.class); //json转换成map
ResultBean result = onLogin(loginBean.getName(), loginBean.getPassword());
out.println('result==>' + result);
map.put('code', result.getCode());
map.put('reason', result.getReason());
map.put('success', result.isSuccess());
} catch (IOException e) {
e.printStackTrace();
}
return map;
}
模拟post请求测试地址:http://coolacom/此处我把本地的localhost通过内网穿透转换成一个外网地址,这样方便调用,具体如何调用可参考Javaweb项目使用【内网穿透】来实现对外访问测试效果如下:数据库记录
GET请求
代码:
/**
* 获取数据库中的列表,get方式
*
* @return
*/
@RequestMapping(value = 'userList', method = RequestMethod.GET)
@ResponseBody
public Map userList() {
out.println('welcome to userList');
Map map = new HashMap<>();
ResultBean result = onUserList();
out.println('result==>' + result);
map.put('code', result.getCode());
map.put('reason', result.getReason());
map.put('success', result.isSuccess());
map.put('records', result.getRecords());
return map;
}
截效果:
接收PC端上传文件
接收Android端上传文件
springmvc服务端+android客户端的文件上传,此参考博文中不仅包括java端接收上传的代码,还包括Android端上传文件的代码
/**
* 手机端上传文件
*
*/
@Resource
private MobileResult mobileResult;
@RequestMapping('/mobile/uploadfile')
@ResponseBody
public MobileResult uploadPhone(@RequestParam(value = 'file', required = false) MultipartFile file, HttpServletRequest request)
throws IllegalStateException, IOException {
String path = uploadFile(file, request);
mobileResult.setCode('200');
mobileResult.setPath(path);
mobileResult.setMessage('上传成功');
return mobileResult;
}
private String uploadFile(MultipartFile file, HttpServletRequest request) throws IOException {
// String path = request.getSession().getServletContext().getRealPath('upload');
String path = '/Users/liuxunming/Documents/AppService/Images/';
String fileName = file.getOriginalFilename();
File targetFile = new File(path, fileName);
if (!targetFile.exists()) {
targetFile.mkdirs();
}
file.transferTo(targetFile);
return targetFile.getAbsolutePath();
}
问题
源码
文章为作者独立观点,不代表 股票程序化软件自动交易接口观点
无上正等正觉 不2022-12-06
因为前期漏掉了芯片这一环,芯片受限的中国手机公司没有一家敢在股票,惧怕老美断供芯片,没办法,中国芯片任重道远。