Add header to Http Header - android

I am using ksoap2 for android project. But server code (which cannot be changed) is old and needs send some properties inside a header on HTTP header.
Server use soap so I decided to use ksoap2 for android but I couldn't find any example how to set http header of ksoap2 request(not inside soap envelope but inside of httpheader).
Is it possible?
If possible can anyone show me a example?

HTTP headers could be modified inside org.ksoap2.transport.call(String, SoapEnvelope, List, File) method.
EDIT
You can pass extra headers directly when calling HttpTransportSE.call()
example:
.
.
.
List<HeaderProperty> headers;
headers.add(new HeaderProperty("Content-Type", "utf8"));
headers.add(new HeaderProperty("Accept", "text/html"));
HttpTransportSE httpTransport = new HttpTransportSE(_soapAddress);
httpTransport.call(soapAction, envelope, headers);
.
.
.

You can retrieve the connection object and add the headers there.
HttpTransportSE transport = new HttpTransportSE(url,timeout);
ServiceConnection conn = transport.getConnection();
conn.setRequestProperty("Accept-Encoding", "utf-8");
//and others...

Related

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.

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.

Categories

Resources