关于webservice服务器接收不到Android传递过来的参数的问题
16lz
2021-01-23
用.NET写了几个简单的接口,在Android上写了个客户端,用的KSOAP2,但发现使用Android通过WebService向服务器发送请求时,服务器接收不到Android传递的参数,通过调试发现:int类型的一直为0,String类型的一直为null。不知何故,或网络查询,或请教大牛,未果。只能换另外一种方法实现了
先是WebService,很简单的一个方法,当然还需要数据库
namespace webs{ /// <summary> /// WebService1 的摘要说明 /// </summary> [WebService(Namespace = "me")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] //[WebServiceBinding(ConformsTo = WsiProfiles.None)] [System.ComponentModel.ToolboxItem(false)] // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。 public class WebService1 : System.Web.Services.WebService { [WebMethod] public List<Center> GetClass(String centerCode, String StartDate) { string strCon = @"server=ECSHIT045;database=mydb;Trusted_Connection=yes"; SqlConnection MyCon = new SqlConnection(strCon); MyCon.Open(); string strSQL = "select * from center where CenterCode='" + centerCode + "' and Convert(varchar,startDate,120) like '" + StartDate + "%'"; SqlCommand cmd = new SqlCommand(strSQL, MyCon); SqlDataReader reader = cmd.ExecuteReader(); List<center> list = new List<center>(); while (reader.Read()) { int ID=(Int32)reader["ID"]; String code=reader["CenterCode"].ToString(); DateTime date=(DateTime)reader["startDate"]; String name=reader["CourseName"].ToString(); center c = new center(); c.ID1 = ID; c.CenterCode1 = centerCode; c.StartDate = date; c.CourseName1 = name; list.Add(c); } MyCon.Close(); return list; } }}
这里需要客户端传递两个参数过来
下面是要用到的一些属性
namespace webs{ public class center { private int ID; private String CenterCode; private DateTime startDate; private String CourseName; public int ID1 { get { return ID; } set { ID = value; } } public String CenterCode1 { get { return CenterCode; } set { CenterCode = value; } } public DateTime StartDate { get { return startDate; } set { startDate = value; } } public String CourseName1 { get { return CourseName; } set { CourseName = value; } } }}
Android客户端
因为没有使用KSOAP2,所以发送的请求需要自己封装
private String getRequestStr(String centerCode,String startDate) { StringBuilder sb = new StringBuilder(); sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>" + "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " + "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" " + "xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">" + "<soap:Body> <GetClass xmlns=\"me\">" + "<centerCode>" + centerCode + "</centerCode> <StartDate>"+startDate +"</StartDate> </GetClass>" + "</soap:Body></soap:Envelope>"); return sb.toString(); }这里需要向服务器传递两个参数
发送请求,获得响应
private static String getResponseXml(String centerCode,String startDate) throws Exception { String soap = getRequestStr(centerCode,startDate); if (soap == null) { return null; } URL url = new URL(URL); URLConnection conn = url.openConnection(); conn.setUseCaches(false); conn.setDoInput(true); conn.setDoOutput(true); conn.setRequestProperty("Content-Length", Integer.toString(soap.length())); conn.setRequestProperty("Content-Type", "text/xml; charset=utf-8"); conn.setRequestProperty("SOAPAction",ACTION); OutputStream os = conn.getOutputStream(); OutputStreamWriter osw = new OutputStreamWriter(os, "utf-8"); osw.write(soap); osw.flush(); osw.close(); BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream())); StringBuilder sBuilder = new StringBuilder(); String line = ""; for (line = br.readLine(); line != null; line = br.readLine()) { sBuilder.append(line); System.out.println(sBuilder); } return sBuilder.toString(); }解析
public static List<CenterBean> getAll(String centerCode,String startDate){ String result = null;try {result = getResponseXml(centerCode, startDate);} catch (Exception e1) {// TODO Auto-generated catch blocke1.printStackTrace();} DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = null;try {builder = factory.newDocumentBuilder();} catch (ParserConfigurationException e) {// TODO Auto-generated catch blocke.printStackTrace();} Document document = null;try {document = builder.parse(new InputSource(new StringReader(result)));} catch (SAXException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}List<InCenterClassBean> list=new ArrayList<InCenterClassBean>();Element root=document.getDocumentElement(); NodeList nodeList=root.getElementsByTagName("center"); for (int i = 0; i < nodeList.getLength(); i++) {Element childElement1=(Element) nodeList.item(i);NodeList child=childElement1.getChildNodes();CenterBean center=new CenterBean(); for(int j = 0;j<child.getLength();j++) { Node childNode = (Node)child.item(j); if (childNode.getNodeType()==Node.ELEMENT_NODE) { Element childElement = (Element)childNode; String name=childElement.getNodeName(); if ("ID".equals(childElement.getNodeName())) { center.setId(Integer.parseInt(childElement.getFirstChild().getNodeValue())); } else if ("CenterCode".equals(childElement.getNodeName())) { center.setCenterCode(childElement.getFirstChild().getNodeValue()); } else if ("startDate".equals(childElement.getNodeName())) { center.setStartDate(childElement.getFirstChild().getNodeValue()); } else if ("CourseName".equals(childElement.getNodeName())) { center.setCourseName(childElement.getFirstChild().getNodeValue()); } } list.add(center);} return list; }
最后不要忘了在AndroidManifest文件中加入网络访问的权限
这里为图方便,将请求网络的事件写在主线程中了,建议大家改为多线程
THE END
更多相关文章
- [转]android开发新浪微博客户端 完整攻略 [新手必读]
- android客户端与服务器交互数据(基于SAOP协议的远程调用标准,通过w
- Android 如何动态设置View参数,LayoutParams.addRules详解,TypedVa
- 获取Android设备常规参数信息(SN,IMEI)及定制信息
- [android]-如何在向服务器发送request时附加已保存的cookie数据
- 转载--Android 开发 调用图库选择图片实现和参数详解
- 多个Android客户端同步服务器端表中数据架构分析
- Android客户端和php+mysql+apache搭建的服务器之间的简单交互