getting error while calling soap request in android? - android

I am using Ksoap2 android assembly 2.4 in my android application.I am try to call the web service by using soap.But I am getting the error at transport.call(soapAction, envelop); this line.
LOGCaT:
org.xmlpull.v1.XmlPullParserException: expected: END_TAG {http://schemas.xmlsoap.org/soap/envelope/}Body (position:END_TAG </{http://schemas.xmlsoap.org/soap/envelope/}soap:Fault>#1:301 in java.io.InputStreamReader#41743310)
And Here my code is:
String url = "http://192.168.56.1:8080/CxfWebservice/webservices/Calculator";
// String namespace = "http://localhost:8080/wsdl";
String namespace = "http://192.160.59.1:8080/wsdl";
String methodname = "sum";
public static void SoapOperation(String url, String method_name,
String name_space) throws Exception {
String soapAction = name_space + method_name;
SoapObject request = new SoapObject(name_space, method_name);
PropertyInfo p = new PropertyInfo();
p.setName("arg0");
p.setValue(5);
p.setType(Integer.TYPE);
PropertyInfo p1 = new PropertyInfo();
p1.setName("arg1");
p1.setValue(15);
p1.setType(Integer.TYPE);
request.addProperty(p );
request.addProperty(p1);
SoapSerializationEnvelope envelop = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelop.setOutputSoapObject(request);
envelop.dotNet = true;
HttpTransportSE transport = new HttpTransportSE(url);
transport.debug = true;
transport.setXmlVersionTag("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
transport.call(soapAction, envelop);
String xml = transport.responseDump;
System.out.println("the response xml is:"+xml);
}
above is my code with please give me a solution for this.

Here is an example. Use this code
public void InteractWithWebService() {
try {
final String URL = con.getResources().getString(R.string.URL);
final String NameSpace = con.getResources().getString(
R.string.NAMESPACE);
final String MethodName = "sum";
final String SOAP_ACTION = con.getResources().getString(
R.string.SOAP_ACTION)
+ MethodName;
SoapObject Request = new SoapObject(NameSpace, MethodName);
Request.addProperty("param_name", Object_name.getText().toString()
.trim());
Request.addProperty("param_name", Object_name.getText().toString()
.trim());
SoapSerializationEnvelope soapEnvelop;
soapEnvelop = new SoapSerializationEnvelope(SoapEnvelope.VER11);
soapEnvelop.dotNet = true;
soapEnvelop.setOutputSoapObject(Request);
HttpTransportSE htp = new HttpTransportSE(URL);
htp.call(SOAP_ACTION, soapEnvelop);
// SoapObject response;
SoapPrimitive resultString = (SoapPrimitive) soapEnvelop
.getResponse();
if (resultString != null) {
status = Integer.parseInt(resultString.toString());
}
} catch (Exception ex) {
status = -1;
}
}
Add these lines in your strings file
<string name="NAMESPACE">http://tempuri.org/</string>
<string name="URL">http://Your_Localhost_address/Name_of_Service.svc</string>
<string name="SOAP_ACTION">http://tempuri.org/IName_of_Service/</string>

Related

org.xmlpull.v1.xmlpullparserexception expected start_tag error when using ksoap2

I use ksoap2 to connect .NET by web service.
This is my Dataset
public DataSet getphimall()
{
DataSet ds1 = new DataSet();
try
{
SqlConnection cnn = new SqlConnection("Data Source=.\\SQLEXPRESS;Initial Catalog=EMHAUI;Integrated Security=True");
SqlCommand cmd = new SqlCommand("sp_GetAllSemester_ad", cnn);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds1);
return ds1;
}
catch (Exception e)
{
return null;
}
}
And this is my webservice
[WebMethod]
public DataSet getSM()
{
Class1 phim1 = new Class1();
return phim1.getphimall();
}
And this is my javaconnector class
public class getSM {
String tenphim;
String daodien;
private static final String SOAP_ACTION = "http://tempuri.org/getSM";
private static final String METHOD_NAME = "getSM";
private static final String NAMESPACE = "http://tempuri.org/";
private static final String URL = "http://10.0.2.2:50532/wsAndroid.asmx";
public getSM getallphim()
{
SoapObject table = null;
SoapObject client = null;
SoapObject tableRow = null;
SoapObject responseBody = null;
AndroidHttpTransport transport = null;
SoapSerializationEnvelope sse = null;
//cái này trong tut viết thế, mình lười đổi tên
sse = new SoapSerializationEnvelope(SoapEnvelope.VER11);
sse.addMapping(NAMESPACE, "getSM", this.getClass());
sse.dotNet = true;
AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL);
getSM setphim = new getSM();
try
{
client = new SoapObject(NAMESPACE, METHOD_NAME);
sse.setOutputSoapObject(client);
sse.bodyOut = client;
androidHttpTransport.call(SOAP_ACTION, sse);
responseBody = (SoapObject) sse.getResponse();
responseBody = (SoapObject) responseBody.getProperty(1);
table = (SoapObject) responseBody.getProperty(0);
tableRow = (SoapObject) table.getProperty(0);
setphim.daodien = tableRow.getProperty("ID").toString();
setphim.tenphim = tableRow.getProperty("SemesterName").toString();
return setphim;
} catch (Exception e)
{
setphim.daodien = e.toString();
setphim.tenphim = e.toString();
return setphim;
}
}}
But When I run my emulator, I have an error
org.xmlpull.v1.xmlpullparserexception expected start_tag error
Please help me! Thanks
There might be a few reasons for the exception that you are getting.
Wrong Parameters for the SOAP call : try to confirm if the values of NAMESPACE, ACTION, METHOD and URL are correct or not, by looking at the WSDL file
Invalid response from server : try to log the response that the server sends to you and check if you are getting correct well-structured XML or not
androidHttpTransport.debug = true;
//execute request
androidHttpTransport.responseDump; //response string from server
dotNet attribute of the envelope : try using soapEnvelope.dotNet=true

SoapObject in android application

Here is some fragment of my android application code PerformDownload.java
public class PerformDownload {
private final String NAMESPACE = "http://tempuri.org/";
private final String URL = "http://10.0.2.2:4304/Service1.asmx";
public String GetContacts(String username) throws IOException, XmlPullParserException
{
// String result = null;
final String SOAP_ACTION = "http://tempuri.org/GetContacts";
final String METHOD_NAME = "GetContacts";
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("username",username);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true; // put this only if the web service is .NET one
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.call(SOAP_ACTION, envelope);
KvmSerializable response= (KvmSerializable)envelope.bodyIn;
I am getting error on SoapObject,SoapSerializationEnvelope(SoapEnvelope.VER11); and so on.
Why the above code is not accessing soap protocol.

Webservices in android

I am new to android. In my application I tried to call SOAP web services, in that I can't understand what is meant
for(SOAP_Action,OperationName,WSDL_TARGET_NAMESPACE,SOAP_ADDRESS). The following is my full code
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
TextView textView = new TextView(this);
setContentView(textView);
SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE,
OPERATION_NAME);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.dotNet = true;
System.out.println("subbu="+request);
envelope.setOutputSoapObject(request);
HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS);
try
{
httpTransport.call(SOAP_ACTION, envelope);
Object response = envelope.getResponse();
textView.setText(response.toString());
}
catch (Exception exception)
{
textView.setText(exception.toString());
}
}
}
Can anybody explain what thats purpose for. Give some link from which I can get idea.
SOAP_ACTION / NAMESPACE and METHODS can be found in the WSDL file of the target webservice !
Here is a sample code to send a SOAP request to a webservice :
public class SoapRequest {
private static final String SOAP_ACTION = "xxx";
private static final String METHOD_NAME = "xxx";
private static final String NAMESPACE = "xxx";
private static final String URL = "url of the webservice";
public static SoapObject soap() throws IOException, XmlPullParserException {
SoapObject request = new SoapObject (NAMESPACE, METHOD_NAME);
/* Here you can add properties to your requests */
PropertyInfo pi1 = new PropertyInfo();
pi1.name = "xxx";
pi1.type = String.class;
request.addProperty(pi1, "xxx");
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.debug = true;
androidHttpTransport.call(SOAP_ACTION, envelope);
SoapObject soapResult = (SoapObject) envelope.bodyIn;
return soapResult;
}

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.

Android : how to pass a integer value to service using KSOAP in android

Not able to pass an integer value to the service request.The value that reaches service request becomes null
Here is my code
private static final String METHOD_NAME ="GetPrivileges";
private static final String NAMESPACE = "http://AuthorizationManagement.ServiceContracts/2007/01/";
private static final String URL ="http://192.168.5.219/NTLS_Authorization
/AuthorisationManager.asmx";
final String SOAP_ACTION ="http://AuthorizationManagement.ServiceContracts/2007/01/GetPrivileges";
public void call() {
try {
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
PropertyInfo pi=new PropertyInfo();
pi.type=PropertyInfo.INTEGER_CLASS;
pi.setName("RoleID");
pi.setValue(3);
pi.setNamespace(NAMESPACE);
request.addProperty(pi);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.call(SOAP_ACTION, envelope);
Object result = envelope.getResponse();
String resultData = result.toString();
public void call(int number)
{
try
{
String METHOD_NAME ="GetPrivileges";
String NAMESPACE = "http://AuthorizationManagement.ServiceContracts/2007/01/";
String URL ="http://192.168.5.219/NTLS_Authorization/AuthorisationManager.asmx";
final String SOAP_ACTION ="http://AuthorizationManagement.ServiceContracts/2007/01/GetPrivileges";
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("RoleID",number);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL);
androidHttpTransport.call(SOAP_ACTION, envelope);
Object result = envelope.getResponse();
String resultData = result.toString();
Log.v("Result==>",resultData);
}
catch(Exception e)
{
e.printStackTrace();
}
}
simply you can add this
requestObject.addProperty("RoleID",3);
May be try this code //note don't put directly number(3 in ur case) instead pass it as parameter ...
final SoapObject requestObject = new SoapObject(Constants.NAMESPACE,METHOD_NAME );
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = false;
requestObject.addProperty("RoleID",number);
envelope.setOutputSoapObject(requestObject);
AndroidHttpTransport androidHttpTransport =new AndroidHttpTransport(Constants.URL);
try
{
androidHttpTransport.call(Constants.SOAP_ACTION+METHOD_NAME, envelope);
SoapObject response = (SoapObject)envelope.getResponse();
Log.v("result",response.toString());
}
catch(Exception e)
{
e.printStackTrace();
}

Categories

Resources