当前位置: 代码迷 >> Android >> android 服务端怎么获取客户端传递来的数据(在线 100分)
  详细解决方案

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

热度:47   发布时间:2016-04-28 02:36:42.0
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 data = req.getParameter("name");
这不就获取到了吗
------解决思路----------------------
String data = req.getParameter("name");这句话就是获取传过来的值
------解决思路----------------------
String data = req.getParameter("name");

从客户端传输数据是通过HTTP协议的,所以HTTP传输的内容都固定封装在Request 对象中。

上面的代码就是从HTTP请求的Request 对象中获取一个叫name的参数的值,类似键值对。获取到的值就是你在客户端塞的一个叫name的参数的值。

不知这么说,你可懂?
  相关解决方案