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?
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; }
Thanks in advance please forgive me if I am making any mistake while explaining the problem.
I am new TO SOAP technology. I want to call a WCF .net based SOAP web service the request for the SOAP will look something like below
<soapenv:Envelope
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">
<soapenv:Header/>
<soapenv:Body>
<tem:IsServiceLive_V>
<!--Optional:-->
<tem:version>1</tem:version>
</tem:IsServiceLive_V>
</soapenv:Body>
</soapenv:Envelope>
and I am trying to make the request in the code like this
public class MainActivity extends Activity {
public static String SOAP_ACTION1="http://tempuri.org/IService1/IsServiceLive_V";
public static String SOAP_ACTION2="http://tempuri.org/IService1/IsServiceLive_V";
public static String METHOD_NAME1="IsServiceLive_V";
public static String METHOD_NAME2="IsServiceLive_V";
public static String NAMESPACE="http://tempuri.org/";
public static String URL="http://64.27.48.117:3340/service/Service1.svc?wsdl";
Button btnFar,btnCel,btnClear;
EditText txtFar,txtCel;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.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)
{
//Initialize soap request + add parameters
try
{
new SoapResult(1).execute();
}
catch(NullPointerException e)
{
}
}
});
}
public class SoapResult extends AsyncTask<Void, Void, SoapObject>
{
int flag_status;
SoapObject final_result;
public SoapResult(int flag) {
// TODO Auto-generated constructor stub
flag_status=flag;
}
#Override
protected SoapObject doInBackground(Void... arg0) {
// TODO Auto-generated method stub
if(flag_status==1)
{
//Initialize soap request + add parameters
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME1);
//Use this to add parameters
/* PropertyInfo property=new PropertyInfo();
property.setName("tmp");
property.setValue("GetRestaurantList");
request.addProperty(property);*/
request.addProperty("version", "1");
//Declare the version of the SOAP request
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
System.out.println(request.toString());
envelope.dotNet = true;
try {
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
//this is the actual part that will call the webservice
androidHttpTransport.call(SOAP_ACTION1, envelope);
System.out.println("ok");
// Get the SoapResult from the envelope body.
SoapObject result = (SoapObject)envelope.bodyIn;
System.out.println(result.toString());
//SoapResult soap=(SoapResult)envelope.getResponse();
//Get the first property and change the label text
final_result=result;
} catch (Exception e) {
e.printStackTrace();
System.out.println("hiiii...");
}
return final_result;
}
#Override
protected void onPostExecute(SoapObject result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
if(flag_status==1)
//txtCel.setText(result.getProperty(0).toString());
if(flag_status==0)
{
// txtFar.setText(result.getProperty(0).toString());
}
}
}
and the SOAP response in SOAP format should look something like this
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
<IsServiceLive_VResponse xmlns="http://tempuri.org/">
<IsServiceLive_VResult>true</IsServiceLive_VResult>
</IsServiceLive_VResponse>
</s:Body>
</s:Envelope>
but it is giving me exception java.lang.IllegalArgumentException: size <= 0
after the call
androidHttpTransport.call(SOAP_ACTION1, envelope);
The XML SOAP request and responses are generated using Soup UI tool and are working fine
just not getting where i am going wrong a little bit help would be appreciable..
Once I was having the same issue. There might be some problem with your service.
And use this
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER12);
Instead of
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
Got the answer for what i was looking for . for the WCF web services we have to create separate class for each method to get the data since it cannot be run from the code where you wanted the results . So if anyone has the problem that I faced please do remember that to consume the WCF web service in your android project Create a different class for each method for the web service .I am giving an simple example below
public class GetNearBy {
public static String HOTEL_NAME="hotel_name";
public static String NUMBER_OF_BRANCHES="branches";
public static String IMAGE1="image1";
public static String IMAGE2="image2";
public static String IMAGE3="image3";
public static String IMAGE4="image4";
public static String IMAGE5="image5";
public static String IMAGE6="image6";
public static String HAS_EVENT="HasEvent";
public static String HAS_OFFER="HasOffer";
public static String HAS_MENU="HasMenu";
public static String HAS_POPULARFOOD="HasPopularFood";
public static String ID="Id";
public static String IS_OPEN="IsOpen";
public static String LAT="LAT";
public static String LONG="LONG";
public static String LOCATION="Location";
public static String STARS="Stars";
public static String FACILITY_COUNT="facility_count";
private static final String NAMESPACE = "http://tempuri.org/";
private static final String METHOD_NAME = "GetNearBy";
private static final String SOAP_ACTION = "http://tempuri.org/IService1/GetNearBy";
private static final String URL = "http://xxxxxx/service/Service1.svc/soap";
public static ArrayList<HashMap<String, String>> connectToSOAPService()
{
ArrayList<HashMap<String, String>> data=new ArrayList<HashMap<String,String>>();
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
// creating SOAP envelope
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
// this should be true even in not .NET services
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
String resultValue = "";
try {
// calling an action
androidHttpTransport.call(SOAP_ACTION, envelope);
// receiving response
SoapObject response = (SoapObject) envelope.getResponse();
for(int i=0;i<response.getPropertyCount();i++)
{
HashMap<String, String> result=new HashMap<String, String>();
SoapObject ele1=(SoapObject)response.getProperty(i);
String Hotel_name=ele1.getProperty("BranchName").toString();
result.put(HOTEL_NAME, Hotel_name);
data.add(result);
}
}
catch(Exception e)
{
}
return data;
}
}
and just call it from where you need to fetch the result from the WCF web service like this
GetNearBy.connectToSOAPService();
try like this
final String NAMESPACE = con.getResources().getString(
R.string.NAMESPACE);
final String METHOD_NAME = "ItemsByCountry";
final String SOAP_ACTION = con.getResources().getString(
R.string.SOAP_ACTION)
+ METHOD_NAME;
final String URL = con.getResources().getString(R.string.URL);
SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);
Request.addProperty("userId", UserId);
Request.addProperty("countryName", countryName);
SoapSerializationEnvelope soapEnvelop;
soapEnvelop = new SoapSerializationEnvelope(SoapEnvelope.VER11);
soapEnvelop.dotNet = true;
soapEnvelop.setOutputSoapObject(Request);
HttpTransportSE htp = new HttpTransportSE(URL);
htp.call(SOAP_ACTION, soapEnvelop);
response = (SoapObject) soapEnvelop.getResponse();
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
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
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