How to send SOAP request and Parse SOAP XML response in Android?

I am very new to Android app development. In my new Android app, I want to show some data from webservice. This means that I have a SOAP message , I need to parse the data from the SOAP response. In the iPhone application, I was very well versed in the response of the SOAP message, but in android I do not know how to do this? I searched a lot on google and got some ideas. But I am very embarrassed about this. Can anyone suggest any simple way to understand the response of a SOAP request / response and SAXParser ( XML format ) the response in SAXParser on Android ? I installed ksoap2-android-assembly-2.6.0-jar-with-dependencies.jar in my project. Here I found some code examples, I post here,

 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);  } } 

This is the correct code. Please give any suggestion on my question. Please help me with this. Thanks in advance.

+6
source share
1 answer


Google for the Ksoap2 tutorial and get a lot of them. Here is a sample code to send a request to a web service.

 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(); } 

And to check xml pls for xml parsers, use only SAX, since STAX is not supported in android. To send an xml request, you can send xml as a string and then decode on the server side.

+5
source

Source: https://habr.com/ru/post/905381/


All Articles