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
Related
private static String SOAP_ACTION1 = "http://www.w3schools.com/webservices/FahrenheitToCelsius";
private static String SOAP_ACTION2 = "http://www.w3schools.com/webservices/CelsiusToFahrenheit";
private static String NAMESPACE = "http://www.w3schools.com/webservices/";
private static String METHOD_NAME1 = "FahrenheitToCelsius";
private static String METHOD_NAME2 = "CelsiusToFahrenheit";
private static String URL = "http://www.w3schools.com/webservices/tempconvert.asmx?WSDL";
Button btnFar,btnCel,btnClear;
EditText txtFar,txtCel;
int value;
SoapObject result;
My code look like below
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnFar = (Button)findViewById(R.id.btnFar);
btnCel = (Button)findViewById(R.id.btnCel);
btnClear = (Button)findViewById(R.id.btnClear);
txtFar = (EditText)findViewById(R.id.txtFar);
txtCel = (EditText)findViewById(R.id.txtCel);
btnFar.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
value = Integer.parseInt(txtFar.getText().toString());
new soapFahrenheit().execute();
}
});
btnClear.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
txtCel.setText("");
txtFar.setText("");
}
});
}
private class soapFahrenheit extends AsyncTask<String, Integer, String> {
protected void onPreExecute()
{
//
txtCel.setText(value);
}
#Override
protected String doInBackground(String... arg0){
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME1);
//Use this to add parameters
request.addProperty("Fahrenheit",value);
//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);
androidHttpTransport.call(SOAP_ACTION1, envelope);
result = (SoapObject)envelope.bodyIn;
} catch (Exception e) {
e.printStackTrace();
}
return result.getProperty(0).toString();
}
protected void onPostExecute(SoapObject result)
{
if(result != null)
{
txtFar.setText(result.getProperty(0).toString());
}
else
{
Toast.makeText(getApplicationContext(), "No Response",Toast.LENGTH_LONG).show();
}
}
}
}
did i miss anything here? this is killing my days.... please help
You are making a network communication on the main UI thread which not allowed Android 4.0+.
So, you need to create a background thread to make the request to the server.
AsyncTask is what you should be using.
The problem with the ACTION String
http://www.w3schools.com/webservices/tempconvert.asmx?op=FahrenheitToCelsius
And xml form
POST /webservices/tempconvert.asmx HTTP/1.1
Host: www.w3schools.com
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://www.w3schools.com/webservices/FahrenheitToCelsius" // ACTION
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<FahrenheitToCelsius xmlns="http://www.w3schools.com/webservices/">
<Fahrenheit>string</Fahrenheit>
</FahrenheitToCelsius>
</soap:Body>
</soap:Envelope>
So the ACTION name should like
private static String SOAP_ACTION1 = "http://www.w3schools.com/webservices/FahrenheitToCelsius";
and
private static String SOAP_ACTION2 = "http://www.w3schools.com/webservices/CelsiusToFahrenheit";
Hope this will solve your problem.
And use dont't do network operation on UI Thread. Use AsynTask
EDIT:
private void doNetwork() {
SoapObject resultRequestSOAP = new SoapObject(NAMESPACE1, METHOD_NAME1);
PropertyInfo pi1 = new PropertyInfo();
pi1.setName("Fahrenheit");
pi1.setValue(""+value);// Say value= 10
pi1.setType(PropertyInfo.STRING_CLASS);
resultRequestSOAP.addProperty(pi1);
SoapSerializationEnvelope envp = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envp.dotNet = true;
try {
envp.setOutputSoapObject(resultRequestSOAP);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.debug = true;
androidHttpTransport.call(SOAP_ACTION1, envp);
SoapObject response = (SoapObject) envp.bodyIn;
System.out.println("response"+response.toString() + " "+response.getProperty(0).toString());
} catch (Exception e) {
}
}
OUTPUT:
FahrenheitToCelsiusResponse{FahrenheitToCelsiusResult=-12.2222222222222; }
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.
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
I used the code below
public class SharepointListActivity extends Activity {
private static final String SOAP_ACTION = "http://schemas.microsoft.com/sharepoint/soap/Login";
private static final String METHOD_NAME = "Login";
private static final String NAMESPACE = "http://schemas.microsoft.com/sharepoint/soap/" ;
private static final String URL = "http://192.168.0.25:1000/_vti_bin/authentication.asmx";
private TextView result;
private Button btnSubmit;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
result = (TextView)findViewById(R.id.editText1);
btnSubmit = (Button)findViewById(R.id.button1);
btnSubmit.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
if(v.getId() == R.id.button1)
{
String list = getMobileTestList();
result.setText(list);
}
}
});
}
private String getMobileTestList()
{
PropertyInfo pi = new PropertyInfo();
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
List<HeaderProperty> headers = new ArrayList<HeaderProperty>();
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE transport = new HttpTransportSE(URL);
try
{
transport.debug = true;
transport.call(SOAP_ACTION, envelope);
Object result = envelope.getResponse();
return result.toString();
}
catch(Exception e)
{
return e.toString();
}
}
}
and in response I have xml like this
<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<LoginResponse xmlns="http://schemas.microsoft.com/sharepoint/soap/">
<LoginResult>
<CookieName>FedAuth</CookieName>
<ErrorCode>NoError</ErrorCode>
<TimeoutSeconds>1800</TimeoutSeconds>
</LoginResult>
</LoginResponse>
</soap:Body>
</soap:Envelope>
What I should do next so I can fetch answer from GetList method. How I should auth in?
If I change METHOD_NAME, SOAP_ACTION and other params for GetList method I have error - 403 forbidden. cause wrong auth so how should I build correct query?
or what should i do with first answer for next query?
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