Andriod For Beginner

Web Services

 What is Web Services?

Web service is a technology to communicate one programming language with another. For example, java programming language can interact with PHP and .Net by using web services. In other words, web service provides a way to achieve interoperability.

Web Service can be defined by following ways:

  • It is a client server application or application component for communication.
  • The method of communication between many devices over network.
  • It is a software system for interoperable machine to machine communication.
  • It is a collection of standards or protocols for exchanging information  between two devices or application.

 

Types 

There different types of web services are used by the android developers most commonly using web services are

  • Restful web service
  • Async web service
  • Json
  • retrofit

 

Normally the beginners will prefer to json and restfull web service to start their career .Here we are going learn about web service in simple login page design and functionality behind it.Before That let’s see some more things about web services.

Creating web services application in android is not a difficult task. We can easily create a restful web services application in android

Needs of using Web Services

Existing Web applications are in a need of creating mobile applications to show their presence in mobile platform as well. Almost all web applications are having their Mobile applications created in Android, iOS or Windows platform.

Exposing the existing functionalities of the applications is bit tough as all the functionalities have to re-written in the respective platforms.But it can be easily achieved with much ease by creating Web Service and expose the existing functionalities as web methods to Mobile platforms.

Advantages of using Web services

The main advantage over a regular HTTP application is that it uses SOAP messaging which is much more efficient and less bandwidth costly.

Another advantage is that Service Transport, XML Messaging, Service Description and Service Discovery layers use a standard protocol which makes it easy for developers to develop functionality irrespective of programming languages.

Suppose you are a provider of a service, say weather forecast. Why make developers, go to your WEBSITE, SCRAP your page and then extract the data that they need? Wouldn’t it be easier to develop this by WEB SERVICES that a developer can pass in a date and get back a well defined XML document containing all the info they need? The same happens for all type of cases…

Now i will explain the RESTFUL Web services and SOAP Web services Advantages

Advantage of RESTFUL Web Services

1. You have a set of resources that you want to manipulate.

2. You want to support navigation between resources.

3. You need scalability.

4. You want to improve performance by caching web service request results at some point between the service and the consumer.

5. Caching on the URI of a service is very easy.

Advantage of SOAP Web Services

1. You want to publish a web service description (using WSDL).

2. You want to use security etc. that relies on the use of SOAP headers or some similar mechanism in which data is added and removed from a request.

3. You want better tooling support.

4. You want tested platform interoperability.

Now lets see some example with Web Services in Android application. So that you can get more in-details about web services.

  • Create android project
  • Design Screens and Java Functionality

 

Create android project

  • Create new android project [ File >> New >> Android Application Project ] with project name “Testwebservices”
  • Enter package name as ‘com.test.example’
  • ChooseMinimum Required SDK, target SDK version for your project
  • Click Next button and finally click ‘Finish’ to create project

 

Design screens and Java functionality

activity_main.xml

<RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android”  

    xmlns:tools=”http://schemas.android.com/tools”  

    android:layout_width=”match_parent”  

    android:layout_height=”match_parent”  

     tools:context=”.MainActivity” >  

 

    <EditText  

        android:id=”@+id/editText1″  

        android:layout_width=”wrap_content”  

         android:layout_height=”wrap_content”  

         android:layout_alignParentTop=”true”  

        android:layout_centerHorizontal=”true”  

         android:hint=”Username” >  

      </EditText>  

 

    <EditText  

        android:id=”@+id/editText2″  

         android:layout_width=”wrap_content”  

         android:layout_height=”wrap_content”  

         android:layout_alignLeft=”@+id/editText1″  

         android:layout_below=”@+id/editText1″  

         android:layout_marginTop=”67dp”

         android:hint=”Password”  

         android:inputType=”textPassword” />  

 

    <ProgressBar  

        android:id=”@+id/progressBar1″ 

        android:layout_width=”wrap_content”  

        android:layout_height=”wrap_content”  

        android:layout_alignLeft=”@+id/button1″  

        android:layout_below=”@+id/editText2″  

        android:layout_marginTop=”22dp” />  

 

    <Button  

        android:id=”@+id/button1″  

        android:layout_width=”wrap_content”  

        android:layout_height=”wrap_content”  

        android:layout_alignLeft=”@+id/editText2″  

        android:layout_below=”@+id/progressBar1″  

        android:layout_marginLeft=”22dp”  

        android:text=”Login” />  

</RelativeLayout>

MainActivity.java

package com.example.newrestapi;

import java.io.BufferedReader;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.util.ArrayList;

import java.util.List;

import org.apache.http.HttpEntity;

import org.apache.http.HttpResponse;

import org.apache.http.NameValuePair;

import org.apache.http.client.HttpClient;

import org.apache.http.client.entity.UrlEncodedFormEntity;

import org.apache.http.client.methods.HttpPost;

import org.apache.http.impl.client.DefaultHttpClient;

import org.apache.http.message.BasicNameValuePair;

import android.os.AsyncTask;

import android.os.Bundle;

import android.app.Activity;

import android.content.Intent;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.EditText;

import android.widget.ProgressBar;

import android.widget.Toast;

public class MainActivity extends Activity {

EditText password,userName;

Button login;

ProgressBar progressBar;

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

password=(EditText) findViewById(R.id.editText2);

userName=(EditText) findViewById(R.id.editText1);

login=(Button) findViewById(R.id.button1);

//progess_msz.setVisibility(View.GONE);

progressBar=(ProgressBar) findViewById(R.id.progressBar1);

progressBar.setVisibility(View.GONE);

login.setOnClickListener(new OnClickListener() {

public void onClick(View v) {

progressBar.setVisibility(View.VISIBLE);

String s1=userName.getText().toString();

String s2=password.getText().toString();

new ExecuteTask().execute(s1,s2);

}

});

}

class ExecuteTask extends AsyncTask<String, Integer, String>

{

@Override

protected String doInBackground(String… params) {

String res=PostData(params);

return res;

}

@Override

protected void onPostExecute(String result) {

progressBar.setVisibility(View.GONE);

//progess_msz.setVisibility(View.GONE);

Toast.makeText(getApplicationContext(), result, 3000).show();

}

}

public String PostData(String[] valuse) {

String s=””;

try

{

HttpClient httpClient=new DefaultHttpClient();

HttpPost httpPost=new HttpPost(“http://url/login”);

List<NameValuePair> list=new ArrayList<NameValuePair>();

list.add(new BasicNameValuePair(“name”, valuse[0]));

list.add(new BasicNameValuePair(“pass”,valuse[1]));

httpPost.setEntity(new UrlEncodedFormEntity(list));

HttpResponse httpResponse= httpClient.execute(httpPost);

HttpEntity httpEntity=httpResponse.getEntity();

s= readResponse(httpResponse);

}

catch(Exception exception) {}

return s;

}

public String readResponse(HttpResponse res) {

InputStream is=null;

String return_text=””;

try {

is=res.getEntity().getContent();

BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(is));

String line=””;

StringBuffer sb=new StringBuffer();

while ((line=bufferedReader.readLine())!=null)

{

sb.append(line);

}

return_text=sb.toString();

} catch (Exception e)

{

}

return return_text;

}

}

 

In above code we call the web services URL for login page.Change the URL which web services call you need to do :

HttpPost httpPost=new HttpPost(“URL”);

If you not clear with these coding , its ok !.In upcoming chapters we have some demo application. So that you can Understand well about other syntax and coding

Please login to get access to the quiz
Android Programming (Prev Lesson)
(Next Lesson) App Store Process
Back to Andriod For Beginner
Inquire Now
close slider