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
Related
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
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/
I am trying to call SOAP web service using one WSDL file.
I have added all required parameters in it.
But I am getting error as below:
SoapFault - faultcode: 'ns1:unexpected-error' faultstring: 'Fault occurred while processing.' faultactor: 'null' detail: null in android
Here is my code sample:
class RegisterMember extends AsyncTask<Void, Void, Void> {
String SOAP_ACTION = "";
String METHOD_NAME = "registerMember";
String NAMESPACE = "http://XXXXX.XX";
String URL="http://XXXX.XX?WSDL";
SoapPrimitive result1;
String str;
#Override
protected void onPreExecute() {
mProgressDialog = new ProgressDialog(MainActivity.this);
mProgressDialog.setMessage("Checking For Activation");
mProgressDialog.show();
}
#Override
protected Void doInBackground(Void... params) {
try {
StringBuffer sb;
SoapObject request=new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("name", "XXXX");
request.addProperty("email", "XXXX#gmail.com");
request.addProperty("username", "XXXXX");
request.addProperty("password", "XXXX");
request.addProperty("mobile", "XXXXXXX");
request.addProperty("pin", "XXXX");
request.addProperty("dob", "XX/XX/XXXX");
request.addProperty("gender", "male");
request.addProperty("address", "XXXXX");
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet=true;
envelope.setOutputSoapObject(request);
envelope.bodyOut = request;
Log.d("In try","In Try");
HttpTransportSE ht = new HttpTransportSE(URL);
ht.call(NAMESPACE+METHOD_NAME, envelope);
Log.d("In try","In Try1");
result1 = (SoapPrimitive)envelope.getResponse();
//SoapObject resultObj = (SoapObject)envelope.getResponse();
/*int numProp = resultObj.getPropertyCount();
sb = new StringBuffer();
for(int jj=0; jj<numProp; jj++) {
sb.append((String) resultObj.getProperty(jj) + "\n");
Log.d("For Loop", String.valueOf(sb.append((String) resultObj.getProperty(jj))));
}*/
Log.d("Envelope", String.valueOf(result1));
// str = envelope.getResponse();
// status= Boolean.valueOf(result1.toString());
// str = result1.toString();
Log.w("String Response of CheckActivation Status - - - - - - - - - -", str);
Log.w("CheckActivation Status - - - - - - - ->>>>>>>>>", String.valueOf(result1));
} catch (Exception e) {
Log.d("No Data Found",e +"");
}
try {
Thread.sleep(1000);
} catch(Exception ex) {
}
return null;
}
#Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
Log.d("Response = = = = =",String.valueOf(result));
mProgressDialog.dismiss();
}
}
I doubt that the SOAPACTION might be causing issue. Is that possible if we have SOAPACtion blank and we call web service?
I have used same code for other web service, with .svc url, and works fine, so I dont think code should have any problem.
SOAP version:1.1
ksoap library version: ksoap2-android-assembly-2.6.0-jar-with-dependencies.jar
Any help is appreciated.
Thanks
Try replacing result1 = (SoapPrimitive)envelope.getResponse();by
result1 = (SoapPrimitive)envelope.bodyIn();
and also set a SOAP_ACTION !
Also this is how you add a property to the request :
PropertyInfo pi = new PropertyInfo();
pi.name = NAME;
pi.type = String.class;
request.addProperty(pi, VALUE);
You need to check whether SOAP wsdl has which style, document or RPC.
both have different WSDL format, and it may possible, if you try to call WSDL with document type, might not give response with same code work for other one.
So please cross check this and confirm.
Regards
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).
I'm writing an Android App that communicates with an web service using KSOAP. The connection between web service and Android app is working as I can call the webservice and get a return value (hello). But if I try to give a name from the App to the web service via .addProperty the webservice returns a null object.
Here is my code:
MainActivity:
private final String NAMESPACE_Local = "http://test.com/";
private final String URL_Local = "http://168.185.226.21:7001/myTest/myTestWebServiceService";
private final String SOAP_ACTION_Local = "Hello_Action_Extend";
private final String METHOD_NAME_Local = "hello_extend";
public void LocalServer(View view)
{
TextView text = (TextView) findViewById(R.id.update_text);
SoapObject request = new SoapObject(NAMESPACE_Local, METHOD_NAME_Local);
request.addProperty("name", "Christian");
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL_Local);
try {
androidHttpTransport.call(SOAP_ACTION_Local, envelope);
SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
Log.i("myApp", response.toString());
text.setText(response.toString());
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(this,"Device or service offline",Toast.LENGTH_LONG).show();
}
}
WebServer:
package com.test;
import javax.jws.*;
#WebService
public class myTestWebService {
#WebMethod(action="Hello_Action") //that method works
public String hello() {
return "hello";
}
#WebMethod(action="Hello_Action_Extend")
public String hello_extend(String name) //that works also, but it is giving back "hello null"
{
return "hello "+name;
}
}
I hope you can help me!
Try replacing:
request.addProperty("name", "Christian");
for:
request.addProperty("name",ElementType.STRING_CLASS, "Christian");
and the response for:
SoapObject reponse=(SoapObject)envelope.getResponse();
response.getProperty("name");
API SoapObject