webservices on android eclipse - android

i am new to android/eclipse(java),in fact this forum too.
Has any one met this situation?
Get input from the user and display the appropriate result by consuming web service from android/eclipse.is there any examples available?
Thank you very much.

I am posting Link of tutorial
Here basic android tutorial to access web service in android basic ksoap android tutorial
and for creating webservice in java use this Tutorial How to create java based web service
Write a web service using java and Then by using ksoap library you will get response. Then Format that response according to your requirement. code for reference:
private static final String NAMESPACE = "http://tempuri.org/";
private static final String URL ="http://localhost/Web_Service.asmx?";// replace it by your Webservice URL
private static final String METHOD_NAME = "Function_Name";
private static final String SOAP_ACTION = "Soap Action";
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("parm_name",prm_value);// Parameter for Method
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
try
{
androidHttpTransport.call(SOAP_ACTION, envelope);//call the eb service Method
}
catch (Exception e)
{
e.printStackTrace();
}//Next task is to get Response and format that response
SoapObject response;
response= (SoapObject) envelope.bodyIn;

there are too may example for same Please Google ..................
this is only for initiation
Soap
http://android.vexedlogic.com/2011/04/17/android-lists-iv-accessing-and-consuming-a-soap-web-service-i/
http://seesharpgears.blogspot.in/2010/11/basic-ksoap-android-tutorial.html
Other
http://webhole.net/2011/11/15/android-tutorial-how-to-post-data-from-an-android-app-to-a-website/
http://apachejava.blogspot.in/2011/06/partial-http-post-show-data-webview.html
http://senior.ceng.metu.edu.tr/2009/praeda/2009/01/11/a-simple-restful-client-at-android/

Related

how to use ASP.NET api in android [duplicate]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 5 years ago.
Improve this question
I am having a lot of trouble finding good information on how to call a standard SOAP/WSDL web service with Android. All I've been able to find are either very convoluted documents and references to "kSoap2" and then some bit about parsing it all manually with SAX. OK, that's fine, but it's 2008, so I figured there should be some good library for calling standard web services.
The web service is just basically one created in NetBeans. I would like to have IDE support for generating the plumbing classes. I just need the easiest/most-elegant way to contact a WSDL based web service from an Android-based phone.
Android does not provide any sort of SOAP library. You can either write your own, or use something like kSOAP 2. As you note, others have been able to compile and use kSOAP2 in their own projects, but I haven't had to.
Google has shown, to date, little interest in adding a SOAP library to Android. My suspicion for this is that they'd rather support the current trends in Web Services toward REST-based services, and using JSON as a data encapsulation format. Or, using XMPP for messaging. But that is just conjecture.
XML-based web services are a slightly non-trivial task on Android at this time. Not knowing NetBeans, I can't speak to the tools available there, but I agree that a better library should be available. It is possible that the XmlPullParser will save you from using SAX, but I don't know much about that.
org.apache.http.impl.client.DefaultHttpClient comes in the Android SDK by default. That'll get you connected to the WSDL.
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpGet httpGet = new HttpGet("http://www.example.com/" + URL);
HttpResponse response = httpClient.execute(httpGet, localContext);
It's true that due to it's overhead SOAP is not the best choice for data exchange with mobile devices. However, you might find yourself in situation in which you do not control the format of server output.
So, if you have to stick with SOAP, there is a kSOAP2 library patched for Android here:
http://code.google.com/p/ksoap2-android/
To call a web service from a mobile device (especially on an Android phone), I have used a very simple way to do it. I have not used any web service client API in attempt to call the web service. My approach is as follows to make a call.
Create a simple HTTP connection by
using the Java standard API
HttpURLConnection.
Form a SOAP request. (You can make
help of SOAPUI to make a SOAP
request.)
Set doOutPut flag as true.
Set HTTP header values like
content-length, Content
type, and User-agent. Do not forget
to set Content-length value as it is a mandatory.
Write entire the SOAP request to the output stream.
Call the method to make a connection and
receive the response (In my case I used
getResonseCode).
If your received response code as
It means you are succeeded to call web service.
Now take an input stream on the same
HTTP connection and receive the
string object. This string object is
a SOAP response.
If the response code is other than
200 then take a ErrorInput stream on
same HTTPobject and receive the
error if any.
Parse the received response
using SAXParser (in my case) or
DOMParaser or any other parsing
mechanism.
I have implemented this procedure for the Android phone, and it is successfully running. I am able to parse the response even if it is more than 700 KB.
SOAP is an ill-suited technology for use on Android (or mobile devices in general) because of the processing/parsing overhead that's required. A REST services is a lighter weight solution and that's what I would suggest. Android comes with a SAX parser, and it's fairly trivial to use. If you are absolutely required to handle/parse SOAP on a mobile device then I feel sorry for you, the best advice I can offer is just not to use SOAP.
About a year ago I was reading this thread trying to figure out how to do SOAP calls on Android - the suggestions to build my own using HttpClient resulted in me building my own SOAP library for Android:
IceSoap
Basically it allows you to build up envelopes to send via a simple Java API, then automatically parses them into objects that you define via XPath... for example:
<Dictionary>
<Id></Id>
<Name></Name>
</Dictionary>
Becomes:
#XMLObject("//Dictionary")
public class Dictionary {
#XMLField("Id")
private String id;
#XMLField("Name")
private String name;
}
I was using it for my own project but I figured it might help some other people so I've spent some time separating it out and documenting it. I'd really love it if some of your poor souls who stumble on this thread while googling "SOAP Android" could give it a go and get some benefit.
DON'T FORGET TO ADD ksoap2.jar in your project and also add the
INTERNET permission in AndroidManifest file
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class WebserviceActivity extends Activity {
private static final String NAMESPACE = "https://api.authorize.net/soap/v1/";
private static final String URL ="https://apitest.authorize.net/soap/v1/Service.asmx?wsdl";
private static final String SOAP_ACTION = "https://api.authorize.net/soap/v1/AuthenticateTest";
private static final String METHOD_NAME = "AuthenticateTest";
private TextView lblResult;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lblResult = (TextView) findViewById(R.id.tv);
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("name","44vmMAYrhjfhj66fhJN");
request.addProperty("transactionKey","9MDQ7fghjghjh53H48k7e7n");
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
try {
androidHttpTransport.call(SOAP_ACTION, envelope);
//SoapPrimitive resultsRequestSOAP = (SoapPrimitive) envelope.getResponse();
// SoapPrimitive resultsRequestSOAP = (SoapPrimitive) envelope.getResponse();
SoapObject resultsRequestSOAP = (SoapObject) envelope.bodyIn;
lblResult.setText(resultsRequestSOAP.toString());
System.out.println("Response::"+resultsRequestSOAP.toString());
} catch (Exception e) {
System.out.println("Error"+e);
}
}
}
I had my tryst with KSOAP; I chose a rather simpler approach.
Given a WSDL file, create SOAP Request templates for each Request(for e.g.: using SOAP UI) and then substitute the values to be passed in code. POST this data to the service end point using DefaultHttpClient instance and get the response stream. Parse the Response Stream using an XML Pull parser.
You can have a look at WSClient++
I've created a new SOAP client for the Android platform. It is using a JAX-WS generated interface, but it is only a proof-of-concept so far.
If you are interested, please try the example and/or watch the source at AndroidSOAP.
If you can, go for JSON. Android comes with the complete org.json package
Call ksoap2 methods. It works very fine.
Set up the details, like
private static String mNAMESPACE=null;
private static String mURL=null;
public static Context context=null;
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(Request);
envelope.addMapping(mNAMESPACE, "UserCredentials",new UserCredendtials().getClass());
AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(mURL);
and then to get the result do
androidHttpTransport.call(SOAP_ACTION, envelope);
result = (SoapPrimitive)envelope.getResponse();
I hope Calling a web service from Android helps.
Few months ago I was working with jax-ws web service in j2ee application, There we were using CXF wsdl2java to generate WS client stub from the WSDL file and with those client stubs we consumed the web services. Few weeks ago, when I was trying to consume the web service in the same way in android platform I couldn't, because the android jar has not all the "jax-ws" supporting classes in it. That time I didn't find any such tool ( if I wasn't failed to google efficiently) to meet my requirement --
Get the client stub from the WSDL.
And call the Service with some argument (java business request
object).
Get the Response Business Object.
So, I developed my own Android SOAP Client Generation Tool. Where you have to follow these steps :
From WSDL Get WS Client Stub, Put it in your project.
Say for Some Service "ComplexOperationService", Instantiate the
Service, Get the Endpoint port and call the service method, and get the response from the Web service :
eg:
ComplexOperationService service = new ComplexOperationService( );
ComplexOperation port= service.getComplexOperationPort();
SomeComplexRequest request = --Get some complex request----;
SomeComplexResp resp = port.operate( request );
You don't need to care about the service class/req/response classes or any other classes and the method as well, as you know its all are generated from WSDL.
And of course you needn't be aware of the soap action/envelop/namespace etc. Just call the method as we, developers do all the time.
I am sure you could make a little SOAP client with Axis. Axis installation instructions.
I think Call SOAP Web Service from Android application will help you a lot.
Follow these steps by the method SOAP
From the WSDL file,
create SOAP Request templates for each Request.
Then substitute the values to be passed in code.
POST this data to the service end point using DefaultHttpClient instance.
Get the response stream and finally
Parse the Response Stream using an XML Pull parser.
If you can use JSON, there is a whitepaper, a video and the sample.code in Developing Application Services with PHP Servers and Android Phone Clients.
For me the easiest way is to use good tool to generate all required classes. Personally I use this site:
http://easywsdl.com/
It supports quite complex web services and uses ksoap2.
I would suggest checking out a very useful tool that helped me a lot. The guys
who take care of that project were very helpful, too.
www.wsdl2code.com/
If you are having problem regarding calling Web Service in android then
You can use below code to call the web service and get response. Make sure that your the web service return the response in Data Table Format..This code will help you if you using data from SQL Server database. If you using MYSQL you need to change one thing just replace word NewDataSet from sentence obj2=(SoapObject) obj1.getProperty("NewDataSet"); by DocumentElement
void callWebService(){
private static final String NAMESPACE = "http://tempuri.org/"; // for wsdl it may be package name i.e http://package_name
private static final String URL = "http://localhost/sample/services/MyService?wsdl";
// you can use IP address instead of localhost
private static final String METHOD_NAME = "Function_Name";
private static final String SOAP_ACTION = "urn:" + METHOD_NAME;
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("parm_name", prm_value);// Parameter for Method
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;// **If your Webservice in .net otherwise remove it**
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
try {
androidHttpTransport.call(SOAP_ACTION, envelope);// call the eb service
// Method
} catch (Exception e) {
e.printStackTrace();
}
// Next task is to get Response and format that response
SoapObject obj, obj1, obj2, obj3;
obj = (SoapObject) envelope.getResponse();
obj1 = (SoapObject) obj.getProperty("diffgram");
obj2 = (SoapObject) obj1.getProperty("NewDataSet");
for (int i = 0; i < obj2.getPropertyCount(); i++) {
// the method getPropertyCount() and return the number of rows
obj3 = (SoapObject) obj2.getProperty(i);
obj3.getProperty(0).toString();// value of column 1
obj3.getProperty(1).toString();// value of column 2
// like that you will get value from each column
}
}
If you have any problem regarding this you can write me..
This is a working example of consuming SOAP web services in android.
**Note ::***DON'T FORGET TO ADD ksoap2.jar in your project and also add the INTERNET permission in AndroidManifest file*
public final String WSDL_TARGET_NAMESPACE = "http://tempuri.org/";
public final String METHOD_NAME = "FahrenheitToCelsius";
public final String PROPERTY_NAME = "Fahrenheit";
public final String SOAP_ACTION = "http://tempuri.org/FahrenheitToCelsius";
public final String SOAP_ADDRESS = "http://www.w3schools.com/webservices/tempconvert.asmx";
private class TestAsynk extends AsyncTask<String, Void, String> {
#Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
Toast.makeText(getApplicationContext(),
String.format("%.2f", Float.parseFloat(result)),
Toast.LENGTH_SHORT).show();
}
#Override
protected String doInBackground(String... params) {
SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE,
METHOD_NAME);
request.addProperty(PROPERTY_NAME, params[0]);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(
SOAP_ADDRESS);
Object response = null;
try {
androidHttpTransport.call(SOAP_ACTION, envelope);
response = envelope.getResponse();
Log.e("Object response", response.toString());
} catch (Exception e) {
e.printStackTrace();
}
return response.toString();
}
}
Please download and add SOAP library file with your project
File Name : ksoap2-android-assembly-3.4.0-jar-with-dependencies
Clean the application and then start program
Here is the code for SOAP service call
String SOAP_ACTION = "YOUR_ACTION_NAME";
String METHOD_NAME = "YOUR_METHOD_NAME";
String NAMESPACE = "YOUR_NAME_SPACE";
String URL = "YOUR_URL";
SoapPrimitive resultString = null;
try {
SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);
addPropertyForSOAP(Request);
SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
soapEnvelope.dotNet = true;
soapEnvelope.setOutputSoapObject(Request);
HttpTransportSE transport = new HttpTransportSE(URL);
transport.call(SOAP_ACTION, soapEnvelope);
resultString = (SoapPrimitive) soapEnvelope.getResponse();
Log.i("SOAP Result", "Result Celsius: " + resultString);
} catch (Exception ex) {
Log.e("SOAP Result", "Error: " + ex.getMessage());
}
if(resultString != null) {
return resultString.toString();
}
else{
return "error";
}
The results may be JSONObject or JSONArray Or String
For your better reference,
https://trinitytuts.com/load-data-from-soap-web-service-in-android-application/
Thanks.
You may perform soap call as post over http with certain headers.
I solved this question without additional libraries like ksoap2
Here is live code getting orders from soap service
private static HashMap<String,String> mHeaders = new HashMap<>();
static {
mHeaders.put("Accept-Encoding","gzip,deflate");
mHeaders.put("Content-Type", "application/soap+xml");
mHeaders.put("Host", "35.15.85.55:8080");
mHeaders.put("Connection", "Keep-Alive");
mHeaders.put("User-Agent","AndroidApp");
mHeaders.put("Authorization","Basic Q2xpZW50NTkzMzppMjR3s2U="); // optional
}public final static InputStream receiveCurrentShipments(String stringUrlShipments)
{
int status=0;
String xmlstring= "<soap:Envelope xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\" xmlns:ser=\"http://35.15.85.55:8080/ServiceTransfer\">\n" +
" <soap:Header/>\n" +
" <soap:Body>\n" +
" <ser:GetAllOrdersOfShipment>\n" +
" <ser:CodeOfBranch></ser:CodeOfBranch>\n" +
" </ser:GetAllOrdersOfShipment>\n" +
" </soap:Body>\n" +
"</soap:Envelope>";
StringBuffer chaine = new StringBuffer("");
HttpURLConnection connection = null;
try {
URL url = new URL(stringUrlShipments);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("Content-Length", xmlstring.getBytes().length + "");
connection.setRequestProperty("SOAPAction", "http://35.15.85.55:8080/ServiceTransfer/GetAllOrdersOfShipment");
for(Map.Entry<String, String> entry : mHeaders.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
connection.setRequestProperty(key,value);
}
connection.setRequestMethod("POST");
connection.setDoInput(true);
OutputStream outputStream = connection.getOutputStream();
outputStream.write(xmlstring.getBytes("UTF-8"));
outputStream.close();
connection.connect();
status = connection.getResponseCode();
} catch (ProtocolException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
Log.i("HTTP Client", "HTTP status code : " + status);
}
InputStream inputStream = null;
try {
inputStream = connection.getInputStream();
} catch (IOException e) {
e.printStackTrace();
}
return inputStream;
}
To call a SOAP web Service from android , try to use this client
DON'T FORGET TO ADD ksoap2-android.jar in your java build path
public class WsClient {
private static final String SOAP_ACTION = "somme";
private static final String OPERATION_NAME = "somme";
private static final String WSDL_TARGET_NAMESPACE = "http://example.ws";
private static final String SOAP_ADDRESS = "http://192.168.1.2:8080/axis2/services/Calculatrice?wsdl";
public String caclculerSomme() {
String res = null;
SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE,
OPERATION_NAME);
request.addProperty("a", "5");
request.addProperty("b", "2");
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS);
try {
httpTransport.call(SOAP_ACTION, envelope);
String result = envelope.getResponse().toString();
res = result;
System.out.println("############# resull is :" + result);
} catch (Exception exception) {
System.out.println("########### ERRER" + exception.getMessage());
}
return res;
}
}
Add Soap Libaray(ksoap2-android-assembly-3.2.0-jar-with-dependencies.jar):
public static String Fn_Confirm_CollectMoney_Approval(
HashMap < String, String > str1,
HashMap < String, String > str2,
HashMap < String, String > str3) {
Object response = null;
String METHOD_NAME = "CollectMoney";
String NAMESPACE = "http://xxx/yyy/xxx";
String URL = "http://www.w3schools.com/webservices/tempconvert.asmx";
String SOAP_ACTION = "";
try {
SoapObject RequestParent = new SoapObject(NAMESPACE, METHOD_NAME);
SoapObject Request1 = new SoapObject(NAMESPACE, "req");
PropertyInfo pi = new PropertyInfo();
Set mapSet1 = (Set) str1.entrySet();
Iterator mapIterator1 = mapSet1.iterator();
while (mapIterator1.hasNext()) {
Map.Entry mapEntry = (Map.Entry) mapIterator1.next();
String keyValue = (String) mapEntry.getKey();
String value = (String) mapEntry.getValue();
pi = new PropertyInfo();
pi.setNamespace("java:com.xxx");
pi.setName(keyValue);
pi.setValue(value);
Request1.addProperty(pi);
}
mapSet1 = (Set) str3.entrySet();
mapIterator1 = mapSet1.iterator();
while (mapIterator1.hasNext()) {
Map.Entry mapEntry = (Map.Entry) mapIterator1.next();
// getKey Method of HashMap access a key of map
String keyValue = (String) mapEntry.getKey();
// getValue method returns corresponding key's value
String value = (String) mapEntry.getValue();
pi = new PropertyInfo();
pi.setNamespace("java:com.xxx");
pi.setName(keyValue);
pi.setValue(value);
Request1.addProperty(pi);
}
SoapObject HeaderRequest = new SoapObject(NAMESPACE, "XXX");
Set mapSet = (Set) str2.entrySet();
Iterator mapIterator = mapSet.iterator();
while (mapIterator.hasNext()) {
Map.Entry mapEntry = (Map.Entry) mapIterator.next();
// getKey Method of HashMap access a key of map
String keyValue = (String) mapEntry.getKey();
// getValue method returns corresponding key's value
String value = (String) mapEntry.getValue();
pi = new PropertyInfo();
pi.setNamespace("java:com.xxx");
pi.setName(keyValue);
pi.setValue(value);
HeaderRequest.addProperty(pi);
}
Request1.addSoapObject(HeaderRequest);
RequestParent.addSoapObject(Request1);
SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(
SoapEnvelope.VER10);
soapEnvelope.dotNet = false;
soapEnvelope.setOutputSoapObject(RequestParent);
HttpTransportSE transport = new HttpTransportSE(URL, 120000);
transport.debug = true;
transport.call(SOAP_ACTION, soapEnvelope);
response = (Object) soapEnvelope.getResponse();
int cols = ((SoapObject) response).getPropertyCount();
Object objectResponse = (Object) ((SoapObject) response)
.getProperty("Resp");
SoapObject subObject_Resp = (SoapObject) objectResponse;
modelObject = new ResposeXmlModel();
String MsgId = subObject_Resp.getProperty("MsgId").toString();
modelObject.setMsgId(MsgId);
String OrgId = subObject_Resp.getProperty("OrgId").toString();
modelObject.setOrgId(OrgId);
String ResCode = subObject_Resp.getProperty("ResCode").toString();
modelObject.setResCode(ResCode);
String ResDesc = subObject_Resp.getProperty("ResDesc").toString();
modelObject.setResDesc(ResDesc);
String TimeStamp = subObject_Resp.getProperty("TimeStamp")
.toString();
modelObject.setTimestamp(ResDesc);
return response.toString();
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}

Connect to a Delphi WebService with Android

I wrote a simple webservice in Delphi XE
It is running on IIS, as ISAPI Dll.
I cant connect to this webservice with Android:
private final String NAMESPACE = "http://217.114.221.83/grigliamo/";
private final String URL = "http://217.114.221.83/grigliamo/DifferenziaWS.dll/wsdl/IWSDifferenzia";
private final String SOAP_ACTION = "http://217.114.221.83/grigliamo/WS_Login";
private final String METHOD_NAME = "WS_Login";
public void getWebService(String x) {
//Create request
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
try {
//Involve web service
androidHttpTransport.call(SOAP_ACTION, envelope);
SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
Assign it to fahren static variable
Res = response.toString();
} catch (Exception e) {
Res = e.toString();
e.printStackTrace();
}
}
I always get an org.xmlpull.v1.XmlPullParserException: expectd START_TAG
Can anybody tell me what's wrong on my webservice please?
Thanks
Andrea
Nothing is wrong with the web service, SoapUI can retrieve the WSDL and send login requests.
I recommend to read ksoap2 org.xmlpull.v1.xmlpullparserexception expected start_tag error
The answers there contain step-by-step guides to build the correct SOAP request parameters / properties for a web service.

How to get data from web service in android?

I'm new in android development and I'm developing an application that can get data from web service. For example I created a login on my android application and the data that will going to input their is from the web service. How can I do it and what is the easiest way to do it using JSON or SOAP? Please provide some example so I'm going to have an idea how to do this thanks.
Here is a good example for this, im using something similar:
enter link description here
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";
public void getFahrenheit(String celsius) {
//Create request
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
//Property which holds input parameters
PropertyInfo celsiusPI = new PropertyInfo();
//Set Name
celsiusPI.setName("Celsius");
//Set Value
celsiusPI.setValue(celsius);
//Set dataType
celsiusPI.setType(double.class);
//Add the property to request object
request.addProperty(celsiusPI);
//Create envelope
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.dotNet = true;
//Set output SOAP object
envelope.setOutputSoapObject(request);
//Create HTTP call object
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
try {
//Invole web service
androidHttpTransport.call(SOAP_ACTION, envelope);
//Get the response
SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
//Assign it to fahren static variable
fahren = response.toString();
} catch (Exception e) {
e.printStackTrace();
}
}
For JSON check this tutorial.
For SOAP check this.
Json is the most simple and clean communication protocol. I would suggest using a library for getting the data https://github.com/kodart/Httpzoid
There are some usage samples.

How can I add Custom [call back] string in android request?

How can i add Custom [call back] string in android request?
Below is my code for call soap webservice in android and get a json response. How can i pass call back=?callback=jQuery15 in android?
Any idea about the same how can I include a callback-jquery15 parameters in url?
private static String SOAP_ACTION1 = "http://www.example.com/NewsServices/AuthenticateApplication";
private static String NAMESPACE = "http://www.example.com/NewsServices/";
private static String METHOD_NAME1 = "AuthenticateApplication";
private static String URL = "http://www.example.com/services/webServices/MobileServices/exampleMobilejson.asmx";
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME1);
request.addProperty("xyz", "xyzaa");
request.addProperty("abc", "abcxx");
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
envelope.dotNet = true;
try {
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.call(SOAP_ACTION1, envelope);
SoapObject result = (SoapObject) envelope.bodyIn;
Toast.makeText(getApplicationContext(), result.toString(),
Toast.LENGTH_LONG).show();
txtCel.setText(result.getProperty(0).toString());
but my problem is my webservice accept parameters with call back how can i use for ex:
http://www.example.com/services/webServices/MobileServices/exampleMobileJson.asmx/AuthenticateApplication?callback=jQuery15&xyz=xyzaa&abc=abcxx
Callback is normally used for JSONP. When you do an AJAX request in the browser to a webserver which is in a other domain. It will be blocked due 'same origin policy'. With JSONP you can circumvent this be adding a callback parameter. This parameter is a function call defined in your webpage.
About adding an extra parameter:
request.addProperty("callback", "jQuery15");
Like you already used for 'xyz' and 'abc'.

Using Android 4.0.3 to access a webservice using ksoap 2.6.0

I am trying to get my Android phone to access the IDEOne (www.ideone.com) web service so I can use its APIs for this project I'm working on. the webservice can be accessed using kSoap so I'm using kSoap 2.6.0 which I got from the Google website. My code throws and exception on the following line of code:
httpTransport.call(SOAP_ACTION, envelope); //send request
I checked my wireless connection and used other wireless connection to make sure it was not a firewall problem. I checked the connectionon the Android emulator and there was a connection. I simply ran code others had posted online about connecting Android to web services via kSoap (http://www.helloandroid.com/tutorials/using-ksoap2-android-and-parsing-output-data), with no luck. It throws and exception on the same call function. Any suggestions as to why? Below is my code. Thank you in advance for your help.
public class IDEOneStubI {
private static final String METHOD_NAME = "createSubmission";
private static final String SOAP_ACTION = "http://ideone.com/api/1/service#createSubmission";
private static final String NAMESPACE = "http://ideone.com/api/1/serviceL";
private static final String URL = "http://ideone.com/api/1/service;";
//you can get these values from the wsdl file^
public SoapObject soap(String METHOD_NAME, String SOAP_ACTION, String NAMESPACE, String URL) throws IOException, XmlPullParserException {
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); //set up request
request.addProperty("user", "nsbradley"); //variable name, value. I got the variable name, from the wsdl file!
request.addProperty("pass", "12345678");
request.addProperty("sourceCode", "Hello World");
request.addProperty("language", 10);
request.addProperty("input", "");
request.addProperty("run", Boolean.TRUE);
request.addProperty("private", Boolean.FALSE);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); //put all required data into a soap envelope
envelope.dotNet = true;
envelope.setOutputSoapObject(request); //prepare request
HttpTransportSE httpTransport = new HttpTransportSE(URL);
httpTransport.debug = true; //this is optional, use it if you don't want to use a packet sniffer to check what the sent message was
httpTransport.call(SOAP_ACTION, envelope); //send request
SoapObject result=(SoapObject)envelope.getResponse(); //get response
return result;
}
I think your URL string is wrong, remove the semicolon at the end and try again.
Since Android 3.0, you must call this kind of methods from an async task.
First of all make sure that you add permision to internet in your manifest file
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
if that doesnt work try changing
SoapObject result=(SoapObject)envelope.getResponse(); //get response
to
SoapObject result=(SoapObject)envelope.bodyIn; //get response
And also you should put the webservice calls in a try catch block, or throw a SoapFault exception.

Categories

Resources