安卓客户端调用java服务器_针对安卓客户端的java服务器,接收和json返回数据

本人新手,刚参加工作,前段时间让我写安卓服务端,java语言,使用的是SSH框架,去百度谷歌,不过这方面的好像不是太多(也可能是我没发现)。

----------------------------------------------------

使用http协议,安卓那边的代码我不太懂,不过在我本机上测试代码的时候 我是使用http请求,以get方式提交。可以调用。

再说我的服务端,Struts+spring+hibernate,传输的是json数据。

剩下的不多说,见代码;

这是我的BaseAction,用于接收json数据:

import com.opensymphony.xwork2.ActionSupport;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import net.sf.json.JSONSerializer;

import org.apache.struts2.ServletActionContext;

public class BaseAction extends ActionSupport {

private static final long serialVersionUID = 1L;

public HttpServletRequest getRequest() {

return ServletActionContext.getRequest();

}

public HttpServletResponse getResponse() {

return ServletActionContext.getResponse();

}

public HttpSession getSession() {

return getRequest().getSession();

}

public HttpSession getSession(boolean create) {

return getRequest().getSession(create);

}

public PrintWriter createPrintWriter() {

HttpServletResponse response = ServletActionContext.getResponse();

response.setContentType("application/x-json;charset=utf-8");

PrintWriter out = null;

try {

out = ServletActionContext.getResponse().getWriter();

} catch (IOException e) {

e.printStackTrace();

}

return out;

}

public JSONObject readJsonObject() {

BufferedReader reader = null;

JSONObject json = null;

String value = null;

try {

reader = getRequest().getReader();

value = reader.readLine();

} catch (IOException e) {

e.printStackTrace();

}

json = (JSONObject) JSONSerializer.toJSON(value);

String jsonString = json.getString("jsonString");

JSONObject jsonObject = JSONObject.fromObject(jsonString);

return jsonObject;

}

}

UserAction

import java.sql.Timestamp;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.context.annotation.Scope;

import org.springframework.stereotype.Controller;

import com.yuxu.common.MD5Util;

import com.yuxutech.biz.IUserBiz;

import com.yuxutech.pojo.BuyerExt;

import com.yuxutech.pojo.Role;

import com.yuxutech.pojo.User;

@Scope("prototype")

@Controller

public class UserAction extends BaseAction {

private static final long serialVersionUID = 1L;

private boolean flag = true;

private String info = null;

private String message = null;

private boolean isSuccess = true;

private User user = null;

@Autowired

private IUserBiz userBiz;

public String register() {

String name = null;

String password = null;

String address = null;

String phone = null;

String email = null;

String mac = null;

String areaId = null;

String role = null;

try {

JSONObject detail = readJsonObject();

name = detail.getString("name");

password = detail.getString("password");

address = detail.getString("address");

phone = detail.getString("phone");

email = detail.getString("email");

mac = detail.getString("mac");

areaId = detail.getString("areaId");

role = detail.getString("role");

} catch (Exception e) {

e.printStackTrace();

this.flag = false;

this.message = "Input type error!";

return "success";

}

if ((name.length() > 20) || (name.length() < 4)) {

this.info = "用户名长度为4-20位!";

this.isSuccess = false;

return "success";

}

if ((password.length() > 20) || (password.length() < 4)) {

this.info = "密码长度为4-20位!";

this.isSuccess = false;

return "success";

}

try {

User user = new User();

user.setId(Integer.valueOf(this.userBiz.getMaxId() + 1));

user.setName(name);

user.setPwd(MD5Util.getMD5(password));

user.setAddress(address);

user.setPhone(phone);

user.setEmail(email);

user.setRole(new Role(Integer.parseInt(role)));

user.setArea(areaId);

user.setState(Integer.valueOf(1));

BuyerExt buyerExt = new BuyerExt();

buyerExt.setMac(mac.replaceAll(":", ""));

buyerExt.setRegTime(new Timestamp(System.currentTimeMillis()));

buyerExt.setUser(user);

user.getBuyerExts().add(buyerExt);

String result = this.userBiz.register(user);

if (result.equals("注册成功!")) {

this.isSuccess = true;

this.info = result;

this.user = this.userBiz.findUserByName(name);

return "success";

}

this.isSuccess = false;

this.info = result;

this.user = null;

return "success";

} catch (Exception e) {

e.printStackTrace();

this.flag = false;

this.message = "System error!";

}

return "success";

}

public String validPwd() {

System.out.println("------------validPassword-------------");

int userId = 0;

String password = null;

try {

JSONObject jsonObject = super.readJsonObject();

userId = jsonObject.getInt("userId");

password = jsonObject.getString("password");

} catch (Exception e) {

e.printStackTrace();

this.flag = false;

this.message = "input type error!";

return "success";

}

try {

boolean isValid = this.userBiz.validPwd(userId,

MD5Util.getMD5(password));

if (isValid) {

this.info = "密码正确,验证成功!";

this.isSuccess = true;

} else {

this.info = "密码错误,请重新输入!";

this.isSuccess = false;

}

return "success";

} catch (Exception e) {

e.printStackTrace();

this.flag = false;

this.message = "system error!";

}

return "success";

}

public String getInfo() {

return this.info;

}

public String getMessage() {

return this.message;

}

public User getUser() {

return this.user;

}

public boolean isFlag() {

return this.flag;

}

public boolean getIsSuccess() {

return this.isSuccess;

}

} 下面的struts配置文件(部分代码),

flag,

message,

isSuccess,

info,

user\.id,

user\.address,

user\.name,

user\.phone,

user\.email,

user\.area

flag,

message,

detail\[\d+\]\.id,

detail\[\d+\]\.name,

detail\[\d+\]\.pid

不过现在并没有加密,只是初期的代码,以后还会继续上传加密之后的,当然如果你现在有更好的方法,请留言,多多交流!

原始地址:

http://www.voidcn.com/article/p-bjpzdlzh-bgd.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值