I have implemented my SOAP webservice following the tutorial found on google developers website, and now i'm writing a android app that call an available service and show result (for now in a textview) using ksoap2 libraries.
That's the code:
public class DownloadDataTask extends AsyncTask<Void, Void, SoapObject> {
private static String METHOD_NAME = "getData";
private static String SOAP_ACTION = "http://example.com/getData";
private static String WSDL_URL = "http://arduino-data-server.appspot.com/FunctionsService.wsdl";
private static String NAMESPACE = "http://example.com/";
private MainActivity caller_activity;
public DownloadDataTask(MainActivity a) {
caller_activity = a;
}
#Override
protected SoapObject doInBackground(Void... arg0) {
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER12);
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(WSDL_URL);
try {
androidHttpTransport.call(SOAP_ACTION, envelope);
SoapObject result = (SoapObject) envelope.getResponse();
return result;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(SoapObject result) {
TextView tw = (TextView) caller_activity.findViewById(R.id.text_view);
if (result == null) {
tw.setText("NULL");
} else {
tw.setText(result.getName());
}
}
}
but everytime, the result SoapObject it's null. what's wrong? on appengine server log, i can see that android app ask for wsdl file, but no request for service was sent. What's wrong (wsdl file is available ad url write inside my code)?
Ksoap doesn't use wsdl (and so doesn't request it). You should pass service url instead of wsdl url. Service url you can find in wsdl (attribute location of the address element in the service description section).
Related
Im trying to get some response from several SOAP webservices at last i tried to run a code that is a well very known example on the internet. But i realized that even this doesnt run on my project. I hardly tried to understand what the error could be but i dont know why its not working with soap.
I would really appriacate your help.
Downloaded new version of KSOAP2 and also permission for internet is given.
public class WEBSERVİCE extends AppCompatActivity {
Button btn;
EditText et;
TextView txv;
String celcius="21";
String fahren;
private String NAMESPACE = "https://www.w3schools.com/xml/";
private String METHOD_NAME = "CelsiusToFahrenheit";
private String SOAP_ACTİON = "https://www.w3schools.com/xml/CelsiusToFahrenheit";
private String URL = "https://www.w3schools.com/xml/tempconvert.asmx?op=CelsiusToFahrenheit?WSDL";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = findViewById(R.id.button);
txv = findViewById(R.id.textView);
et = findViewById(R.id.editText1);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AsyncCallWS task = new AsyncCallWS();
task.execute();
}
});
}
private class AsyncCallWS extends AsyncTask<String, Void, String> {
#Override
protected void onPreExecute() {
txv.setText("calculating");
super.onPreExecute();
}
#Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
#Override
protected String doInBackground(String... objects) {
return getBolum(celcius);
}
#Override
protected void onPostExecute(String o) {
txv.setText(fahren + "F");
super.onPostExecute(o);
}
}
public String getBolum(String celsius) {
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
PropertyInfo pi = new PropertyInfo();
pi.setName("Celcius");
pi.setValue(celsius);
pi.setType(double.class);
request.addProperty(pi);
SoapSerializationEnvelope envelope = new
SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHTTPTransport = new HttpTransportSE(URL);
try {
androidHTTPTransport.call(SOAP_ACTİON, envelope);
SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
fahren = response.toString();
} catch (IOException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
}
return fahren;
}
}
No Error Messages but the value it turns back is always "null"
EDIT:posted changed code again
There isn't anything wrong with SOAP Api. The problem is your AsyncTask class. Read the documentation for AsyncTask first. Please do proper research before you use any code from internet. Always read about the components that are used snippets on internet otherwise you are going to have hard time figuring out problems.
Your AsyncTask class is declared as:
private class AsyncCallWS extends AsyncTask<String,Void,Void>
Change it to
private class AsyncCallWS extends AsyncTask<String,Void,String>
Third generic parameter in your Void which is supposed to be result type. So in your case your async task won't return any data once it is finished.
//CHANGE TYPE TO STRING
public String getBolum(String celsius) {
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
PropertyInfo pi=new PropertyInfo();
pi.setName("Celcius");
pi.setValue(celsius);
pi.setType(double.class);
request.addProperty(pi);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHTTPTransport = new HttpTransportSE(URL);
try {
androidHTTPTransport.call(SOAP_ACTİON, envelope);
SoapPrimitive response= (SoapPrimitive) envelope.getResponse();
//RETURN RESULT
return response.toString();
} catch (IOException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
}
//I forgot this line previously:
return "";
}}
In your async task, you need to change return type of doInBackground and paramter of onPostExecute to String:
#Override
protected String doInBackground(String... objects) {
return getBolum(celcius); //RETURN RESULT
}
#Override
protected void onPostExecute(String result) {
txv.setText(result+"F");
fahren = result;
super.onPostExecute(result);
}
It should work now.
I'm try to connect android application to a web service uploaded on somee.
I have the following problem :
FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo: java.lang.NullPointerException
the web service code :
[WebService(Namespace = "http://www.n-m.somee.com/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class WebService1 : System.Web.Services.WebService
{
SqlConnection con = new SqlConnection(#"workstation id=WSDatabase.mssql.somee.com;packet size=4096;user id=***;pwd=***;data source=WSDatabase.mssql.somee.com;persist security info=False;initial catalog=WSDatabase ");
[WebMethod]
public string get_name(String pssw)
{
con.Open();
SqlCommand cmd = new SqlCommand("select username from users where pssw="+pssw, con);
string rd =(string) cmd.ExecuteScalar();
con.Close();
return rd;
}
}
I added the following line to manifest file:
** MainActivity.java file**
public class MainActivity extends AppCompatActivity {
protected String namespace="http://www.n-m.somee.com/";
protected String url="http://www.n-m.somee.com/WebService1.asmx";
protected String SOAPAction="http://www.n-m.somee.com/get_name";
protected String method_name= "get_name";
TextView user_name;
#SuppressLint("NewApi")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
public void goButton(View v)
{
user_name=(TextView)findViewById(R.id.textView1);
//allow internet
if(Build.VERSION.SDK_INT>9)
{
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
//create soap object
SoapObject soapObject=new SoapObject(namespace,method_name);
soapObject.addProperty("pssw", "1234");
//create envelop
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(soapObject);
//Rrquest get data from webservice and set it into soapobject
HttpTransportSE transport = new HttpTransportSE(url);
try {
transport.call(SOAPAction, envelope);
// get data
SoapPrimitive output=(SoapPrimitive) envelope.getResponse();
//set data into control
user_name.setText("pssw: "+output.toString());
} catch (IOException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
}
}
}
I appreciate any help
thank you
Your urls were a bit wrong. This is what I used,
protected String namespace = "http://www.nourhan-m.somee.com/";
protected String url = "http://www.nourhan-m.somee.com/WebService1.asmx";
protected String SOAPAction = "http://www.nourhan-m.somee.com/get_name";
protected String method_name = "get_name";
I made some modification to your code to make it work.
public String callService(String username) {
//create soap object and add params to it
SoapObject request = new SoapObject(namespace, method_name);
request.addProperty("pssw", username);
//create envelop
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
//set request to envelope
envelope.bodyOut = request;
HttpTransportSE transport = new HttpTransportSE(url);
transport.debug = true;
try {
transport.call(SOAPAction, envelope);
Log.e("OUT", transport.requestDump);
return transport.responseDump;
} catch (IOException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (Exception e){
e.printStackTrace();
}
return "";
}
This is how I invoked the method,
You can invoke it as follows,
final String username = (TextView) findViewById(R.id.textView1)
new Thread(new Runnable() {
#Override
public void run() {
Log.e("OUT", callService(username));
}
}).start();
I got an empty response, maybe it's because I didn't give a valid username. Have a look at it. Cheers :)
I am trying to call a soap which looks like this in SOAPUI. It's having 4 parameter. url is - http://seycel.com.mx/ws/res2.php
Inputs are like this-
`<usuario xsi:type="xsd:string">1212121212</usuario>
<sms xsi:type="xsd:string">saldo</sms>
<palabra xsi:type="xsd:string">0439267236</palabra>
<fecha xsi:type="xsd:string">2015-05-20 20:10:10</fecha>`
I want to call this from android and fetch the return tag. What I am trying to do is like this -
private static final String SOAP_ACTION = "urn:recargas#saldo";
private static final String METHOD_NAME = "saldo";
private static final String NAMESPACE = "urn:recargas";
private static final String URL = "http://seycel.com.mx/ws/res2.php?wsdl";
private class UserRegistrationTask extends AsyncTask<String, String, String> {
protected String doInBackground(String... values) {
SoapPrimitive result = null;
try {
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("palabra", "0439267236");// Parameter for Method
request.addProperty("usuario", "1212121212");// Parameter for Method
request.addProperty("sms", "saldo");// Parameter for Method
request.addProperty("fecha", "15-05-30 20:52:20");// Parameter for Method
SoapSerializationEnvelope envelope =
new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL);
androidHttpTransport.call(SOAP_ACTION, envelope);
result = (SoapPrimitive) envelope.getResponse();
} catch (IOException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
}
return result.toString();
}
protected void onPostExecute(String result) {
Log.d("TAG", "value: " + result);
}
}
getting an error like this java.lang.String cannot be cast to org.ksoap2.serialization.SoapPrimitive
Error I am getting is this
org.ksoap2.SoapFault cannot be cast to org.ksoap2.serialization.SoapObject
public final static String URL = "http://23.253.164.20:8096/login.asmx";
public static final String NAMESPACE = "http://23.253.164.20:8096";
public static final String SOAP_ACTION_PREFIX = "http://23.253.164.20:8096/getName";
private static final String METHOD = "getName";
private class AsyncTaskRunner extends AsyncTask<String, String, String> {
private String resp;
#Override
protected String doInBackground(String... params) {
publishProgress("Loading contents..."); // Calls onProgressUpdate()
try {
// SoapEnvelop.VER11 is SOAP Version 1.1 constant
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
SoapObject request = new SoapObject(NAMESPACE, METHOD);
//bodyOut is the body object to be sent out with this envelope
envelope.bodyOut = request;
HttpTransportSE transport = new HttpTransportSE(URL);
try {
transport.call(NAMESPACE + SOAP_ACTION_PREFIX + METHOD, envelope);
} catch (IOException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
}
//bodyIn is the body object received with this envelope
if (envelope.bodyIn != null) {
//getProperty() Returns a specific property at a certain index.
SoapPrimitive resultSOAP = (SoapPrimitive) ((SoapObject) envelope.bodyIn)
.getProperty(0);
resp=resultSOAP.toString();
}
} catch (Exception e) {
e.printStackTrace();
resp = e.getMessage();
}
return resp;
}
/**
*
* #see android.os.AsyncTask#onPostExecute(java.lang.Object)
*/
#Override
protected void onPostExecute(String result) {
// execution of result of Long time consuming operation
// In this example it is the return value from the web service
textView.setText(result);
}
Possibility checked
1) Internet permission given
2) Ksoap2 library imported
3) server side method is running (you can the URL)
4) Not running on the emulator instead on the mobile phone
You have some bugs in whole code.
there is no method "getName" - it looks like You meant "getGreetingForName" but i'm not sure becouse You have'nt provided param "stringName",
NAMESPACE = "http://23.253.164.20:8096/"; - add slash on the end,
transport.call(NAMESPACE + SOAP_ACTION_PREFIX + METHOD, envelope);
that is not proper - becouse method called here is "http://23.253.164.20:8096http://23.253.164.20:8096/getNamegetName"
see also my answer to Your second posting - there is working code of login:
getting parameter error in ksoap 2 android
Regards,
Marcin
i try to connect my Android application to a Webservice.
I wrote a new class and defined some Variables:
I´ve got the Async Class to use the Network
class GetValueTask extends AsyncTask<ApiConnector,Long,String> {
#Override
protected String doInBackground(ApiConnector... params) {
//wird im Background Thread ausgeführt
return params[0].getValue();
}
#Override
protected void onPostExecute(String s) {
//wird im Mainthread ausgeführt
MainActivity.this.setText(s);
}
}
And I have a Class where i want to call the Webservice
public class ApiConnector
{
private static final String SOAP_ACTION ="urn:microsoft-dynamics-schemas/codeunit/AddService:Add";
private static final String METHOD_NAME ="Add";
private static final String NAMESPACE ="urn:microsoft-dynamics-schemas/codeunit/AddService";
private static final String URL ="http://192.168.0.154:9047/DynamicsNAV80/WS/CRONUS%20AG/Codeunit/AddService";
private static final String USERNAME="B.Denger";
private static final String PASSWORD ="TestPW123!";
public String getValue() {
SoapObject request = new SoapObject(NAMESPACE,METHOD_NAME);
request.addProperty("no","10");
PropertyInfo unamePI = new PropertyInfo();
PropertyInfo passPI = new PropertyInfo();
// Set Username
unamePI.setName("username");
// Set Value
unamePI.setValue(USERNAME);
// Set dataType
unamePI.setType(String.class);
// Add the property to request object
request.addProperty(unamePI);
//Set Password
passPI.setName("password");
//Set dataType
passPI.setValue(PASSWORD);
//Set dataType
passPI.setType(String.class);
//Add the property to request object
request.addProperty(passPI);
SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
soapEnvelope.setOutputSoapObject(request);
HttpTransportSE aht= new HttpTransportSE(URL);
try {
aht.call(SOAP_ACTION, soapEnvelope);
SoapPrimitive resultString = (SoapPrimitive) soapEnvelope.getResponse();
return resultString.toString();
}catch(Exception e ) {
e.printStackTrace();
return "Fail at Call";
}
}
}
I have set the using-permission in the Manifest file
<uses-permission android:name="android.permission.INTERNET"/>
in my MainActivity do i execute the AsynkTask with a Button
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
GetValueTask getValueTask = new GetValueTask();
getValueTask.execute(new ApiConnector());
}
});
after execution i get following Logcat entry:
W/System.err﹕ org.ksoap2.transport.HttpResponseException: HTTP request failed, HTTP status: 401
i googled a whole Day for it, but i did not solved the problem yet.
is there anybody who could help me, or could give me a hint where i have to search?
I found the solution here:
Android Consuming Dynamics NAV SOAP Web Service
but it didnt Work with the jcif 1.3.17 jar
at https://jcifs.samba.org/src/ can you download the latest version.
In my case I fixed same problem by adding this code:
List<HeaderProperty> llstHeadersProperty = new ArrayList<>();
llstHeadersProperty.add(new HeaderProperty("Authorization", "Basic " + org.kobjects.base64.Base64.encode("user:password".getBytes())));
loHttpTransport.call(sSOAP_ACTION, loEnvelope, llstHeadersProperty);
Complete task:
private class fnAsyncTask extends AsyncTask<Void, Void, Void> {
#Override
protected Void doInBackground(Void... params)
{
//for linear parameter
SoapObject loRequest = new SoapObject(sNAMESPACE, sMETHOD_NAME);
// adding method property here serially
// loRequest.addProperty("CountryName", "france");
SoapSerializationEnvelope loEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
loEnvelope.implicitTypes = true;
loEnvelope.setOutputSoapObject(loRequest);
loEnvelope.dotNet = true;
HttpTransportSE loHttpTransport = new HttpTransportSE(_sURL);
loHttpTransport.debug = true;
try
{
List<HeaderProperty> llstHeadersProperty = new ArrayList<>();
llstHeadersProperty.add(new HeaderProperty("Authorization", "Basic " + org.kobjects.base64.Base64.encode("user:password".getBytes())));
loHttpTransport.call(sSOAP_ACTION, loEnvelope, llstHeadersProperty);
}
catch (HttpResponseException e)
{
// TODO Auto-generated catch block
Log.e("HTTPLOG", e.getMessage());
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
Log.e("IOLOG", e.getMessage());
e.printStackTrace();
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
Log.e("XMLLOG", e.getMessage());
e.printStackTrace();
} //send request
Object result = null;
try {
result = (Object )loEnvelope.getResponse();
//See output in the console
Log.i("RESPONSE",String.valueOf(result));
} catch (SoapFault e) {
// TODO Auto-generated catch block
Log.e("SOAPLOG", e.getMessage());
e.printStackTrace();
}
return null;
}
}
Full example
http://www.nascenia.com/consuming-soap-web-services-from-android/