android 服务端如何获取客户端传递来的数据(在线 100分)

飘飘哥 飘飘哥 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
客户端代码

String url ="http://192.168.1.160:8080/MDXT/padlogin.action";

HttpPost request = new HttpPost(url);
// 先封装一个 JSON 对象
JSONObject param = new JSONObject();
try {
param.put("name", "rarnu");
param.put("password", "123456");

// 绑定到请求 Entry
StringEntity se = new StringEntity(param.toString());
request.setEntity(se);

// 发送请求
HttpResponse httpResponse = new DefaultHttpClient().execute(request);
JSONObject result = null;
int code=httpResponse.getStatusLine().getStatusCode();
if (code == 200) {
// 得到应答的字符串,这也是一个 JSON 格式保存的数据
String retSrc = null;
retSrc = EntityUtils.toString(httpResponse.getEntity(),"utf-8");
JSONObject jtmpJsonObject = new JSONObject(retSrc);
String str = jtmpJsonObject.getString("username");// 此处"dataMap"与服务器关联
System.out.println("用户名+**********************"+str);
}



} catch (JSONException e) {
e.printStackTrace();
}

注:想知道 request.setEntity(se); 我这样设置的值 怎么在服务端获取,
如果是说让我用http://192.168.1.160:8080/MDXT/padlogin.action?usern='ss’&pas='xx' 这种方式的话 那就算了,因为我主要是想知道数据在服务器端是怎么接受的

服务端代码
服务端本人采用的是struts2
struts2的配置文件 就不贴出来了
现在服务器端的数据 我在客户端能接收到 但是就是不知道怎么接受客户端传递来的数据
public String loginPad(){

HttpServletRequest req=ServletActionContext.getRequest();
HttpServletResponse resp=ServletActionContext.getResponse();


LoginDao login = new LoginDao();
JSONObject json = new JSONObject();
System.out.println("lai ..........................");
resp.setCharacterEncoding("GBK");
try {
String data = req.getParameter("name");
json.put("username", "by");
json.put("mdname", "xxx");

System.out.println("lai .........................."+data);
//返回json 格式的数据
resp.getWriter().write(json.toString());

//MenDianInfo mdInfo = login.isExistedUser(username, password);


} catch (Exception e) {
e.printStackTrace();
}

return null;
}


在线等,谢谢各位大神
...全文

相关推荐

评论 5
收藏 已收藏
分享
返回顶部
飘飘哥
Android 80,490 成员 91,371 内容