如何在android中访问Webservice?

时间:2012-01-11 10:17:43

标签: android web-services

我是android开发的新手,我需要使用SOAP消息从Webservice获取XML文件。我已经尝试了我的最佳水平,在Android中找到SOAP消息解析但是,我无法找到SOAP消息解析的确切解决方案。在这里,我附上了我的示例代码来解析SOAP消息。你能帮我解析一下Android中的SOAP消息吗? (回复得到许可否认)。

我尝试了下面给出的源代码:

SoapObject request = new SoapObject(NAMESPACE ,METHOD_NAME);
request.addProperty("username","d");
request.addProperty("password","d123");
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet=true;
envelope.setOutputSoapObject(request);envelope.setAddAdornments(true);
HttpTransportSE httpTransport = new HttpTransportSE(URL);  
try 
  {
      httpTransport.call(SOAP_ACTION, envelope); //send request
      SoapObject result=(SoapObject)envelope.bodyIn;
      String results = result.toString();
      tv.setText( ""+results);
  }
  catch (Exception e)
  {
       tv.setText(e.getMessage());
   }

AndroidManifest.Xml file:

<uses-permission android:name="android.permission.INTERNET">
</uses-permission>

2 个答案:

答案 0 :(得分:1)

您的代码中缺少envelope.getResponse();,可能导致您无法获得响应。

你可以尝试,

SoapObject result=(SoapObject)envelope.getResponse();
String results = result.toString();

答案 1 :(得分:0)

你可以这样做

        HttpPost httppost = new HttpPost(webServicePath);
    httppost.setHeader("Content-Type", "text/xml;charset=UTF-8");

    HttpParams params = new BasicHttpParams();
    params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION,
            HttpVersion.HTTP_1_1);

    String soapRequestXML = getXMLAsString();
    soapRequestXML = prepareInputParam(soapRequestXML);

    StringEntity se;
    se = new StringEntity(soapRequestXML, HTTP.UTF_8);
    se.setContentType("text/xml");
    httppost.setEntity(se);

    response = httpClient.execute(httppost);