How I Can Send An Array to WebService Method - android

i need to send a multiple values to a webservice -asp.net- via android application
this is the webservice method
<WebMethod()> _
Public Function AddTheNums(ByVal nums() As String,) As String
For i = 0 To nums.Length - 1
--some process
Next
Return status
End Function
and i use this code to in android
public class Login extends AsyncTask<String, Void, String>
{
public Login(String MethodName)
{
}
public void onPreExecute()
{
}
#Override
protected String doInBackground(String... params)
{
final SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("nums", params[0]);
request.addProperty("nums", params[1]);
final SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
envelope.dotNet = true;
try
{
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.call(SOAP_ACTION, envelope);
SoapPrimitive result = (SoapPrimitive) envelope.getResponse();
response = result.toString();
}
return response;
}
#Override
public void onPostExecute(String res)
{
}
}
can any one help me on this i need to send a multiple nums to the parameter in webserive
i used this code
request.addProperty("nums", params[0]);
request.addProperty("nums", params[1]);
but it doesnt work correctly ...
Best Regards

I'm not really sure that it's possible to use multiple params with same name in soapobject. Isn't it better to pass param like
request.addProperty("nums", params);
And I suppose this discussion can help https://groups.google.com/forum/#!topic/ksoap2-android/pq1V2ZXY3D8

Related

Response from SOAP webservice always returns null Android studio

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.

Server not able to read parameters while making a webservice connection

I am connecting to .net webservice using below code,i am able to connect and get incomplete response from server.I am adding userId has parameter with '1' has value of type string.The issue is 'server is not able to read the parameters at all i am sending in request' .I am not sure if something is wrong on my side.I have attached the webservice request and response and my code.
and my android connecting code
private class AsyncCallWS extends AsyncTask<String, Void, Void> {
#Override
protected Void doInBackground(String... params) {
//Invoke webservice
displayText=WebService.invokeHelloWorldWS(editText,"GetReminder");
return null;
}
#Override
protected void onPostExecute(Void result) {
//Set response
tv.setText(displayText);
//Make ProgressBar invisible
pg.setVisibility(View.INVISIBLE);
}
#Override
protected void onPreExecute() {
//Make ProgressBar invisible
pg.setVisibility(View.VISIBLE);
}
#Override
protected void onProgressUpdate(Void... values) {
}
}
webservice called here.
public class WebService {
//What is the Namespace of the Webservice ??
private static String NAMESPACE = "http://tempuri.org";
//Webservice URL
private static String URL = "http://1.3.44.5:8080/csmmobileapi/service.asmx";
//SOAP Action URI ???
private static String SOAP_ACTION = "http://tempuri.org/GetReminder";
private static final String TAG=null;
public static String invokeHelloWorldWS(String name, String webMethName)
{
boolean result=false;
String resTxt = null;
// Create request
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
SoapObject request = new SoapObject(NAMESPACE, webMethName);
envelope.setOutputSoapObject(request);
androidHttpTransport.debug=true;
// Property which holds input parameters
PropertyInfo sayHelloPI = new PropertyInfo();
// Set Name
sayHelloPI.setName("UserId");
// Set Value
sayHelloPI.setValue("1");
// Set dataType
sayHelloPI.setType(String.class);
// Add the property to request object
request.addProperty(sayHelloPI);
//Set envelope as dotNet
envelope.dotNet = true;
try {
// Invoke web service
androidHttpTransport.call(SOAP_ACTION, envelope);
// Get the response
String response=androidHttpTransport.responseDump;
Log.d("Result --- ", response.toString() );
//tried with SoapObject and SoapPrimitive
SoapObject obj=(SoapObject)envelope.getResponse();
Log.d("Result --- ", obj);
System.out.println("obj---->" + obj.toString());
}
Did a very silly mistake,i found out after doing so much trial n error work..,
private static String NAMESPACE = "http://tempuri.org"; changed to private static String NAMESPACE = "http://tempuri.org/"; and everything worked well.

SoapFault - faultcode: 'ns1:unexpected-error' android SOAP call

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

How can I make a ksoap2 call in async task?

I am a newbie on android development. I am trying to develop an application which will connect with .net webservice in order to retrieve data. I would like to make the ksoap2 call with AsyncTask. How I call it asyncronus with asynctask?
My SoapCall class is
public class SoapCall {
public final static String SOAP_ACTION = "http://www.alpha.net.com/ExecuteEBSCommand";
public final static String OPERATION_NAME = "ExecuteEBSCommand";
public final static String NAMESPACE = "http://www.alpha.net.com";
public final static String URL = "http://192.168.2.100/Ebs2Alpha/Service.asmx";
public String connection(String Command, String CommandParameters) throws Throwable, Throwable {
String response = null;
SoapObject Request = new SoapObject(NAMESPACE, OPERATION_NAME);
Request.addProperty("strCommand", Command);
Request.addProperty("strCommandParameters", CommandParameters);
SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
soapEnvelope.dotNet = true;
soapEnvelope.setOutputSoapObject(Request);
// Needed to make the internet call
// Allow for debugging - needed to output the request
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.debug = true;
// this is the actual part that will call the webservice
androidHttpTransport.call(SOAP_ACTION, soapEnvelope);
// Get the SoapResult from the envelope body.
SoapObject result = (SoapObject) soapEnvelope.bodyIn;
response = result.getProperty(0).toString();
return response;
}
}
So far I am getting the response by calling the connection method in main activity with
SoapCall call1= new SoapCall();
call1.connection("get_clients", "%");
Using AsyncTask is straightforward. Here is an example.
public class MyTask extends AsyncTask<String, Integer, String>{
#Override
protected String doInBackground(String... params) {
String response = null;
SoapObject Request = new SoapObject(NAMESPACE, OPERATION_NAME);
Request.addProperty("strCommand", params[0]);
Request.addProperty("strCommandParameters", params[1]);
SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
soapEnvelope.dotNet = true;
soapEnvelope.setOutputSoapObject(Request);
// Needed to make the internet call
// Allow for debugging - needed to output the request
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.debug = true;
// this is the actual part that will call the webservice
androidHttpTransport.call(SOAP_ACTION, soapEnvelope);
// Get the SoapResult from the envelope body.
SoapObject result = (SoapObject) soapEnvelope.bodyIn;
response = result.getProperty(0).toString();
return response;
}
}
And the call to the task with parameters.
MyTask myTask = new MyTask();
myTask.execute(new String[] {Command, CommandParameters});
Hope it will help.
I'd suggest you use the AsyncTaskLoader which for my taste is easier than the AsyncTask.
Have a look here, the example is very extensive and looks intimidating at first, you'll probably find much simpler ones. The idea is that your Activity implements LoaderCallbacks for the creation of the loader and a method that is being called when the loader has finished. You 'start' a loader via the LoaderManager.
The AsynctaskLoader is a class that extends AsyncTaskLoader and does the asynchronous stuff.
I'll give you a simple example:
This is the AsyncTaskLoader:
public class StartupLoader extends AsyncTaskLoader<Boolean> {
Context context;
public StartupLoader(Context context) {
super(context);
this.context = context;
forceLoad();
}
#Override
public Boolean loadInBackground() {
// DO STUFF!
return true;
}
#Override
protected void onStopLoading() {
}
#Override
public void onCanceled(Boolean data) {
super.onCanceled(data);
}
#Override
protected void onReset() {
super.onReset();
}
}
This is what you have in the Activity that will start the loader, it is an inner class:
public class StartupCallback implements
LoaderManager.LoaderCallbacks<Boolean> {
#Override
public void onLoadFinished(Loader<Boolean> loader, Boolean succ) {
// Here you get your results back
}
#Override
public Loader<Boolean> onCreateLoader(int id, Bundle args) {
return new StartupLoader(getApplicationContext());
}
#Override
public void onLoaderReset(Loader<Boolean> loader) {
}
}
And this is how you start the loader from whereever you want (within that Activity):
StartupCallback startupCallback = new StartupCallback();
getSupportLoaderManager().initLoader(0, null, startupCallback);
where 0 is an ID that you give the loader, null is a Bundle of arguments.
Good luck :)

calling a soap webservice from android

Hello I am trying to call a web service from android. The code is working fine with no errors but I get no output. I am new to android. Please help me out.
The whole tutorial is here ....
There are three buttons clear button do well but both convert to celsius and convert to fahrenheit don't work.
Actually there is the statement in both of them in the try block
SoapObject result = (SoapObject)envelope.bodyIn;
I guess at this line the application get stuck because I put the builder message after each statement it prompted but after this statement it did not .
Please tell me what is the problem, I am really worried..
try to use this class to call a web service:
public class WSRequest {
public HttpTransportSE androidHttpTransport;
public SoapSerializationEnvelope envelope;
public String methodName;
public SoapObject request;
public WSRequest(String methodName)
{
this.methodName = methodName;
this.request = new SoapObject(SRWebServer.NAMESPACE, methodName);
envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.implicitTypes = true;
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
androidHttpTransport = new HttpTransportSE(SRWebServer.URL);
}
public void RegisterMarshal()
{
MarshalBase64 marshal = new MarshalBase64();
marshal.register(envelope);
}
public SoapObject Send() throws IOException, XmlPullParserException
{
System.setProperty("http.keepAlive", "false");
new MarshalDate().register(envelope);
this.androidHttpTransport.call(SRWebServer.NAMESPACE + this.methodName, envelope);
return (SoapObject) this.envelope.getResponse();
}
public void AddProperties(String name, Object value)
{
this.request.addProperty(name, value);
}
//
}
and use this way:
WSRequest request = new WSRequest("method name here");
request.addProperties("property1Name",property1);
request.Send();
requestSend() will return a SoapObject containing the object received from the web service.

Categories

Resources