【问题标题】:Android: How to passing XML file to .net WebService and progress the XML file in Web Service?Android:如何将 XML 文件传递​​给 .net WebService 并在 Web Service 中处理 XML 文件?
【发布时间】:2012-01-18 11:01:30
【问题描述】:

我想使用以下代码创建一个 Xml 文件并将其传递给 .net Web 服务。但是,我不知道如何在 .net Web 服务中获取和处理这个 XML 文件。你能帮我解决这个问题吗?

public void registration(String[] data) {       

 String line = "<UserData
                Name=\"Username" Email = \"emaiAddress" Password = \"Password" +
                "SecurityQuestion = \"Question" SAnswer = \"SAnswer"/>";

 try {

  DefaultHttpClient httpClient = new DefaultHttpClient();

  HttpPost httpPost = new HttpPost(localhost + "UserData");

      StringEntity se = new StringEntity(line, HTTP.UTF_8);

      se.setContentType("text/xml");

  httpPost.setEntity(se);

  HttpResponse httpResponse = httpClient.execute(httpPost);
  HttpEntity httpEntity = httpResponse.getEntity();             

      line = EntityUtils.toString(httpEntity);

  } catch (UnsupportedEncodingException e) {            

  }
}

我可以知道如何从 Web 服务中的这个 xml 获取属性。我尝试使用以下代码。但是卡住了,不知道如何进行。

//user registration

[WebMethod]
public void registration(XmlDocument data)
{
     XmlDocument xml = data;
     var username = xml.GetElementsByTagName("Username");            



}

提前感谢您的帮助。

【问题讨论】:

    标签: android asp.net-mvc web-services dom xml-parsing


    【解决方案1】:

    试试这个。,

    public void registration(String[] data) {       
    
         String line = "<UserData
                        Name=\"Username" Email = \"emaiAddress" Password = \"Password" +
                        "SecurityQuestion = \"Question" SAnswer = \"SAnswer"/>";
    
         try {
    
          DefaultHttpClient httpClient = new DefaultHttpClient();
    
          HttpPost httpPost = new HttpPost(localhost + "UserData");
    
              StringEntity se = new StringEntity(line, HTTP.UTF_8);
    
              se.setContentType("text/xml");
    
          httpPost.setEntity(se);
    
          HttpResponse httpResponse = httpClient.execute(httpPost);
          HttpEntity httpEntity = httpResponse.getEntity();     
    
          if (httpResponse != null) {       
                        InputStream in = httpResponse.getEntity().getContent();
                        DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
                        Document doc = builder.parse(in);
    
                        if (doc != null) {
    
                            NodeList Username_node = doc.getElementsByTagName("Username");
                            String usrname = Username_node.item(0).getChildNodes().item(0).getNodeValue();
                            System.out.println(""+usrname);
                        }
                    } else {
                        showAlert(thisActivity, "No response");
                    }
    
          } catch (UnsupportedEncodingException e) {            
    
          }
          }
    

    【讨论】:

    • 感谢您的帮助。但是,我想在 .net Web 服务中读取 xml 文件,而不是在 android http post 中。你能帮我解决这个问题吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-07-09
    • 2011-10-17
    • 1970-01-01
    • 1970-01-01
    • 2011-06-27
    • 2011-09-18
    • 1970-01-01
    相关资源
    最近更新 更多