Android Restful Web Service Client Example

In this tutorial you learn how to create an android restful web service client to consume a restful web service developed in Java.

Before making the android client make sure the web service is already running on server. I am using apache tomcat server to run the java web service in my local system.

If you don’t know how to make Java restful web service then read below tutorial.

Also Read: Create Simple Java RESTful Web Services Using Jersey

In this example the android client will send some string to server which is reversed and sent back to the android client.

 

Android Restful Web Service Client Example

1. Create a new android project with package name com.androidrestfullwebservice

2. I am using Volley library to load data from server. If you don’t know how to use this library then read below tutorial.

Also Read: Android Volley Tutorial With Example

3. Just copy and paste below line in build.gradle (Module) file to add dependency for Volley library. After adding Sync the project.

compile 'com.android.volley:volley:1.0.0'

 

4. As we are doing network related work so add internet access permission in AndroidMainifest.xml file by adding following line.

<uses-permission android:name="android.permission.INTERNET"/>

 

5. Create an activity and add following code in respective files.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.androidrestfullwebservice.MainActivity"
    android:orientation="vertical">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/editText"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Fetch"
        android:id="@+id/btn"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/text"/>
</LinearLayout>

 

MainActivity.java

package com.androidrestfullwebservice;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

public class MainActivity extends AppCompatActivity {
    EditText editText;
    TextView text;
    Button btn;
    String url ="http://192.168.1.3/JavaRESTfullWS/rest/DemoService/";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        editText = (EditText)findViewById(R.id.editText);
        text = (TextView)findViewById(R.id.text);
        btn = (Button)findViewById(R.id.btn);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                StringRequest request = new StringRequest(Request.Method.GET, url+editText.getText().toString(), new Response.Listener<String>(){
                    @Override
                    public void onResponse(String s) {
                        text.setText(s);
                    }
                },new Response.ErrorListener(){
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                        text.setText("Some error occurred!!");
                    }
                });

                RequestQueue rQueue = Volley.newRequestQueue(MainActivity.this);
                rQueue.add(request);
            }
        });
    }
}

 

Note: Make sure to replace 192.168.1.3 with your system IPv4. You can find it by running ipconfig command in command prompt.

 

6. Now run the project.

 

Screenshot

Android Restful Web Service Client Example

Comment below if you are facing any difficulty to make android restful web service client.

Leave a Comment

Your email address will not be published. Required fields are marked *