Set Soap Header ksoap2 android - android

First, I apologize for asking a question that is already common here in the SOF.
But I am a beginner and I'm certainly cruel.
I am creating an android application that communicates with a WS. So I can make requests to the WS, I have to add a value to the header of the envelope, but I can not add.
I found some answers about it here in the SOF, however, could not fully understand how it works. Perhaps, my doubts are due to the nodes of the header, which ended up confusing me even more.
One of the answers I found I ended up not helping: "How to set soap header using ksoap2 android"
Below is the XML request that needs to be done:
?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:Header
ValidationSoapHeader xmlns="http://tempuri.org/"
DevToken>string/DevToken
/ValidationSoapHeader
/soap:Header
soap:Body
ListaCidades xmlns="http://tempuri.org/" /
/soap:Body
/soap:Envelope
And my code below:
SoapObject request = new SoapObject(ApplicationData.NAMESPACE, ApplicationData.METHOD_NAME_LISTA_CIDADES);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
How exactly do I use the envelope.HeaderOut? Is it really necessary to create a helper method to build an Element even having to pass only one parameter (DevToken)?
Thank you for your attention!

Element h = new Element().createElement(NAMESPACE, "AuthHeader");
Element Username = new Element().createElement(NAMESPACE, "Username");
Username.addChild(Node.TEXT, "CBROWN");
h.addChild(Node.ELEMENT, Username);
Element wssePassword = new Element().createElement(NAMESPACE, "wssePassword");
wssePassword.addChild(Node.TEXT, "welcome");
h.addChild(Node.ELEMENT, wssePassword);
envelope.headerOut = new Element[]{h};
add above code for add header in envelope

Related

how to get data from soap into my android

Ive build an app that use sql ws to get data. i can get the data but because its a String I dont know how to split it into some verbals.
this is mt soqp rsponse
<?xml version="1.0" encoding="UTF-8"?><S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header/>
<S:Body>
<ns2:getDataFromSqlResponse xmlns:ns2="http://myAndroidIt.gy.com/">
<return>id- 2 first name- hanane last name- yniv
</return>
</ns2:getDataFromSqlResponse>
</S:Body>
</S:Envelope>
how can i put only the id (2) into verbals?
I want to use it.
thx!
I suggest you use a library like ksoap2-android for calling Soap Webservices. You can add the library to your project with maven like this:
<dependency>
<groupId>com.google.code.ksoap2-android</groupId>
<artifactId>ksoap2-android</artifactId>
<version>3.2.0</version>
</dependency>
If you don't use maven you can find the download link for the jar here.
You can use the ksoap2 library to make soap calls like this:
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);         
// Here you could add things to your request like this:
request.addProperty("parameter1", 7);
request.addProperty("parameter2", "asdf");
request.addProperty("parameter3", someChildSoapObject);
SoapSerializationEnvelope envelope = new SSoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
          
HttpTransportSE httpTransport = new HttpTransportSE(URL);
httpTransport.call(SOAP_ACTION, envelope);
SoapObject response = (SoapObject)envelope.getResponse();
After you have your response object you can parse the values you need from the response object like this:
int id = Integer.parseInt(response.getPropertySafely("id").toString());
String someOtherProperty = resonse.getPropertySafelyAsString("someProperty");
You can find more information about how to use ksoap2-android here.
If you don't want to or can't use a soap library i would suggest you use a serializer framework like Simple to deserialize the String. You can find Simple here.
I actually once used Simple to write a small soap framework when it was impossible to use a library so I can recommend this if you can't use ksoap2-android.

Android sending data to server with soap

I'm new to android and I want to get data from user through edittext in android and send it to server with the help of soap request i.e. in xml format,can somebody please help me reply a.s.a.p.???
You can send the request in xml format as shown in the xml below. Modify the same according to your needs.
Example. (using KSOAP2 library)
SoapObject request = new SoapObject("http://service.medal.org/", "GetPosts");
PropertyInfo getpostreq = new PropertyInfo();
getpostreq.name="GetPostsReq";
getpostreq.type=String.class;
getpostreq.setValue("<GetPostsReq>"
+"<sessionId>"+sessionid+"</sessionId>"
+"<postedAfter>"+5+"</postedAfter>"
+"<postedBefore>"+20+"</postedBefore>"
+"<radius>10</radius>"
+"<location>"
+"<latitude>"+lati+"</latitude>"
+"<longitude>"+longi+"</longitude>"
+"</location>"
+"<postedBy>all</postedBy>"
+"</GetPostsReq>");
request.addProperty(getpostreq);
SoapSerializationEnvelope envelop = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelop.setOutputSoapObject(reques

Removing i:type field in SOAP request generated by kSoap2 on Android

I already tried reading the internet about my issue, but I could not find the right information I need, so I try to explain my issue:
I am using kSoap2 to "talk" to a webservice over SOAP.
To generate my SOAP request I use the following code:
// Generate SOAP request XML
SoapObject request = new SoapObject(PUB_NAMESPACE,
"testSoapInterface");
// Add request header
PropertyInfo requestHeader = new PropertyInfo();
requestHeader.setNamespace(PUB_NAMESPACE);
requestHeader.setName("requestheader");
// Generate username property
PropertyInfo usernameProp = new PropertyInfo();
usernameProp.setNamespace(BASE_NAMESPACE);
usernameProp.setName("username");
usernameProp.setValue(username);
// Generate applicationId property
PropertyInfo applicationIdProp = new PropertyInfo();
applicationIdProp.setNamespace(BASE_NAMESPACE);
applicationIdProp.setName("applicationId");
applicationIdProp.setValue("test");
// Add properties to requestHeader (nested)
requestHeader.setValue(new SoapObject(PUB_NAMESPACE, "requestheader")
.addProperty(usernameProp)
.addProperty(applicationIdProp));
request.addProperty(requestHeader);
Now, to serialize this, I use the following:
// Serialize SOAP request to the non .NET based SOAP server
SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
soapEnvelope.dotNet = false;
soapEnvelope.implicitTypes = true;
soapEnvelope.setAddAdornments(false);
soapEnvelope.setOutputSoapObject(request);
Because I am using nested soap (the requestheader consists of applicationId and username) I can imagine that this might be the cause.
I also have to use different namespaces for the different lines, which can also be a cause.
Can anybody help me on this??
Thanks!
You can use implicitTypes property of your envelope:
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.implicitTypes = true;
In this way the generated xml will not contain i:type.
Had the same Problem, seems to be impossible to use PropertyInfo without generating any i:type with it. Nice solution would be to Override AddProperty(PropertyInfo pi) so it works on any case without the i:Type.
Got three solutions to offer:
1
If u dont need the Namespace then request.AddProperty(name,value) does it!
2
U could make your request header an own SoapObject, it wont use the "i:type".
SoapObject requestHeader = new SoapObject(NAMESPACE,"requestheader");
and in the last line
request.AddSoapObject(requestHeader);
3
For me it worked to set the Version of the SoapEnvelope to "VER10" since the types are ignored then. They're still in your request but ignored.
Replace:SoapEnvelope.VER11 with:SoapEnvelope.VER10
Where ever you are creating
SoapSerializationEnvelope sEnvelop;
just assign sEnvelop.implicitTypes = true;
it will not create "i:type="d:string"" or "i:type="d:long"" inner datatype tag and web service can execute successfully

Receiving custom objects from SOAP-based .NET web service in Android

I'm developing an Android app which needs to connect to a .NET SOAP web service and generate/populate a number of objects from the response. I'm totally new to web services and SOAP, and relatively new to Android. The web service has already been built (not by me).
I've been trying to find info on connecting to SOAP web services in Android. The two basic suggestions seem to be:
Don't use SOAP,
If you do use SOAP, use KSOAP2.
I've looked at various tutorials for KSOAP2 but they all seem to deal only with the most basic, primitive types, such as sending 2 ints to get 1 int back, or sending and receiving strings;, however, I need to send custom objects back and forth.
Is it possible to send and receive custom objects using KSOAP2? If so, how easy/difficult is this? Does the XML have to be parsed to create/populate the objects "by hand"?
EDIT/UPDATE:
After trying for a while to connect to the existing webservice (takes a String and returns a complex object), and getting an error, I decided to try a simpler approach. I connected to a simpler webservice which takes no parameters, and returns an int. This simpler webservice is hosted in the same place as the original one, and the simple one now works fine for me.
FURTHER UPDATE:
All working fine now! Turns out I just had a capitalisation error which was throwing off my parameter.
The final problem was "national" versus "National". Here's the soapUI-generated code:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">
<soapenv:Header/>
<soapenv:Body>
<tem:GetClientByNationalID>
<!--Optional:-->
<tem:nationalID>XXXXXXX</tem:nationalID>
</tem:GetClientByNationalID>
</soapenv:Body>
</soapenv:Envelope>
And the request code which was being generated by my Java:
<v:Envelope xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:d="http://www.w3.org/2001/XMLSchema" xmlns:c="http://schemas.xmlsoap.org/soap/encoding/" xmlns:v="http://schemas.xmlsoap.org/soap/envelope/">
<v:Header />
<v:Body>
<GetClientByNationalID xmlns="http://tempuri.org/" id="o0" c:root="1">
<NationalID i:type="d:string">
XXXXXXXX
</NationalID>
</GetClientByNationalID>
</v:Body>
</v:Envelope>
My final java code is:
public void webServiceCall() {
String NAMESPACE = "http://tempuri.org/";
String METHOD_NAME = "GetClientByNationalID";
String SOAP_ACTION = "http://tempuri.org/IClientService/GetClientByNationalID";
// unsecure service
String URL = "http://XXXXXXXXXXXXXXXXXXXX.net/FPUnSecureService/ClientService.svc";
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
String clientID = "XXXXXXX";
PropertyInfo pi = new PropertyInfo();
pi.setName("nationalID");
pi.setValue(clientID);
pi.setType(clientID.getClass());
request.addProperty(pi);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
envelope.addMapping(NAMESPACE, "GetClientByNationalID", new ClientID().getClass());
Marshal floatMarshal = new MarshalFloat();
floatMarshal.register(envelope);
HttpTransportSE t = new HttpTransportSE(URL);
ClientID client = new ClientID();
t.debug = true;
try {
Log.i("webServiceCall", "Trying call to web service");
t.call(SOAP_ACTION, envelope);
SoapObject response = (SoapObject) envelope.getResponse();
Log.i("success", response.getPropertyAsString(0));
Log.i("success", response.getPropertyAsString(1));
Log.i("success", response.getPropertyAsString(2));
Log.i("success", response.getPropertyAsString(3));
Log.i("success", response.getPropertyAsString(4));
} catch (Exception e) {
Log.e("webServiceCall", "Error calling webservice.");
e.printStackTrace();
System.out.println("Request: " + t.requestDump);
System.out.println("Response: " + t.responseDump);
}
}
I am still confused about these lines:
envelope.addMapping(NAMESPACE, "GetClientByNationalID", new ClientID().
Marshal floatMarshal = new MarshalFloat();
floatMarshal.register(envelope);
I think I added the Marshal parts when I was passing an object as a parameter, I'm not sure if I still need them. I'm also not sure about the addMapping call either, and what I should have in there. Thanks.
Use ksoap2, and check my answers with code examples at the following links: Link1,link2,link3. I wrote some details which will help you understand how to start coding.
let me know if u need help.
UPDATE ( Answering your question about mapping and marshalling)
u use addMapping when you are Sending a complex type ( ie an object) through the soap envelope. How will the webservice recognize the complex type ? u need to create locally a class that implement kvmserializable (as mentionned in my links) which will have same parameter as the object on the server, and then u need to add mapping between it and the class that maps to it on the server, so that the enveloppe when parsed on the server it knows this complex type X map to class X on the server. So if you are not Sending complex type, you don't need to add mapping.(PS:I see that your not sending a complex type since nationalID is of type string. IF lets say nationalID was a complex type of type Z you would do : addMapping(NAMESPACE, Z.class.getSimpleName(), Z.class) )
As for marshalling, it uses java serialization to change Objects to stream of data to be unmarshalled on the web service. So when u are sending a complex type,based on my experience, it is a good practice to add marshalling to change the complex type to streams of data by serializing it. If you find that you don't need it just don't add it, but its always good to understand what are the options out there.
Look at this http://seesharpgears.blogspot.com/2010/10/ksoap-android-web-service-tutorial-with.html , there are examples of how to pass complex objects.

How to use OCR web service in android application. How can we send request and get response?

How can we use OCR web service in android application
I have use this webservice.
How can i pass data using soap base web service and get response back.
How can i pass request for nested XML tags ?
http://www.ocrwebservice.com/services/OCRWebService.asmx?op=OCRWebServiceRecognize
Please help..
Please check this library for Android: kSoap2.
Also, check this similar question, that offers a few other solutions: "How to call web service with Android"
Hope it helps!
Actually i got the correct answer. For nested request i wrote the below code and it worked.
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
// Add input values to SOAPObject object - request
request.addProperty("user_name", "test");
request.addProperty("license_code",
"test");
// Add property for nested tags
PropertyInfo pi = new PropertyInfo();
pi.setName("OCRWSSetting");
pi.setValue(new SoapObject(NAMESPACE, "OCRWSSettings")
.addProperty("ocrLanguages", "ENGLISH")
.addProperty("outputDocumentFormat", "TXT")
.addProperty("convertToBW", false)
.addProperty("getOCRText", true)
.addProperty("createOutputDocument", false)
.addProperty("multiPageDoc", false)
.addProperty("ocrWords", false));
request.addProperty(pi);
// Add property for another nested tags
pi = new PropertyInfo();
pi.setName("OCRWSInputImage");
pi.setValue(new SoapObject(NAMESPACE, "OCRWSInputImage")
.addProperty("fileName", getString(R.string.file_name))
.addProperty("fileData",base64String)
);
request.addProperty(pi);

Categories

Resources