Passing two parameters to consume a web service from android - android

How to pass two parameters to consume a web service from android by the method SOAP.
I have tried,but i am unable to consume that web service from android.
Suggestions?
This is the exact web service which i am trying to consume "http://54.251.60.177/TMSOrdersService/TMSDetails.asmx"
The input values for this web service are
FromDate : 01/01/2012
ToDate : 07/07/2012
Sources for reference
Webservice_.java
public class Webservice_ extends Activity
{
public static String rslt=""; /** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button b1=(Button)findViewById(R.id.button1);
final AlertDialog ad=new AlertDialog.Builder(this).create();
b1.setOnClickListener(new OnClickListener() {
public void onClick(View arg0)
{
try
{
EditText ed1=(EditText)findViewById(R.id.editText1);
EditText ed2=(EditText)findViewById(R.id.editText2);
int a=Integer.parseInt(ed1.getText().toString());
int b=Integer.parseInt(ed2.getText().toString()); rslt="START";
Caller c=new Caller();
c.FromDate=a;
c.ToDate=b;
c.ad=ad;
c.join();
c.start();
while(rslt=="START")
{
try {
Thread.sleep(10);
}catch(Exception ex) {
}
} ad.setTitle(+a +b);
ad.setMessage(rslt);
}catch(Exception ex) {
ad.setTitle("Error!"); ad.setMessage(ex.toString());
}
ad.show();
} });
}}
CallSoap.java
public class CallSoap
{
public final String METHOD_NAME = "GetTMSChart";
public final String NAMESPACE = "http://tempuri.org/";
public final String SOAP_ACTION = "http://tempuri.org/GetTMSChart";
public final String URL = "http://54.251.60.177/TMSOrdersService/TMSDetails.asmx";
public CallSoap()
{
}
public String Call(int FromDate,int ToDate)
{
SoapObject request = new SoapObject(NAMESPACE,SOAP_ACTION);
PropertyInfo pi=new PropertyInfo();
pi.setName("FromDate");
pi.setValue(FromDate);
pi.setType(Date.class);
request.addProperty(pi);
pi=new PropertyInfo();
pi.setName("ToDate");
pi.setValue(ToDate);
pi.setType(Date.class);
request.addProperty(pi);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE httpTransport = new HttpTransportSE(URL);
Object response=null;
try
{
httpTransport.call(SOAP_ACTION, envelope);
response = envelope.getResponse();
}
catch (Exception exception)
{
response=exception.toString();
}
return response.toString();
}
}
Caller.java
public class Caller extends Thread
{
public CallSoap cs;
public int FromDate,ToDate;
protected AlertDialog ad;
public void run()
{
try
{
cs=new CallSoap();
String resp=cs.Call(FromDate, ToDate);
Webservice_.rslt=resp;
}
catch(Exception ex)
{
Webservice_.rslt=ex.toString();
}
}
}
Thanks for your precious time!..

The core code would be something like following in your javafile...
SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE,OPERATION_NAME);
PropertyInfo pi=new PropertyInfo();
pi.setName("a");
pi.setValue(a);
pi.setType(Integer.class);
request.addProperty(pi);
pi=new PropertyInfo();
pi.setName("b");
pi.setValue(b);
pi.setType(Integer.class);
request.addProperty(pi);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
Here is complete one by one step to produce the same.
Hope this helps.
On top of that you can see this link,
Thanks,
Jigar

Related

java.net.UnknownHostException(Unable to resolve host “URL”: No address associated with hostname)

I am developing an app for testing asp.net webservice in android.for that a simple webservice for adding two numbers is used.where the numbers are passed as parameters.the result is get in an dialogue.the webserver works in local. when the application runs following result get as an exception.i provided the internet permission in manifest.
java.net.UnknownHostException(Unable to resolve host
“http://url.com/”: No address associated with hostname)
my code given below
Main Activity
public static String rslt=""; /** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b1=(Button)findViewById(R.id.button1);
final AlertDialog ad=new AlertDialog.Builder(this).create();
b1.setOnClickListener(new OnClickListener() {
#Override public void onClick(View arg0) {
// TODO Auto-generated method stub
try
{
EditText ed1=(EditText)findViewById(R.id.editText1);
EditText ed2=(EditText)findViewById(R.id.editText2);
int a=Integer.parseInt(ed1.getText().toString());
int b=Integer.parseInt(ed2.getText().toString());
rslt="START";
Caller c=new Caller();
c.a=a;
c.b=b;
// c.ad=ad;
c.join();
c.start();
while(rslt=="START") {
try {
Thread.sleep(10);
}catch(Exception ex) {
}
}
ad.setTitle("RESULT OF ADD of "+a+" and "+b);
ad.setMessage(rslt);
}catch(Exception ex) {
ad.setTitle("Error!"); ad.setMessage(ex.toString());
}
ad.show();
} });
}
}
Caller.java
public class Caller extends Thread {
public CallSoap cs;
public int a, b;
public void run() {
try {
cs = new CallSoap();
String resp = cs.Call(a, b);
MainActivity.rslt = resp;
} catch (Exception ex) {
MainActivity.rslt = ex.toString();
}
}
}
CallSoap.java
public class CallSoap
{
public final String SOAP_ACTION = "http://tempuri.org/Add";
public final String OPERATION_NAME = "Add";
public final String WSDL_TARGET_NAMESPACE = "http://tempuri.org/";
public final String SOAP_ADDRESS = "url";
public CallSoap()
{
}
public String Call(int a,int b)
{
SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE,OPERATION_NAME);
PropertyInfo pi=new PropertyInfo();
pi.setName("a");
pi.setValue(a);
pi.setType(Integer.class);
request.addProperty(pi);
pi=new PropertyInfo();
pi.setName("b");
pi.setValue(b);
pi.setType(Integer.class);
request.addProperty(pi);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS);
Object response=null;
try
{
httpTransport.call(SOAP_ACTION+OPERATION_NAME, envelope);
response = envelope.getResponse();
}
catch (Exception exception)
{
response=exception.toString();
}
return response.toString();
}
please help me.thanks in advance.
I think you are testing it in an Emulator. Since the service is in a real server, I prefer using a real device. Also, using emulator is OK. But some emulator shows weird behavior with real web services. Try using a different emulator, preferably a newly created one.

Facing Java.net.SocketException: Permission Denied

I am getting no errors in LogCat. The ip showing in code is localhost ip address. I have availability of internet in my AVD.
My MainActivity.java is :
public class MainActivity extends Activity {
private static final String SOAP_ACTION = "http://tempuri.org/add";
private static final String METHOD_NAME = "add";
private static final String NAMESPACE = "http://tempuri.org/";
private static final String URL = "http://101.63.111.137/yash/DemoService.asmx";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button callMe = (Button) findViewById(R.id.b1);
callMe.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
String responseData = getData();
((TextView) findViewById(R.id.textView1)).setText("Response Received is: " + responseData);
}
});
}
private String getData() {
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
PropertyInfo pi = new PropertyInfo();
pi.setName("i");
pi.setValue(((EditText) findViewById(R.id.e1)).getText().toString());
pi.setType(int.class);
request.addProperty(pi);
PropertyInfo pi2 = new PropertyInfo();
pi2.setName("j");
pi2.setValue(((EditText) findViewById(R.id.e2)).getText().toString());
pi2.setType(int.class);
request.addProperty(pi2);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport=new HttpTransportSE(URL);
try {
androidHttpTransport.call(SOAP_ACTION, envelope);
SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
return response.toString();
} catch (Exception e) {
e.printStackTrace();
return e.toString();
}
}
}
I have added these lines in AndroidManifest.xml
Thanks in advance.
Have you added INTERNET_ACCESS permission to your Manifest?
<manifest xlmns:android...>
...
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
</manifest>
SocketException

Unable to call Java Webservice from android application

I am trying to call java webservice form android application but unable to call it. When I generate WSDL file, SOAPAction showing blank
string(<soap:operation soapAction=""/>) in soap:operation.
My android application code:
public class MainActivity extends Activity {
private static final String SOAP_ACTION = "http://com/add";
private static final String METHOD_NAME = "add";
private static final String NAMESPACE = "http://com/";
private static final String URL = "http://localhost:8080/WebApplication1/demo";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button=(Button)findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
PropertyInfo pi1 = new PropertyInfo();
pi1.setName("i");
pi1.setValue(3);
pi1.setType(int.class);
request.addProperty(pi1);
PropertyInfo pi2 = new PropertyInfo();
pi2.setName("j");
pi2.setValue(3);
pi2.setType(int.class);
request.addProperty(pi2);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
// HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL);
try {
((TextView) findViewById(R.id.textView1)).setText(String
.valueOf("The WebService is about to call"));
androidHttpTransport.call(SOAP_ACTION, envelope);
((TextView) findViewById(R.id.textView1)).setText(String
.valueOf("The WebService call is done"));
SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
((TextView) findViewById(R.id.textView1)).setText(String
.valueOf(response.toString()));
((TextView) findViewById(R.id.textView1)).setText(String
.valueOf("Done"));
} catch (Exception e) {
e.printStackTrace();
Log.e("Err", "Error Says: " + e.toString());
}
}
});
}
}
There is showing an exception network on main thread exception
Because you are calling the web service in your main thread.This must be done in the background using background thread.You can use asyntask for background operations.

Android Soap Web service Error behind Proxy Server

I have written a program for communicating with a web service and get response value. But when i debug the programme i end with requestDump=null at the line androidHttpTransport.call(SOAP_ACTION, envelope); Can some one tell me the reason for the error and what can i do for this
public class WebService extends Activity {
private final String NAMESPACE = "http://tempuri.org/";
private final String URL = "http://www.w3schools.com/webservices/tempconvert.asmx";
private final String SOAP_ACTION = "http://tempuri.org/CelsiusToFahrenheit";
private final String METHOD_NAME = "CelsiusToFahrenheit";
String celsius;
Button b;
TextView tv;
EditText et;
String res,resultval;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web_service);
et=(EditText)findViewById(R.id.editText1);
tv=(TextView)findViewById(R.id.Result);
b=(Button)findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
//String result=getFarenheit(et.getText().toString());
//tv.setText(result+"°F");
new service().execute();
}
});
}
private class service extends AsyncTask<Void, Void, String>{
#Override
protected String doInBackground(Void... arg0) {
celsius=et.getText().toString();
SoapObject request= new SoapObject(NAMESPACE, METHOD_NAME);
PropertyInfo celsuiusPI= new PropertyInfo();
celsuiusPI.setName("Celsius");
celsuiusPI.setValue(celsius);
celsuiusPI.setType(String.class);
request.addProperty("XMLMarks",celsuiusPI);
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);
SoapPrimitive response;
HttpTransportSE androidHttpTransport=new HttpTransportSE(URL);
try{
androidHttpTransport.setXmlVersionTag("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
androidHttpTransport.debug = true;
androidHttpTransport.call(SOAP_ACTION, envelope);
String dump= androidHttpTransport.requestDump.toString();
response=(SoapPrimitive)envelope.getResponse();
Toast.makeText(WebService.this, response.toString(), 20).show();
Log.i("WebService output", response.toString());
System.out.println("WebService Response"+response.toString());
Object res= response.toString();
resultval=(String) res;
}
catch(Exception e){
e.printStackTrace();
}
return res;
}
protected void onPostExecute(String h){
String result=h;
tv.setText(result+"°F");
}
}
}
Just replace your service AsyncTask with this new one and see result:
code:
private class service extends AsyncTask<Void, Void, String> {
#Override
protected String doInBackground(Void... arg0) {
System.out.println("In DoIn Background");
// Initialize soap request + add parameters
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
// Use this to add parameters
request.addProperty("Celsius", txtCel.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.
SoapObject result = (SoapObject) envelope.bodyIn;
if (result != null) {
// Get the first property and change the label text
// txtFar.setText(result.getProperty(0).toString());
res = result.getProperty(0).toString();
} else {
Toast.makeText(getApplicationContext(), "No Response",
Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
e.printStackTrace();
}
return res;
}
protected void onPostExecute(String h) {
String result = h;
tv.setText(result + "°F");
}
}

How to pass parameter value to webservice in android

Hi friends
I am using ksoap2 in android can anybody tell how to pass parameter value to webservice in android
Thanks
public class AndroidClientService extends Activity {
private static final String SOAP_ACTION = "http://10.120.10.87:8080/TestService/services/TestService/saveServices";
private static final String OPERATION_NAME = "saveServices";
private static final String WSDL_TARGET_NAMESPACE = "http://10.120.10.87:8080/TestService/services/TestService?WSDL";
private static final String SOAP_ADDRESS = "http://10.120.10.87:8080/TestService/services/TestService";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView textView = new TextView(this);
setContentView(textView);
SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE,
OPERATION_NAME);
/*
* PropertyInfo pi = new PropertyInfo(); pi.setName("celcius"); pi.type
* = PropertyInfo.OBJECT_CLASS; pi.setValue(100);
* request.addProperty(pi);
*/
request.addProperty("number", "8806007");
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS);
try
{
httpTransport.call(SOAP_ACTION, envelope);
Object response = envelope.getResponse();
textView.setText(response.toString());
}
catch (Exception exception)
{
textView.setText(exception.toString());
}
}
}
Refer : WEBSERVICE

Categories

Resources