Android中使用HttpUrlConnection连接SpringMVC后台,实现登陆如何传用户名和密码

本文讨论了在Android应用中使用HttpUrlConnection连接SpringMVC后台进行登录时遇到的问题。后台设置了@appLogin方法处理登录请求,但Android端发送请求后收到400错误。问题可能在于URL构造和参数传递。尝试使用URLEncoder编码用户名和密码,但在日志中发现Socket断开的提示。寻求解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

后台做个一个appLogin的方法
类是UserControl
@Controller
@RequestMapping("/user")
这个是登陆的代码
@RequestMapping("/appLogin")
public String appLogin(
@ModelAttribute("command") Usera user,
@PathVariable String userName,
@PathVariable String password,
Model model,HttpSession session,HttpServletResponse response) throws IOException{
String loginResult = userDao.login(userName, password);
if (loginResult!=null&&loginResult!="error") {
session.setAttribute("loginInfo", loginResult);
//相应登陆成功的信息 通过response输出流输出
response.getOutputStream().write("登陆成功".getBytes());
return null;
}else{
response.getOutputStream().write("登陆失败".getBytes());
return "/loginPage";
}
}

然后android中使用HttpUrlConnection方法
String spec = "http://192.168.1.66:8080/graduation/user/appLogin";
            URL url = new URL(spec);
            HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();

但是urlConnection.getResponseCode 的代码是400...
想咨询一下我的Url写的不对么?应该怎么写
还想问一下我的参数传值对不对
String data = "name="+URLEncoder.encode(name,"UTF-8")+"&password="+URLEncoder.encode(password,"UTF-8");

最后logcat总是在提示:
04-21 04:37:05.910 82-82/? D/: Socket deconnection
有没有大神可以给我看一下
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值