Receiving custom objects from SOAP-based .NET web service in Android - 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.

Related

Issues fetching JSON string with ksoap2 in android

I am experiencing what I find to be a VERY strange problem currently. I have database with about 1000 records. I have a web service for fetching these and turning them into a json string. Testing this using soapui or another web service client works perfectly fine.
My problem is that when I fetch the data using ksoap2 in android it crashes, and here is the crazy part: it only crashes sometimes!?!?
After quite a bit of debugging I have noticed that the JSON i get in android is ALMOST the same as the one I get in eg. SOAPUI.
The problems I have seen are
it has removed a ":" between a key and value- eg. "id""2" instead of "id":"2"
it might be missing a '"' on either side of a key or a value. eg: "id:"2" instead of "id":"2"
An entire key has been removed, eg: "name":"myname","2" instead of "name":"myname","id":"2". Furthermore it is NOT the same place that something is removed every time...
I am seeing these errors (when I use logs) before I start parsing the json (which of course fails if it is not valid json). Ss the web service is returning the correct thing (according to SOAPUI) I'm assuming it is ksoap that somehow messes up parsing the soap response.
Keep in mind as well that it occasionally works and the json has all the data and is successfully parsed!
Here is my code calling the web service (The json is ~345.000 characters):
SoapObject request = new SoapObject(NAMESPACE, method);
input.addPropsToRequest(request);
SoapSerializationEnvelope envelope = new
SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.implicitTypes = true;
envelope.dotNet = false;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.debug = true;
androidHttpTransport.call(soap_action, envelope);
String json = envelope.getResponse().toString();

Soap Parsing java based wsdl url no response in Android

I have a WSDL java based webservice and an android project.I didnt get any response from that wsdl url.
I couldn't solve this problem.
Here is Android Code Part.
try
{
SoapObject request = new SoapObject( "http://keycloud.noip.me:8081/knowledgecenterservice/webservice/ContentManagementService" , "getImageFromDistrict " );
request.addProperty( "district" , "Bangalore" );
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope( SoapEnvelope.VER11 );
new MarshalBase64().register( envelope ); //serialization
envelope.encodingStyle = SoapEnvelope.ENC;
envelope.setOutputSoapObject( request );
HttpTransportSE transport = new HttpTransportSE( "http://keycloud.noip.me:8081/knowledgecenterservice/webservice/ContentManagementService" );
transport.call( "http://keycloud.noip.me:8081/knowledgecenterservice/webservice/ContentManagementService/getImageFromDistrict" , envelope );
SoapObject value = ( SoapObject ) envelope.getResponse();
text.setText(value.toString());
dialog.dismiss();
}catch(Exception e)
{
}
Try changing
androidHttpTransport.call("", envelope);
to
androidHttpTransport.call(METHOD_NAME, envelope);
To check the response from server
Install SOA Client Add-on on Mozilla Firefox.
I checked the response from server using the above specified URL i.e
http://keycloud.noip.me:8081/ekrishi/webservice/TestService?wsdl
and I happen to see that the method is not requesting for any parameters/inputs. So I don't know why did you wirte this line request.addProperty("district","Bangalore");
And I just invoked that method without any parameters and I got the error
faultcode: soap:Server -->means there's a problem on server side. So check with the person who has written this webservice.
faultstring: Fault occurred while processing.
Hope this information is useful for you :)
EDIT I have attached the snapshot of the error I got using SOA client. If everything is fine on server side, we will not get any error. I strongly believe that there's a problem on server side. Check your URL once.
See here the method is not requesting for any parameters. So no need to add -- request.addProperty("district", "Bangalore");. Below is the screenshot of that.
I feel like the URL you are using in wrong. Cross check it once.

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.

Solutions for cookieless session in aspx web service

I have a problem that i dont know how to solve, few days ago i posted a question about using session web service with ksoap. ksoap session web service The problem was that ksoap doesn`t have cookie option so that the sessionID can be stored. But i though about a possible solution, which will work with cookieless session, (sending the sessionID inside the url). The problem is that the session in the web service doesnt work in "cookieless mode", only with cookies which i cant use.
Another idea was to implement the same web service methods inside a aspx page so that the cookieless session will work there, but i dont know how to call a page method.
And since i dont know how to call a page method, i thought that i should call it from my web service and just resend the returned data.
So i need a way to implement my solutions:
1) A way to make cookieless session work in a asmx web service
2) A way to call a page web method with ksoap so that the cookiless session will work there
3) Maybe a way to call an aspx page method from web service method so that i use the web service method like a "bridge" which will resend the page methods returned data.
String NAMESPACE = "http://tempuri.org/";
String METHOD_NAME = "Test";
String SOAP_ACTION = "http://tempuri.org/Test";
String URL = "http://mysite.com/WebService.asmx";
SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);
PropertyInfo pi = new PropertyInfo();
pi.setName("s");
pi.setValue("dddddddd");
pi.setType(String.class);
Request.addProperty(pi);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(Request);
envelope.addMapping(NAMESPACE, "Category",new Category().getClass());
AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL);
androidHttpTransport.call(SOAP_ACTION, envelope);
SoapObject response = (SoapObject)envelope.getResponse();
........................
And my web service method returns a list of Category objects (that is tested and working, but not if i use session).
Does anyone has a proposal??
I was able to do it like so
Basically I'd call login web service and save the Set-Cookie header value that came in the response. Then returning the values in subsequent calls to other web methods.
There will be two 'Set-Cookie' header keys that look something like this:
Set-Cookie: ASP.NET_SessionId=zpe44yb05a105zuz2f24aqi5; path=/;
HttpOnly
Set-Cookie: .ASPXAUTH=4BEC23D4FA4B34C7145550F2FE279E047ED2E16F1503896C949AC3A08DAAC932F6869D9B667A6735D6EA6419074F0F1C34BC3F08210904B2BB9E543CF4CCE6C2E70975F804E2447F60966406ADC1F015D267B0413FF4C79A252C55E904AD86BB315ED161300DF200163CFA77492D680ACCF9C763F3F61F70DA4E8261C9DB6466;
path=/; HttpOnly
Save them both and call subsequent "secure" webmethods with both values concatinated with a ; between them.
The header that you include in subsequent calls should be like this:
Cookie: ASP.NET_SessionId=zpe44yb05a105zuz2f24aqi5; path=/; HttpOnly;
.ASPXAUTH=4BEC23D4FA4B34C7145550F2FE279E047ED2E16F1503896C949AC3A08DAAC932F6869D9B667A6735D6EA6419074F0F1C34BC3F08210904B2BB9E543CF4CCE6C2E70975F804E2447F60966406ADC1F015D267B0413FF4C79A252C55E904AD86BB315ED161300DF200163CFA77492D680ACCF9C763F3F61F70DA4E8261C9DB6466;
path=/; HttpOnly

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

Categories

Resources