how to parse through complex soap objects in Android - android

Hello Friends i am using soap web services very first time in my code.
but i am not getting how to handle nested soap objects.
this is my code
private final String URL = "http://192.168.0.20/BookingEngineService/HotelBookingService.asmx";
private final String SOAP_ACTION = "http://tempuri.org/AndroidTestRequest";
private final String METHOD_NAME = "AndroidTestRequest";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("User", "abcd#xyz.com");
request.addProperty("Password", "abcd#123");
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.headerOut = new Element[1];
envelope.headerOut[0] = buildAuthHeader();
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
try {
androidHttpTransport.call(SOAP_ACTION, envelope);
Log.i("myAppEnvelope", envelope.toString());
SoapObject response = (SoapObject) envelope.getResponse();
// here i am trying to get values from response which is soap object
for (int i = 0; i < response.getPropertyCount(); i++) {
SoapObject con = (SoapObject) response.getProperty(i);
Log.e("Continent", con.toString());
}
} catch (Exception e) {
e.printStackTrace();
}
i am getting soap object as a response and which is very complex so i am not getting how to parse it. this is my response this is very small part of my response.
I am not getting how to get values from SoapObject -> Properties -> elementData -> value-> property -> elementData and so on ... its too complex please help me.

You can convert soap object to a string. Check the example code bellow
Eg:
final SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
final String str = response.toString();

It's a simple XML parsing. Go through the link for more details.
You can get the response as input stream and convert that into string and using that string, you can create an XML object. Please refer the link for more information.

Related

android soap web service - not retrieving output

private final String zipCodenameSpace ="http://www.webserviceX.NET/";
private final String zipURL="http://www.webserviceX.net/uszip.asmx";
private final String zipSoapAction ="http://www.webserviceX.NET/GetInfoByCity";
private final String zipMethodName="GetInfoByCity";
SoapObject request = new SoapObject(zipCodenameSpace, zipMethodName);
PropertyInfo cityInfo = new PropertyInfo();
cityInfo.setName("USCity");
cityInfo.setValue(city);
// cityInfo.setType(String.class);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
request.addProperty(cityInfo);
TextView tv = (TextView) findViewById(R.id.tv1);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(zipURL);
try {
androidHttpTransport.call(zipSoapAction, envelope);
// Object response =(SoapObject) envelope.getResponse();
SoapObject response = (SoapObject) envelope.getResponse();
Log.d("This is an element", response.toString());
// tv.setText(response.toString());
// return response.toString();
} catch (Exception e) {
e.printStackTrace();
}
Showing null exception. Error is occurring at soapObject response.
I tried to use soap primitive but not working. Please help me where the problem is
Given Webservice returns xml.
cityInfo.setValue(city);
Where are you fetching this "city" value in your code? I think thats the problem. For testing you can set it as string directly here.like "NewYork".

android send data to a .net webservice

I am trying to create a android app to send text and photos to .net webservice. I have functions in my webservice. one of them gets a dummy name(I created this to check if I can make a connection) and the other one is to insert some data into DB. I want to post my work to get help.
private final String NAMESPACE = "http://methodoor.com/";
//webservice is working, you can check it online
private final String URL = "http://servicing2.rotanet.com.tr/service.asmx";
private final String SOAP_ACTION = "http://methodoor.com/checkupservice/SendData";
private final String METHOD_NAME = "SendData";
//Create request
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("containerId",1);
.........
.........
request.addProperty("sFileID","asd");
request.addProperty("userId",1);
//Create envelope
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
//Set output SOAP object
envelope.setOutputSoapObject(request);
//Create HTTP call object
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
try {
//Invole web service
androidHttpTransport.call(SOAP_ACTION, envelope);
//Get the response
SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
//Assign it to fahren static variable
fahren = response.toString();
} catch (Exception e) {
}
My problem is, I am not sure if this is the correct way to pass data to webservice. it doesnt crash or gives any error message. It just doesnt insert into the DB
here you go..make sure to check spelling of each tag in your service, method name and path of your service..
public SoapObject soap(String METHOD_NAME, String SOAP_ACTION, String NAMESPACE, String URL,String IP,String SERVICEPATH) throws IOException, XmlPullParserException
{
abc.allowAllSSL();
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); //set up request
//request.addProperty("iTopN", "5"); //variable name, value. I got the variable name, from the wsdl file!
request.addProperty("UserId", login);
request.addProperty("Password", password);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); // put all required data into a soap
envelope.dotNet = true;
envelope.setOutputSoapObject(request); // prepare request
envelope.bodyOut = request;
Log.d("ENVELOPE",""+"Coming3");
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
//androidHttpTransport.
androidHttpTransport.call(SOAP_ACTION, envelope);
Log.d("ENVELOPE",""+envelope.bodyIn);
SoapObject result = (SoapObject) envelope.bodyIn; // get response
Log.d("ENVELOPE",""+envelope.bodyIn);
SoapObject responseBodyRaw,responseBody,tableRow;
return result;
}
Here is parameter details
private String NAMESPACE = "http://tempuri.org/";
private String SOAP_ACTION = "http://tempuri.org/UserProfile";
private String METHOD_NAME = "UserProfile";
private String URL="https://172.17.60.15/HostingService/PhoneForService.asmx";
//private String URL="https://172.19.2.250/testService/phone.asmx";
private String SERVICEPATH="/HostingService/PhoneForService.asmx";
I hope would be helpful for you

How to parse a SoapObject in android

I'm using webservices for an application to get the data from external servers. I'm able to connect to the server, send my SOAP request and able get the data back to my android class as SoapObject.
Here I'm having trouble parsing this SoapObject to retrieve the values (as strings) coming from the webservice.
My code is here:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list);
mInflater = (LayoutInflater)getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
System.out.println("Entered "+getClass().getSimpleName());
//Calling the web service to get the documents list
callService("getDocumentsList","http://wservice.friedmaggy.com/getDocumentsList");
-- I have created callService method to call the webservice.:
public void callService(String operation, String soapaction)
{
try {
String SOAP_ACTION = soapaction;
String OPERATION_NAME = operation;
String WSDL_TARGET_NAMESPACE = getString(R.string.targetNamespace);
String SOAP_ADDRESS = getString(R.string.soapAddress);
SoapObject request = new SoapObject(
WSDL_TARGET_NAMESPACE, OPERATION_NAME);
System.out.println("SOAP_ACTION "+SOAP_ACTION);
System.out.println("OPERATION_NAME "+OPERATION_NAME);
System.out.println("WSDL_TARGET_NAMESPACE "+WSDL_TARGET_NAMESPACE);
System.out.println("SOAP_ADDRESS "+SOAP_ADDRESS);
// System.out.println("SOAP_ACTION "+SOAP_ACTION);
PropertyInfo propInfo = new PropertyInfo();
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
HttpTransportSE httpTransport = new HttpTransportSE(
SOAP_ADDRESS);
envelope.setOutputSoapObject(request);
httpTransport.call(SOAP_ACTION, envelope);
SoapObject response = (SoapObject) envelope.bodyIn;
if(response.getPropertyCount()>0){
data=new StringBuilder();
for (int i = 0; i < response.getPropertyCount(); i++) {
Course c = new Course((SoapObject) response.getProperty(i));
courseList.add(c);
}
for (Course c : courseList) {
data.append("CourseName :" + c.getName());
results.add(c.getName());
}
-- As seen above, i'm passing the whole response object to another bean class to fetch the values.(Course c = new Course((SoapObject) response.getProperty(i));)
Can you help me how to parse this response object to fetch the required String values out.?
androidHttpTransport.call(SOAP_ACTION, envelope);
Object response = envelope.getResponse();
Vector<SoapObject> res= null;
if (response instanceof SoapObject) {
res = new Vector();
res.add((SoapObject) response);
} else if (response instanceof Vector) {
res = (Vector<SoapObject>) response;
}
for(SoapObject so1: res){
//retrieve String here from soap object using this
so1.getProperty(0).toString();
//then so1.getProperty(1).toString and likewise
}

Android: soap primitive error

I am having some issues with accessing a web service. Getting ClassCastException Error. Consider a scenario that I am trying to access a method of a web-service and the webservice is supposed to return two strings (Lets say String1 and String2). Moreover, I have to provide or pass two parameters (lets say Parameter 1 and Parameter 2 where Parameter 1 should be integer and Parameter 2 should be String) Here is my code
public class MyWebService extends Activity {
private static final String SOAP_ACTION ="http://www.mywebsite.com/myMethod";
private static final String METHOD_NAME = "MyMethod";
private static final String NAMESPACE = "http://www.myNamespace/";
private static final String URL = "http://mysession.com/myservice.asmx?WSDL";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try {
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
PropertyInfo pi = new PropertyInfo();
pi.setName("Parameter 1");
pi.setValue(1);
pi.setType(pi.INTEGER_CLASS);
request.addProperty(pi);
PropertyInfo pi2 = new PropertyInfo();
pi2.setName("Parameter 2");
pi2.setValue("Any string");
pi2.setType(pi2.STRING_CLASS);
request.addProperty(pi2);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet=true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.call(SOAP_ACTION, envelope);
SoapObject result=(SoapObject)envelope.getResponse();
String string1=result.getProperty(0).toString();
String string2=result.getProperty(1).toString();
} catch (Exception e) {
e.printStackTrace();
}
}
}
This is the exception I am getting
java.lang.ClassCastException: org.ksoap2.serialization.SoapPrimitive
Can anyone tell me if I am doing something wrong here..
Thanks
Try this,
SoapPrimitive result= (SoapPrimitive)envelope.getResponse();
OR
Object result= (Object)envelope.getResponse();
instead of
SoapObject result=(SoapObject)envelope.getResponse();
Try this:
SoapObject request = new SoapObject(SOAP_NAMESPACE,SOAP_METHOD);
request.addProperty("name", abcd);
request.addProperty("age", 30);

How to add header,username,password sharepoint webservice using ksoap in android

I am writing an Android application that will use the getlistitems() method of the lists.amx service in sharepoint 2010. I am using ksoap2-android to handle my soap messages. When I try to authenticate I get an xmlpullparser exception expected START_TAG... Why will the following code not authenticate to the sharepoint server?
private static final String SOAP_ACTION = "http://schemas.microsoft.com/sharepoint/soap/GetListItems";
private static final String METHOD_NAME = "GetListItems";
private static final String NAMESPACE = "http://schemas.microsoft.com/sharepoint/soap/";
private static final String URL = "http://www.domain.com/tr-TR/_vti_bin/Lists.asmx";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tv=(TextView)findViewById(R.id.tve);
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("listName", "Haberler");
request.addProperty("viewName", null);
request.addProperty("query", null);
request.addProperty("viewFields", null);
request.addProperty("rowLimit", "30");
request.addProperty("queryOptions", null);
request.addProperty("webID",null);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
// envelope.headerOut=new Element[1];
// envelope.headerOut[0]=buildAuthHeader();
String authentication = android.util.Base64.encodeToString("myusername:mypassword".getBytes(), android.util.Base64.NO_WRAP);
List<HeaderProperty> headers = new ArrayList<HeaderProperty>();
headers.add(new HeaderProperty("X-FORMS_BASED_AUTH_ACCEPTED","f" +authentication));
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.debug = true;
try {
androidHttpTransport.call(SOAP_ACTION, envelope,headers);
SoapObject response = (SoapObject) envelope.getResponse();
tv.setText(response.toString());
Log.e("SONUC",response.toString());
} catch (Exception e1) {
e1.printStackTrace();
}
}
Don't send null values. Instead, add all the values and then try sending it.
Try to use NtlmTransport instead of HttpTransportSE. MayBe it works for you.

Categories

Resources