SOAP web service call from android - android

Could anyone please help in processing the SOAP request and response from an android application.
I have followed the tutorial at
http://www.c-sharpcorner.com/UploadFile/88b6e5/how-to-call-web-service-in-android-using-soap/
But when I run the application, it exits prematurely with the error
Unfortunately, has stopped.
I am running it in an emulator.
Can anyone please explain what I might be missing. Any example source code will be very useful.

I have one simple code for you. Where we have a button and we will be calling helloworld method.
Button Worldmethod;
private static final String SOAP_ACTION1 = "http://tempuri.org/HelloWorld";
private static final String HelloWorldmethod = "HelloWorld";
private static final String NAMESPACE = "http://tempuri.org/";
private static final String URL = "http://192.168.1.5/MobileDemo/Demo.asmx";
#Override
protected void onCreate(Bundle savedInstanceState) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
super.onCreate(savedInstanceState);
setContentView(R.layout.webservices_layout);
Worldmethod= (Button)findViewById(R.id.Worldmethod);
Worldmethod.setOnClickListener(this);
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.Worldmethod:
try
{
methodbody= (TextView)findViewById(R.id.methodbody);
SoapObject request1 = new SoapObject(NAMESPACE, HelloWorldmethod);
// add paramaters and values
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request1);
//Web method call
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.debug = true;
androidHttpTransport.call(SOAP_ACTION1, envelope);
//get the response
SoapPrimitive result= (SoapPrimitive)envelope.getResponse();
String results = result.toString();
methodbody.setText( ""+results);
}
catch (Exception e)
{
methodbody.setText(e.getMessage());
e.printStackTrace();
}
break;
}
}

Related

Android Web Service connenction Error

I created Java web services and trying to connect with android code.I am getting the service running but android app is showing error in soap object creating for namespace and method name.I did all the changes and everything is correct namespace,method name,URL all is correct.But I don't know what is wrong can anyone help me out...!
My android code is----->
showing error in
-"SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);"
public class RetailerActivity extends Activity {
private static final String SOAP_ACTION = "urn:training/searchCompanyInfo";
private static final String METHOD_NAME = "searchCompanyInfo";
private static final String NAMESPACE = "urn:training/";
private static final String URL = "http://localhost/attest/CompanyInfoService?wsdl";
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
System.out.println(NAMESPACE);
System.out.println(METHOD_NAME);
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
HttpTransportSE ht = new HttpTransportSE(URL);
try {
ht.call(SOAP_ACTION, envelope);
SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
SoapPrimitive s = response;
String str = s.toString();
String resultArr[] = str.split("&");//Result string will split & store in an array
TextView tv = new TextView(this);
for(int i = 0; i<resultArr.length;i++){
tv.append(resultArr[i]+"\n\n");
}
setContentView(tv);
} catch (Exception e) {
e.printStackTrace();
}
}
}
In Android, You cannot perform network operation on the main thread, You need to execute your soap request in a background thread. Please read up on AsyncTask.

Ksoap2 .NET web service connection

http://i.stack.imgur.com/pK9rm.png
http://i.stack.imgur.com/27Qj0.png
I want to connection web service using ksoap2,I have a working example but how can I integrate my application?
public class WebServiceDemoActivity extends Activity
{
private static String SOAP_ACTION = "http://tempuri.org/UrunleriListele";
private static String NAMESPACE = "http://tempuri.org/";
private static String METHOD_NAME = "UrunleriListele";
private static String URL = "http://services.annebebekavm.com/Service1.asmx?WSDL";
Button btnFar,btnCel,btnClear;
EditText txtFar,txtCel;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnFar = (Button)findViewById(R.id.btnFar);
txtFar = (EditText)findViewById(R.id.txtFar);
btnFar.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("Fahrenheit",txtFar.getText().toString());
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
envelope.dotNet = true;
try {
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.call(SOAP_ACTION, envelope);
SoapObject result = (SoapObject)envelope.bodyIn;
if(result != null)
{
txtFar.setText(result.getProperty(0).toString());
}
else
{
Toast.makeText(getApplicationContext(), "No Response",Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
Please reconsider to use another client
http://code.google.com/p/android-ws-client/
you can create all classe what you need to.
by one application copy libs to your project
with your generate classes
Here is example it look really simple to
Service servis = new Service();
ServiceSoap soap12 = servis.getServiceSoap12();
LoginParams inParams = new LoginParams();
inParams.setLogin("user");
inParams.setPassword("pass");
inParams.setStrategy(LoginStrategy.NOHASHPASS); // even enums are supported
LoginState result = soap.authorize(inParams);
result.isLogin(); // boolean if is succesfull

Calling webservice in ksoap2

I've got a problem with this webservice. I used to call diffrent ws in my project and it works fine.
private static final String URL2 = "http://46.248.168.51/webservice/soap/endpoint/apikey/cb7f1f308e82ca2be8541d5ba829dc1e/?wsdl";
private static final String METHOD_NAME2 = "getObjectList";
private static final String SOAP_ACTION2 = "http://46.248.168.51/webservice/soap/endpoint/apikey/cb7f1f308e82ca2be8541d5ba829dc1e/getObjectList";
private static final String NAMESPACE2 = "http://46.248.168.51/webservice/soap/endpoint/apikey/cb7f1f308e82ca2be8541d5ba829dc1e/";
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.pp);
SoapObject request = new SoapObject(NAMESPACE2, METHOD_NAME2);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
AndroidHttpTransport httpTransport = new AndroidHttpTransport(URL2);
httpTransport.debug = true;
try {
httpTransport.call(SOAP_ACTION2, envelope);
SoapObject result = (SoapObject) envelope.getResponse();
} catch (Exception e) {
Log.d("e.getMessage()", e.getMessage());
e.getMessage();
e.printStackTrace();
}
httpTransport.call returns errors
11-10 15:31:43.421: D/e.getMessage()(2550): expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope (position:START_TAG #2:486 in java.io.InputStreamReader#4124e8d0)
Thanks for any help
Since your code works with several other web services. I think you have only changed the URL,SOAP Action and Method. So please check weather the response contain a popper SOAP message. To check that you can something like TCPMon in between the communication.

Showing result in tabhost on android eclipse

The above image is just adding two values and displaying the result on the below textview.
But i need to show that answer on the first tab(ie.Tab_1) of tabhost on next screen.
How to do this?
.java class
public class Demo_webserviceActivity extends Activity
{
/** Called when the activity is first created. */
private static String NAMESPACE = "http://tempuri.org/";
private static String METHOD_NAME = "FahrenheitToCelsius";
private static String SOAP_ACTION = "http://tempuri.org/FahrenheitToCelsius";
private static String URL = "http://www.w3schools.com/webservices/tempconvert.asmx?WSDL";
Button btnFar;
EditText txtFar;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnFar = (Button)findViewById(R.id.btnFar);
txtFar = (EditText)findViewById(R.id.txtFar);
btnFar.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
String b;
//Initialize soap request + add parameters
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
//Use this to add parameters
request.addProperty("Fahrenheit",txtFar.getText().toString());
//Declare the version of the SOAP request
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
envelope.dotNet = true;
try
{
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
//this is the actual part that will call the webservice
androidHttpTransport.call(SOAP_ACTION, envelope);
// Get the SoapResult from the envelope body.
SoapPrimitive result = (SoapPrimitive)envelope.getResponse();
if(result != null)
{
//Get the first property and change the label text
b = result.toString();
Intent i = new Intent(getApplicationContext(),Activity2.class);
i.putExtra("gotonextpage", b.toString());
startActivity(i);
}
else
{
Toast.makeText(getApplicationContext(), "No Response",Toast.LENGTH_SHORT).show();
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
});
}
}
NOTE : This source is actually consuming a webservice from android and showing the result by SOAP method
Thanks a lot.
You can use Shared preferences to store values, then you can access it in any activity of your project:
follow this link

How do I use parameters with ksoap2?

I have created a web service to test passing parameters via ksoap2. I thought it seemed like a pretty simple process, but I'm apparently missing something simple. The web service does nothing more than return the integer that is passed to it. When I execute the code below it returns the number 0 instead of 1.
private static String SOAP_ACTION = "http://tempuri.org/TestParams";
private static String NAMESPACE = "http://tempuri.org/";
private static String METHOD_NAME = "TestParams";
private static String URL = "http://services.lockrem.com/WebService.asmx?WSDL";
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("RoundId", 1);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
try {
androidHttpTransport.call(SOAP_ACTION, envelope);
} catch (Exception e) {
e.printStackTrace();
}
SoapObject result = (SoapObject)envelope.bodyIn;
Note: I have tested this using a .net web page and it returns the number 1 as expected. The web service is not the issue here.
Here is the web service.
[WebMethod]
public int TestParams(int RoundId)
{
return RoundId;
}
Thank you for any help.
Disclaimer: Whenever I try to talk to a .NET webservice using kSoap2, I usually end up doing a lot of fiddling. And I mean that literally: I setup a windows machine with Fiddler2 and use that as a proxy for both the failing request and a working reference request, so I can actually see what's going wrong. I strongly advise doing something similar as soon as you try to do something non-trivial.
That said, in your case, it might be enough to do
envelope.dotNet = true;
Try removing the WSDL at the end
private static String URL = "http://services.lockrem.com/WebService.asmx";
In addition try using this to get the response:
Object response = envelope.getResponse();
instead of SoapObject result = (SoapObject)envelope.bodyIn;
EDITED
This works:
public class MainActivity extends Activity {
private static String NAMESPACE = "http://tempuri.org/";
private static String METHOD_NAME = "TestParams";
private static String URL = "http://services.lockrem.com/WebService.asmx";
private TextView tv;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv = (TextView)findViewById(R.id.tv);
HttpTransportSE transport = new HttpTransportSE(URL);
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("RoundId", 1);
SoapSerializationEnvelope envelope =
new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
try {
transport.call(NAMESPACE + METHOD_NAME, envelope);
Object response = envelope.getResponse();
tv.setText(response.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
}
Hope it helps.

Categories

Resources