RSS

Passing string array to c# web service from Android

25 Mar

If you have below xml to parse

<?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>
    <Sample xmlns="http://MTKAndroidService.org/">
      <array>
        <string>string</string>
        <string>string</string>
      </array>
    </Sample>
  </soap:Body>
</soap:Envelope>

Then use function below to call particular webservice by passing array object to it

public String Sample(){ 
        String SOAP_ACTION = "http://MTKAndroidService.org/Sample";
        String METHOD_NAME = "Sample";
//      URL =  "http://10.0.2.2:49923/Service1.asmx";   // to be adjusted to the URL above once this code is added into WebService;
        String IP_LIST="";
        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); 
        List logs =  new ArrayList();
        logs.add("hello");
        logs.add("world");
        SoapObject soapLogs = new SoapObject(NAMESPACE, "logs");
        for (String i : logs){
            soapLogs.addProperty("string", i);
        }
        request.addSoapObject(soapLogs);

        SoapSerializationEnvelope IPenvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        IPenvelope.dotNet = true;
        IPenvelope.setOutputSoapObject(request);
        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
            try {
                androidHttpTransport.call(SOAP_ACTION, IPenvelope);
                SoapPrimitive response = (SoapPrimitive)IPenvelope.getResponse();
                Log.i("myApp", response.toString());
                IP_LIST= response.toString();
            } 
            catch (Exception e) {
                e.printStackTrace();
            }
            return IP_LIST;
    }
Advertisements
 
Leave a comment

Posted by on March 25, 2014 in Android Source Code

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: