
Live Demo
Follow the below steps to consume RestFul in android. In this tutorial i will use the restFul webservices to validate login credentials. If user is valid it will go to the welcome page else it will be in same page.
Step1
Create a android blank project, if you don’t know how to create blank project in android check my previous post.
Step2
Need internet access permission. write the below code in AndroidManifest.xml file.
Step3
Need RestFul url which is running in another server or same machine.
Step4
Make a RestFul Client which will extends AsyncTask. See RestFulPost.java.
Step5
Make DTO class for RestFull Request data and RestFul Response.
How it works?
User provide the user id and password and click in login button(activity_main.xml) -> it sends post request to restFul webservices and set the response to DTO class. Then we set the data to UI from DTO.

AndroidManifest.xml
MainActivity.java
package javaant.com.consuming_restful; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.graphics.Color; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import com.google.gson.Gson; import java.io.IOException; import java.util.HashMap; import java.util.Map; import javaant.com.consuming_restful.dto.LoginRequest; import javaant.com.consuming_restful.dto.LoginResponse; import javaant.com.consuming_restful.dto.LoginResponseData; import javaant.com.consuming_restful.restclient.RestFulPost; import javaant.com.consuming_restful.restclient.RestFulResult; import javaant.com.consuming_restful.util.Util; public class MainActivity extends Activity implements RestFulResult, View.OnClickListener { EditText userId = null; EditText password = null; Button logingButton = null; Context appContext; TextView msg=null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); appContext = this; userId = (EditText) findViewById(R.id.txtUserIdLogin); password = (EditText) findViewById(R.id.txtPassword); logingButton = (Button) findViewById(R.id.btnLogin); msg= (TextView) findViewById(R.id.txtmsg); logingButton.setOnClickListener(this); } @Override public void onClick(View v) { LoginRequest loginRequest = new LoginRequest(); loginRequest.setUserId(userId.getText().toString()); loginRequest.setPassword(password.getText().toString()); msg.setText(""); MaploginMap = new HashMap (); try { loginMap.put("url", Util.getProperty("login_url", appContext)); } catch (IOException e) { e.printStackTrace(); } loginMap.put("data", loginRequest); RestFulPost restFulPost = new RestFulPost(this, appContext, "Please Wait", "Login"); restFulPost.execute(loginMap); } @Override public void onResfulResponse(String result, String responseFor) { Gson gson = new Gson(); LoginResponse response = gson.fromJson(result, LoginResponse.class); LoginResponseData data=response.getData(); if(data.getValidUser().equalsIgnoreCase("yes")){ msg.setText("Valid user id and password"); msg.setTextColor(Color.GREEN); Intent welcome = new Intent(this, welcome.class); startActivity(welcome); } else{ msg.setText("Invalid User id and password"); msg.setTextColor(Color.RED); } Log.d("login", "login resonse " + result); } }
RestFulPost.java
package javaant.com.consuming_restful.restclient; import android.app.ProgressDialog; import android.content.Context; import android.os.AsyncTask; import android.util.Log; import com.google.gson.Gson; import org.apache.http.HttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.util.EntityUtils; import java.util.Map; import javaant.com.consuming_restful.util.Util; /** * Created by Nirmal Dhara on 29-10-2015. */ public class RestFulPost extends AsyncTask
Login Request json
LoginRequest{ "userId":"yourid", "password":"yourpassword" }
LoginRequest.java to map the json request
package javaant.com.consuming_restful.dto; /** * Created by Nirmal Dhara on 29-10-2015. */ public class LoginRequest { private String userId; private String password; public String getUserId() { return userId; } public void setUserId(String userId) { this.userId = userId; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
Login Response json
{ "data": { "userId":"2", "image":"http://javamad.com/users/2.jpg", "userName":"Nirmal Dhara", "ValidUser":"yes", "code":"200", "Email_id":"6PYdh2NJEI4YzoQwzZiH3e4aT1OU56loU8iG3GtyUj8=" }, "status":"success" }
LoginResponse.java to map the json response
package javaant.com.consuming_restful.dto; /** * Created by Nirmal Dhara on 29-10-2015. */ public class LoginResponse { private String status; private LoginResponseData data; LoginResponse() { } public String getStatus() { return status; } public void setStatus(String status) { this.status = status; } public LoginResponseData getData() { return data; } public void setData(LoginResponseData data) { this.data = data; } }
Note-
We can use the RestFulPost.java (Rest client to post the data to RestFul webservices) in others activity for RestFul post only we need to change the rest url and data(Json request mapping class).

Leave a Reply