Wednesday 13 April 2016

Retrofit in Android


Retrofit :  

           The retrofit library which is used for converting simple rest api calls to java interfaces.  In terms of performance, Retrofit wins over volley and other libraries. Retrofit is developed by   Square team.  Retrofit takes less time to implement and focus on the rest.


The below are simple steps to  start with Retrofit :


Add dependency in build.gradle :


compile ‘com.squareup.retrofit:retrofit:1.9.0’

You can see the below example with proper steps to achieve this :


  1. Create an Interface
  2. Create a Rest Adapter
  3. Implementation for interface RestService
  4. Consume Api calls



public class MyApi {

   public static final String BASE_URL = "https://api.github.com";
   //1.Create interface
   // 2.Create rest adapter
   // 3.Implemention for interface RestService
   //4.Consume APis


   //1.Create interface
   public interface RestService {

       //synchronous get method, dont run on UI thread.
       @GET("/users/{user}/repos")
       List<Repo> listRepos(@Path("user") String user);

       //Asynchronous get method, can run on UI thread.
       @GET("/users/{user}/repos")
       void listReposAsync(@Path("user") String user, Callback<List<Repo>> callback);
   }

   // 2.Create rest adapter
   private RestAdapter buildRestAdapter() {
       RestAdapter.Builder builder = new RestAdapter.Builder();
       builder.setEndpoint(BASE_URL);
       //displaying  logs in logcat with request and response
       //set NOne in producation builds
       builder.setLogLevel(RestAdapter.LogLevel.FULL);

       RestAdapter adapter = builder.build();
       adapter.create(RestService.class);
       return adapter;
   }

   //3.Implemention for interface RestService
   public RestService getRestService() {
       RestAdapter adapter = buildRestAdapter();
       RestService service = adapter.create(RestService.class);
       return service;
   }

   //4.Consume APis - Sync
   public List<Repo> getRepos(String gitUserName) {
       return getRestService().listRepos(gitUserName);
   }

   //4.Consume APis - Async
   public void getReposAsync(String gitUserName, Callback<List<Repo>> callback) {
       getRestService().listReposAsync(gitUserName, callback);
   }
}


Find the source code in github :

Tuesday 12 April 2016

What is Intent and Intent Filter in Android ?

Intent : 

    Intent is a way of telling to Android to perform a desired action. One components wants to invoke another to do a particular job, Any other components that are exists and it can handle through Intent-Filters by invoking Android platform to accomplish the job. This means components are not aware of each other's existence and still work together to give the desired result for the end user.


Basically Intents are two types:

1. Explicit Intent : This is used to call specific component. When you know which component you want to launch and you do not want to give the user free control over which component to use. For example, you have an application that has 2 activities. Activity A and activity B. You want to launch activity B from activity A. In this case you define an explicit intent targeting activity B and then use it to directly call it. See the below example :

   startActivity(new Intent(this, ActvityB.class));

// ActivityB should be added in manifest file with in application tag.
<activity android:name=".ActivityB"/>

2. Implicit Intent:
                         This is used when you have idea of what you want to do ,but you do not know which component should be launched. These Intents are send to the Android system, it searches for all components which are registered for the specific action and the data type.
  
Example:
      final String action = Intent.ACTION_VIEW;
      final Intent implicit = new Intent(action);
      implicit.setData(Uri.parse("http://www.google.com/"));
      startActivity(implicit);

Intent Filters:  
                     If an Intents is send to the Android system, it will determine suitable applications for this Intents. If several components have been registered for this type of Intents, Android offers the user the choice to open one of them.  An Intents is send to the Android system, it will determine suitable applications for this Intents. If several components have been registered for this type of Intents, Android offers the user the choice to open one of them

Intent Filter has action, data, category etc. Please check this more info