I have added the library "ksoap2-android-assembly-2.4-jar-with-dependencies.jar "
but i am getting below error :
Exception in thread "main" java.lang.NullPointerException at org.kxml2.io.KXmlParser.require(KXmlParser.java:1353) at org.ksoap2.SoapEnvelope.parse(SoapEnvelope.java:127) at org.ksoap2.transport.Transport.parseResponse(Transport.java:63) at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:100)
private void getData()
{
String METHOD_NAME = "schedule.setschedule";
String SOAP_ACTION = "urn:schedule#setschedule";
String NAMESPACE = "urn:schedule";
String URL = "http://96.30.19.40:8080/server.php?wsdl";
try {
SoapObject request=soap(METHOD_NAME,SOAP_ACTION,NAMESPACE,URL);
System.out.println("suceess");
System.out.println(request.toString());
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("fail1");
e.printStackTrace();
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("fail2");
}
}
public static SoapObject soap(String METHOD_NAME, String SOAP_ACTION, String NAMESPACE, String URL) throws IOException, XmlPullParserException {
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!
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); //put all required data into a soap envelope
envelope.setOutputSoapObject(request); //prepare request
HttpTransport httpTransport = new HttpTransport(URL);
httpTransport.debug = true; //this is optional, use it if you don't want to use a packet sniffer to check what the sent message was (httpTransport.requestDump)
httpTransport.call(SOAP_ACTION, envelope); //send request
SoapObject result=(SoapObject)envelope.getResponse(); //get response
return result;
}
The correct download url for the project is here
http://code.google.com/p/ksoap2-android/wiki/HowToUse?tm=2
The direct url provided above is an old version. Read the instructions and download the complete bundle.
You may not have the correct ksoap2 package
Related
I'm trying to connect with a web service.
The sample request for this web service contains this array:
<Details>
<invoiceDetails>
<ItemID>int</ItemID>
<Price>decimal</Price>
<Quantity>decimal</Quantity>
<UOM>int</UOM>
</invoiceDetails>
<invoiceDetails>
<ItemID>int</ItemID>
<Price>decimal</Price>
<Quantity>decimal</Quantity>
<UOM>int</UOM>
</invoiceDetails>
</Details>
I have managed to make a call with SOAP before, but not with arrays,
so how would I add this to my request?
Try this code,
public String getservicecallmethod(String nameSpace, String methodName,
String soapAction, String Url, List<Info> mInfo) {
String mResponse = "";
SoapObject request = new SoapObject(nameSpace, methodName);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
if (mPropertyInfo != null) {
for (Info propertyInfo : mInfo) {
request.addProperty(propertyInfo);
}
}
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE ht = new HttpTransportSE(Url);
ht.debug = true;
try {
ht.call(soapAction, envelope);
} catch (IOException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
try {
mResponse = envelope.getResponse().toString();
} catch (Exception e) {
e.printStackTrace();
}
return mResponse;
}
I have developed an Android Application which consumes a Webservice. I have tested the application on different devices and its working perfectly but on the tablet of my client, it's throwing an exception. All devices on which application is tested are running Android 6.0.1 Marshmallow.Following is the exception
java.net.ConnectException: failed to connect to web.abc.com(port 80):connect failed: ECONNREFUSED (Connection refused)
Following is my code for consuming Webservice
String namespace = "http://web.abc.com/";
private static final String url = "http://web.abc.com/WebService/ServiceFileName.asmx";
String SOAP_ACTION;
SoapObject request = null, objMessages = null;
SoapSerializationEnvelope envelope;
HttpTransportSE androidHttpTransport;
/**
* Set Envelope
*/
protected void SetEnvelope() {
try {
// Creating SOAP envelope
envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
//You can comment that line if your web service is not .NET one.
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
//envelope.addMapping(namespace, "ProductStausResponse",new ProductStausResponse().getClass());
androidHttpTransport = new HttpTransportSE(url);
androidHttpTransport.debug = true;
} catch (Exception e) {
e.printStackTrace();
Log.v("Soap Exception", e.toString());
System.out.println("Soap Exception---->>>" + e.toString());
}
}
// MethodName variable is define for which webservice function will call
public String getProductStatus(String MethodName, String Parameter)
{
try {
ProductCode = ProductCode.trim();
SOAP_ACTION = namespace + MethodName;
//Adding values to request object
request = new SoapObject(namespace, MethodName);
/// Code for setting properties
SetEnvelope();
try {
//SOAP calling webservice
androidHttpTransport.call(SOAP_ACTION, envelope);
//Got Webservice response
String result = envelope.getResponse().toString();
return result;
} catch (Exception e) {
// TODO: handle exception
//e.printStackTrace();
//return "Exception";
return e.toString();
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
Log.v("Soap Exception", e.toString());
//return "Exception";
return e.toString();
}
}
I get the string of array on responseJSON like this
Result: responseJSON = ["Product1","Product2","Product1","Product2"]
try {
// Invole web service
androidHttpTransport.call(SOAP_ACTION+methName, envelope);
// Get the response
SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
// Assign it to static variable
responseJSON = response.toString();
} catch (Exception e) {
e.printStackTrace();
}
how can to get each element of the array so that I can use it to display on my ListView?
Thanks
public void invokeJSONWS(String country, String methName) {
// Create request
SoapObject request = new SoapObject(NAMESPACE, methName);
/*
// Property which holds input parameters
PropertyInfo paramPI = new PropertyInfo();
// Set Name
paramPI.setName("country");
// Set Value
paramPI.setValue(country);
// Set dataType
paramPI.setType(String.class);
// Add the property to request object
request.addProperty(paramPI);
*/
// 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+methName, envelope);
// Get the response
SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
// Assign it to static variable
responseJSON = response.toString();
JSONArray responseArr = new JSONArray(responseJSON);
for(int i=0;i < responseArr.length();i++)
{
String temp=responseArr.getString(i);
myProduct.add(new Product(responseJSON,2,R.drawable.user_awake,responseJSON));
}
} catch (Exception e) {
e.printStackTrace();
}
}
this is the method that i used it it correct that i also include the adding an item in the list view??
please help
thanks
pass this string to JSONAarry constructor like this
ArrayList<String> mParsedList = new ArrayList<String>();
JSONAarry responseArr=new JSONArray(responseJSON);
for(int i=0;i<responseArr.length;i++)
{
String temp=responseArr.getString(i);// get one by one element
myProduct.add(new Product(temp,2,R.drawable.user_awake,responseJSON));
}
I have a webservice with this location:
<wsdlsoap:address location="http://app.example.com:8080/WSTest/services/Hello"/>
And I want to get access to this webservice over my Android app. I do this:
private class ConnectTask extends AsyncTask<String, Void, String> {
#Override
protected String doInBackground(String... params) {
private final String NAMESPACE = "http://ws.audiomobil.com";
private final String URL = "http://app.example.com:8080/WSTest/services/Hello?wsdl";
private final String SOAP_ACTION = "http://ws.audiomobil.com/hello";
private final String METHOD_NAME = "hello";
try {
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
String firstName = "Android";
String lastName = "Program";
/*
// Pass value for fname variable of the web service
PropertyInfo fnameProp = new PropertyInfo();
fnameProp.setName("fname"); // Define the variable name in the web service method
fnameProp.setValue(firstName); // Define value for fname variable
fnameProp.setType(String.class); // Define the type of thevariable
request.addProperty(fnameProp); // Pass properties to thevariable
// Pass value for lname variable of the web service
PropertyInfo lnameProp = new PropertyInfo();
lnameProp.setName("lname");
lnameProp.setValue(lastName);
lnameProp.setType(String.class);
request.addProperty(lnameProp);
*/
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL,60000);
try {
androidHttpTransport.call(SOAP_ACTION, envelope);
SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
String s = response.toString();
System.out.println("WS: " + s);
} catch (Exception e) {
System.out.println(e);
e.printStackTrace();
}
} catch (Exception e) {
System.out.println(e);
e.printStackTrace();
}
return null;
}
}
++++++EDIT
My Webservice class is this:
public class Hello {
public String hello(String fname, String lname){
return "Hello";
}
}
+++++++
When I run this on my AVD Emulator Android 4.0.3 then its working fine. Its get access to webservice and I get the response.
But when I run it on my device Version 4.0.3, then it's not working. It stops at this point: androidHttpTransport.call(SOAP_ACTION, envelope);
And it throws a XMLPullParserException. Heres the logcat:
01-23 11:24:36.959: I/System.out(8172): org.xmlpull.v1.XmlPullParserException:
unterminated entity ref (position:TEXT #1:86 in java.io.InputStreamReader#4188c258)
I have really no idea how this can happen. Can anyone help me pls?
I think you are missing the xml version tag. Try to set xml version tag before calling
androidHttpTransport.setXmlVersionTag("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
androidHttpTransport.call(SOAP_ACTION, envelope);
Sample Code:
public static String webServiceForScriptTimeLimit() {
String scriptTimeLimitResponse = null;
try {
SoapObject request = new SoapObject(NAMESPACE,
SCRIPT_TIMELIMIT_METHOD_NAME);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.implicitTypes = true;
envelope.enc = SoapSerializationEnvelope.ENC2003;
envelope.xsd = SoapEnvelope.XSD;
envelope.xsi = SoapEnvelope.XSI;
envelope.setOutputSoapObject(request);
envelope.setAddAdornments(false);
HttpTransportSE ht = new HttpTransportSE(SOAP_ADDRESS);
ht.debug = true;
ht.call(SOAP_ACTION_TIME_LIMIT, envelope);
final SoapPrimitive response = (SoapPrimitive) envelope
.getResponse();
scriptTimeLimitResponse = response.toString();
}
catch (Exception ex) {
FileLog.logInfo(
"Exception ---> WS_ERROR ---> WebServiceUtility: webServiceForScriptTimeLimit() - "
+ ex.toString(), 0);
}
return scriptTimeLimitResponse;
}
This is my primitive web-service:
#Override
public boolean LogOn(String UserName, String Password) {
String username = "";
String password = "";
if (UserName != null) {
username = UserName;
}
if (Password != null) {
password = Password;
}
System.out.println("username is"+username);
System.out.println("password is"+password);
//test parameters
if (username.equals("admin") && password.equals("admin")) {
return true;
} else {
return false;
}
}
This client on Android:
private static final String METHOD_NAME = "LogOn";
private static final String NAMESPACE = "http://calculator.me.org/";
private static final String URL = "http://10.0.2.2:8080/logon/WebServiceImplService?wsdl";
#Override
protected Integer doInBackground(String... params) {
// TODO Auto-generated method stub
try
{
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
//Add the input variables for the method
request.addProperty("username", "admin");
request.addProperty("password","admin");
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
System.setProperty("http.keepAlive", "false");
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.debug=true;
androidHttpTransport.call(getSoapAction(METHOD_NAME), envelope);
SoapObject result = (SoapObject) envelope.bodyIn;
//get response from web server
String d =result.getProperty("return").toString();
// Logging the raw request and response (for debugging purposes)
Log.d(TAG, "HTTP REQUEST:\n" + androidHttpTransport.requestDump);
Log.d(TAG, "HTTP RESPONSE:\n" + androidHttpTransport.responseDump);
Log.i(TAG,"response from service:"+d);
if(d.equals("true"))
{
//The result will be here, use it wisely :)
return 1;
}
else
return 0;
} catch (Exception e) {
Log.e(TAG,"doInBackground failed"+e);
e.printStackTrace();
return 0;
}
}
When i'm manual testing web-service, log web-service:
INFO: username isadmin
INFO: password isadmin
When i'm trying login from android client, i'm get next log:
INFO: username is
INFO: password is
If i'm delete nullpointer check, on web-service i'm get NullPointerException.
Logs from Android client:
11-12 02:29:55.081: D/SmsSender:LogOnAsync(1878): HTTP REQUEST:
11-12 02:29:55.081: D/SmsSender:LogOnAsync(1878): <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><n0:LogOn id="o0" c:root="1" xmlns:n0="http://calculator.me.org/"><username i:type="d:string">admin</username><password i:type="d:string">admin</password></n0:LogOn></v:Body></v:Envelope>
11-12 02:29:55.081: D/SmsSender:LogOnAsync(1878): HTTP RESPONSE:
11-12 02:29:55.081: D/SmsSender:LogOnAsync(1878): <?xml version='1.0' encoding='UTF-8'?><S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"><S:Body><ns2:LogOnResponse xmlns:ns2="http://calculator.me.org/"><return>false</return></ns2:LogOnResponse></S:Body></S:Envelope>
11-12 02:29:55.081: I/SmsSender:LogOnAsync(1878): response from service:false
What's wrong?
I have a similar problem. I'm decided to read:
Override
protected Integer doInBackground(String... params) {
// TODO Auto-generated method stub
try
{
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
PropertyInfo pi1 = new PropertyInfo();
pi1.setName("arg0");
pi1.setValue("admin");
pi1.setType(String.class);
request.addProperty(pi1);
PropertyInfo pi2 = new PropertyInfo();
pi2.setName("arg1");
pi2.setValue("admin");
pi2.setType(String.class);
request.addProperty(pi2);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
System.setProperty("http.keepAlive", "false");
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.debug=true;
androidHttpTransport.call(getSoapAction(METHOD_NAME), envelope);
SoapObject result = (SoapObject) envelope.bodyIn;
//get response from web server
String d =result.getProperty("return").toString();
// Logging the raw request and response (for debugging purposes)
Log.d(TAG, "HTTP REQUEST:\n" + androidHttpTransport.requestDump);
Log.d(TAG, "HTTP RESPONSE:\n" + androidHttpTransport.responseDump);
Log.i(TAG,"response from service:"+d);
if(d.equals("true"))
{
//The result will be here, use it wisely :)
return 1;
}
else
return 0;
} catch (Exception e) {
Log.e(TAG,"doInBackground failed"+e);
e.printStackTrace();
return 0;
}
}
This will help only if you brought the whole method LogOn