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.
Related
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 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'.
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/
I would like to learn how to use webSservices with Android.
At the moment, I have a server with the wsdl file and I am able to call a desired Webservice from.
My problem is that I got the response from the server and I would like to extract some informations which are interessant for me, for example the user id or something like that.
Here is my code:
public class TestBookSoapActivity extends Activity {
private static final String SOAP_ACTION = "JournalArticles";
private static final String METHOD_NAME = "JournalArticles";
private static final String NAMESPACE = "http://api.mdpi.com/ws/";
private static final String URL = "http://api.mdpi.com/ws/server.php";
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
executeAppelSOAP();
}
private void executeAppelSOAP() {
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("JournalID", 1);
request.addProperty("Volume", 12);
request.addProperty("Issue", 1);
SoapSerializationEnvelope enveloppe = new SoapSerializationEnvelope(SoapEnvelope.VER11);
enveloppe.setOutputSoapObject(request);
AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport (URL);
try {
androidHttpTransport.call(SOAP_ACTION, enveloppe);
Object resultat = enveloppe.getResponse();
System.out.println(" Journal Articles= " + resultat.toString());
} catch(Exception e) {
e.printStackTrace();
System.out.println("Problem");
}
}
Could I use the enveloppe.parse() method and if so, how?
Thank you.
There are two steps you should take:
Get the correct XML value
Parse XML
About 1...
I prefer HttpTransportSE so instead of:
AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport (URL);
use:
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
Then instead of:
Object resultat = enveloppe.getResponse();
use:
SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
String xml = response.toString();
You can also check with the debugger how response looks like. More details you can read in answers to this question.
About 2...
You can use any XML parser. I prefer SAX for performance reasons. Here is an Android example: how to parse XML in Android
Regards,
Dragan
I want to use ksoap2 as SOAP web service for Android.
I use http://www.helloandroid.com/tutorials/using-ksoap2-android-and-parsing-output-data this example to begin with ksoap2. When import latest file and paste this code
private static final String SOAP_ACTION = "http://footballpool.dataaccess.eu/data/TopGoalScorers";
private static final String NAMESPACE = "http://footballpool.dataaccess.eu";
private static final String URL = "http://footballpool.dataaccess.eu/data/info.wso?WSDL";
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("iTopN", "5"); //variable name, value. I got the variable name, from the wsdl file!
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); //put all required data into a soap envelope
envelope.setOutputSoapObject(request); //prepare request
AndroidHttpTransport httpTransport = new AndroidHttpTransport(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.requestDump)
httpTransport.call(SOAP_ACTION, envelope); //send request
SoapObject result=(SoapObject)envelope.getResponse(); //get response
return result;
}
call
SoapObject result=soap(METHOD_NAME, SOAP_ACTION, NAMESPACE, URL);
I receive this error (example display error):
The type AndroidHttpTransport is deprecated
Can some one tell me where I'm wrong??
Thanks
The tutorial you followed is outdated. Use HttpTransportSE in place of AndroidHttpTransport.