android 服务端如何获取客户端传递来的数据(在线 100分)
客户端代码
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;
}
在线等,谢谢各位大神
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;
}
在线等,谢谢各位大神
...全文
相关推荐
Android Socket服务端与客户端用字符串的方式互相传递图片的方法
主要介绍了Android Socket服务端与客户端用字符串的方式互相传递图片的方法的相关资料,需要的朋友可以参考下
100多个Android Demo的集合
几乎涵盖Android的所有组件,来自“安卓巴士”,感谢作者的奉献
Android小项目集合100多个
是学习和联系Android小项目的使用资料
Android编程使用Intent传递图片的方法详解
主要介绍了Android编程使用Intent传递图片的方法,结合实例形式分析了Android基于Intent传输图片的原理与具体实现技巧,需要的朋友可以参考下
Android中传递图片的2种方法
主要介绍了Android中传递图片的2种方法,本文分别给出种方法的操作代码实例,需要的朋友可以参考下


