2012-01-07 124 views
6

Android應用程序開發非常新。在我的新Android應用程序中,我想顯示來自webservice的一些數據。這意味着我有一個SOAP message,我需要解析來自SOAP響應的數據。在iPhone應用程序中,我很清楚解析SOAP消息響應,但在Android中,我不知道如何執行此操作?我在谷歌搜索很多,並得到一些想法。但是,這很令人困惑。任何人都可以請建議任何最簡單的方法來理解SOAP發送請求/接收響應和解析(XML format)在SAXParserAndroid響應?我在我的項目中安裝了ksoap2-android-assembly-2.6.0-jar-with-dependencies.jar。在這裏,我發現了一些示例代碼,我在這裏發佈,如何在Android中以XML格式發送SOAP請求和解析SOAP響應?

import java.io.*; 
import org.ksoap2.SoapEnvelope; 
import org.kxml2.io.KXmlParser; 
import org.xmlpull.v1.XmlPullParserException; 


public class ParsingSteps 
{ 
    public static void main(String[] args) 
    { 
     try{ 
      // String msg="<hello>World!</hello>"; 
       String msg = "<SOAP-ENV:Envelope " + "xmlns:SOAP-ENV=\"http:// 
www.w3.org/2001/12/soap-envelope\" " + "xmlns:xsi=\"http://www.w3.org/ 
2001/XMLSchema-instance <http://www.w3.org/%0A2001/XMLSchema-instance>\"" 
+"xmlns:xsd=\"http://www.w3.org/2001/ 
XMLSchema\"& gt;" + 
        "<SOAP-ENV:Body>" + 
        "<result>" + 
        "<message xsi:type=\"xsd:string\">Hello World</message>" + 
        "</result>" + 
        "</SOAP-ENV:Body>" + 
        "</SOAP-ENV:Envelope>"; 

     //  byte[] in= msg.getBytes(); 

       KXmlParser parser=new KXmlParser(); 
       parser.setInput(new StringReader(msg)); 
      SoapEnvelope soapenvelope= new SoapEnvelope 
(SoapEnvelope.VER12); 
       //soapenvelope.parse(parser); 
       soapenvelope.parseBody(parser); 

         } 
       catch (IOException e) { 
              System.out.println("Error reading URI: " + e.getMessage()); 
     } catch (XmlPullParserException e) { 
             System.out.println("Error in parsing: " + e.getMessage()); 
       } 
     //  String result=parser.getName(); 
       //System.out.println(result); 
   } 
} 

這是正確的代碼。請對我的問題提出任何建議。請幫助我。提前致謝。

回答

5


谷歌對於Ksoap2教程你會得到很多。以下是將請求發送到Web服務的示例代碼。

public class WebServicePoc extends Activity{ 
private static final String SOAP_ACTION = "http://tempuri.org/Arnoid"; 
private static final String METHOD_NAME = "Arnoid"; 
private static final String NAMESPACE = "http://tempuri.org/"; 
private static final String URL = "http://ipaddress:port/UserAuthenticationInterfacer.asmx"; 
EditText editText; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    HashMap<String, String> a=new HashMap<String, String>(); 
    try { 

     SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); 
     request.addProperty("FOSID", "1994"); 
     request.addProperty("IMEINumber", ""); 
     request.addProperty("SIMCardNo", ""); 
     request.addProperty("ApplicationName", "App"); 
     request.addProperty("CurrentVersion", "1.0.0.0"); 
     SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 
     envelope.dotNet=true; 
     envelope.setOutputSoapObject(request); 
     AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL); 
     androidHttpTransport.call(SOAP_ACTION, envelope); 
     SoapObject result = (SoapObject)envelope.getResponse(); 
     editText=(EditText)findViewById(R.id.text1); 
     editText.setText(result.toString()); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

而對於xml請爲xml解析器檢查教程,因爲在android中不支持STAX,所以請僅使用SAX。對於發送XML請求,你可以將xml作爲字符串發送,然後在服務器端進行解碼。

+0

非常感謝您的回覆。我將把你的想法融入到我的項目中。我希望你的幫助在將來。謝謝。 – Gopinath 2012-01-07 06:05:44

+0

@Gopinath如何設置屬性以發送正確的消息? – arniotaki 2014-10-10 13:40:34