Thursday 22 June 2017

Android Drag & Drop


STEP - 1 : values --> attr.xml

<?xml version="1.0" encoding="utf-8"?><resources>    <declare-styleable name="RearrangeableLayout">        <attr name="outlineWidth" format="dimension" />        <attr name="outlineColor" format="color" />        <attr name="selectionAlpha" format="float" />        <attr name="selectionZoom" format="float" />    </declare-styleable></resources>


STEP - 2 : activity_main.xml

<?xml version="1.0" encoding="utf-8"?><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"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    >        <propfinsolutions.realstate.app.com.testfifth.RearrangeableLayout        xmlns:android="http://schemas.android.com/apk/res/android"        xmlns:app="http://schemas.android.com/apk/res-auto"        android:id="@+id/rearrangeable_layout"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:clipToPadding="false"        app:outlineWidth="2dp"        app:outlineColor="#00FFFF"        app:selectionAlpha="0.5"        app:selectionZoom="1.2">


        <!-- add child views with `android:id` attr to       save position during orientation change -->

        <TextView            android:id="@+id/texview"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="Sample Demo"            android:textSize="30sp"            android:background="@android:color/darker_gray"            android:layout_margin="15dp" />
        <TextView            android:id="@+id/textview2"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="Sample Demo with very large text that will overflow in width"            android:textSize="30sp"            android:background="@android:color/holo_green_light"/>
        <Button            android:id="@+id/button"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="Button"            android:textSize="30sp"            android:layout_margin="15dp"/>
        <TextView            android:id="@+id/textview3"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="Sample"            android:textSize="15sp"            android:background="@android:color/holo_orange_light"            android:layout_margin="15dp"/>
    </propfinsolutions.realstate.app.com.testfifth.RearrangeableLayout></RelativeLayout>


STEP - 3: MainActivity.java


import android.graphics.Rect;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.util.Log;import android.view.View;import android.view.ViewTreeObserver;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";    private RearrangeableLayout root;
    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);
        // initialize the RearrangeableLayout object        root = (RearrangeableLayout) findViewById(R.id.rearrangeable_layout);
        // callback method to call childPositionListener() method        childPosiitonListener();
        // callback method to call preDrawListener() method        preDrawListener();
    }

    /**     *  In this method, Added a ChildPositionListener to the root layout to receive     *  position of child view whenever any child view is dragged     */    public void childPosiitonListener(){

        root.setChildPositionListener(new RearrangeableLayout.ChildPositionListener() {
            @Override            public void onChildMoved(View childView, Rect oldPosition, Rect newPosition) {
                Log.e(TAG, childView.toString());                Log.e(TAG, oldPosition.toString() + " -> " + newPosition.toString());            }
        });    }

    /**     *  In this method, Added a PreviewListener to the root layout to receive update during     *  child view is dragging     */    public void preDrawListener(){
        root.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            @Override            public boolean onPreDraw() {
                Log.e(TAG, "onPrepreview");                Log.e(TAG, root.toString());                return true;            }
        });    }
}


STEP - 4 : RearrangeableLayout.java

import android.content.Context;import android.content.res.TypedArray;import android.graphics.Bitmap;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.ColorMatrix;import android.graphics.ColorMatrixColorFilter;import android.graphics.Paint;import android.graphics.PointF;import android.graphics.Rect;import android.os.Parcelable;import android.util.AttributeSet;import android.util.Log;import android.util.SparseArray;import android.view.MotionEvent;import android.view.View;import android.view.ViewGroup;import android.view.animation.AccelerateInterpolator;import android.view.animation.Animation;import android.view.animation.LayoutAnimationController;import android.view.animation.TranslateAnimation;import android.widget.Toast;
/** * Created by raja on 6/19/15. */public class RearrangeableLayout extends ViewGroup {
    private static final String TAG = "RearrangeableLayout";
    private PointF mStartTouch;    private View mSelectedChild;    private float mSelectionZoom;    private Paint mSelectionPaint;    private Paint mOutlinePaint;    private SparseArray<Parcelable> mContainer;
    /* callback to update clients whenever child is dragged */    private ChildPositionListener mListener;
    /* used by ChildPositionListener callback */    private Rect mChildStartRect;    private Rect mChildEndRect;
    public RearrangeableLayout(Context context) {
        this(context, null);    }

    public RearrangeableLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);    }

    public RearrangeableLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);        init(context, attrs);    }

    private void init(Context context, AttributeSet attrs) {
        mStartTouch = null;        mSelectedChild = null;        mContainer = new SparseArray<Parcelable>(5);        mListener = null;        mChildStartRect = null;        mChildEndRect = null;
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.RearrangeableLayout);        float strokeWidth = a.getDimension(R.styleable.RearrangeableLayout_outlineWidth, 2.0f);        int color = a.getColor(R.styleable.RearrangeableLayout_outlineColor, Color.GRAY);        float alpha = a.getFloat(R.styleable.RearrangeableLayout_selectionAlpha, 0.5f);        mSelectionZoom = a.getFloat(R.styleable.RearrangeableLayout_selectionZoom, 1.2f);        a.recycle();
        float filter[] = new float[] {
                1f, 0f, 0f, 0f, 0f,                0f, 1f, 0f, 0f, 0f,                0f, 0f, 1f, 0f, 0f,                0f, 0f, 0f, alpha, 0f        };        ColorMatrixColorFilter colorFilter = new ColorMatrixColorFilter(new ColorMatrix(filter));
        mOutlinePaint = new Paint(Paint.ANTI_ALIAS_FLAG);        mOutlinePaint.setStyle(Paint.Style.STROKE);        mOutlinePaint.setStrokeWidth(strokeWidth);        mOutlinePaint.setColor(color);        mOutlinePaint.setColorFilter(colorFilter);
        mSelectionPaint = new Paint();        mSelectionPaint.setColorFilter(colorFilter);
        Animation trans = new TranslateAnimation(Animation.ABSOLUTE, 0, Animation.ABSOLUTE, 0,                Animation.RELATIVE_TO_PARENT, 1, Animation.RELATIVE_TO_PARENT, 0);        trans.setDuration(500);        trans.setInterpolator(new AccelerateInterpolator(1.0f));
        LayoutAnimationController c = new LayoutAnimationController(trans, 0.25f);        setLayoutAnimation(c);    }

    @Override    protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
        return super.checkLayoutParams(p) && p instanceof LayoutParams;    }

    @Override    public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
        return new LayoutParams(getContext(), attrs);    }

    @Override    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int width = MeasureSpec.getSize(widthMeasureSpec);        int height = MeasureSpec.getSize(heightMeasureSpec);
        width = Math.max(width, getMinimumWidth());        height = Math.max(height, getMinimumHeight());
        //Log.d(TAG, String.format("onMeasure: (%d, %d)", width, height));        //measureChildren(widthMeasureSpec, heightMeasureSpec);
        for(int i=0; i<getChildCount(); i++) {
            View view = getChildAt(i);            LayoutParams mp = (LayoutParams) view.getLayoutParams();            view.measure(MeasureSpec.makeMeasureSpec(width -mp.leftMargin -mp.rightMargin, MeasureSpec.AT_MOST),                    MeasureSpec.makeMeasureSpec(height -mp.topMargin -mp.bottomMargin, MeasureSpec.AT_MOST));
            //int w = view.getMeasuredWidth();            //int h = view.getMeasuredHeight();            //Log.d(TAG, String.format("View #%d: (%d, %d)", i, w, h));        }
        setMeasuredDimension(width, height);    }

    @Override    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        if (mSelectedChild == null) {
            doInitialLayout(l, t, r, b, getChildCount());        }
    }

    private void doInitialLayout(int l, int t, int r, int b, int count) {
        int currentLeft = l;        int currentTop = t;        int prevChildBottom = -1;        for (int i=0; i<count; i++) {
            View view = getChildAt(i);            LayoutParams mp = (LayoutParams) view.getLayoutParams();            int width = view.getMeasuredWidth();            int height = view.getMeasuredHeight();            int left, top, right, bottom;
            if (view.getVisibility() != View.GONE && !mp.moved) {
                if (currentTop+height > b || l+width > r) {
                    Toast.makeText(getContext(), "Couldn't fit a child View, skipping it", Toast.LENGTH_SHORT)
                            .show();                    Log.d(TAG, "Couldn't fit a child View, skipping it");                    continue;                }
                if (currentLeft+width > r) {
                    left = l + mp.leftMargin;                    currentTop = prevChildBottom;                } else {
                    left = currentLeft + mp.topMargin;                }
                top = currentTop + mp.topMargin;                right = left + width;                bottom = top + height;                //Log.d(TAG, String.format("Layout #%d: (%d, %d, %d, %d)", i, left, top, right, bottom));                mp.left = left;                mp.top = top;                view.layout(left, top, right, bottom);
                currentLeft = right + mp.rightMargin;                prevChildBottom = bottom + mp.bottomMargin;            }
            else if (mp.moved && view != mSelectedChild) {
                int x1 = Math.round(mp.left);                int y1 = Math.round(mp.top);                int x2 = Math.round(mp.left) + width;                int y2 = Math.round(mp.top) + height;                view.layout(x1, y1, x2, y2);            }
        }
    }

    /**     * this method can be used to force layout on a child     * to recalculate its hit-rect,     * otherwise outline border of the selected child is     * drawn at the old position     */    private void layoutSelectedChild(LayoutParams lp) {
        int l = Math.round(lp.left);        int t = Math.round(lp.top);        int r = l + mSelectedChild.getMeasuredWidth();        int b = t + mSelectedChild.getMeasuredHeight();
        lp.moved = true;        mSelectedChild.layout(l, t, r, b);    }

    @Override    protected void dispatchDraw(Canvas canvas) {
        if (mSelectedChild != null) {
            mSelectedChild.setVisibility(View.INVISIBLE);        }
        super.dispatchDraw(canvas);
        if (mSelectedChild != null) {
            Rect rect = new Rect();            mSelectedChild.getHitRect(rect);
            int restorePoint = canvas.save();            canvas.scale(mSelectionZoom, mSelectionZoom, rect.centerX(), rect.centerY());            canvas.drawRect(rect, mOutlinePaint);
            mSelectedChild.setDrawingCacheEnabled(true);            Bitmap child = mSelectedChild.getDrawingCache();            if (child != null) {
                LayoutParams lp = (LayoutParams) mSelectedChild.getLayoutParams();                canvas.drawBitmap(child, lp.left, lp.top, mSelectionPaint);            } else {
                Log.d(TAG, "drawingCache not found! Maybe because of hardware acceleration");                mSelectedChild.draw(canvas);            }
            canvas.restoreToCount(restorePoint);        }
    }

    @Override    public boolean onInterceptTouchEvent(MotionEvent ev) {
        float x = ev.getX();        float y= ev.getY();        if (ev.getActionMasked() == MotionEvent.ACTION_MOVE) {
            prepareTouch(x, y);            return true;        }
        return false;    }

    @Override    public boolean onTouchEvent(MotionEvent event) {
        float x = event.getX();        float y = event.getY();        //mViewDragHelper.processTouchEvent(event);
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                prepareTouch(x, y);                break;            case MotionEvent.ACTION_MOVE:
                if (mSelectedChild != null && mStartTouch != null) {
                    LayoutParams lp = (LayoutParams) mSelectedChild.getLayoutParams();                    float dx = x - mStartTouch.x;                    float dy = y - mStartTouch.y;
                    lp.left = lp.initial.x + dx;                    if (lp.left < 0.0f) {
                        lp.left = 0.0f;                    }

                    lp.top = lp.initial.y + dy;                    if (lp.top < 0.0f) {
                        lp.top = 0.0f;                    }

                    /* layout child otherwise hit-rect is not recalculated */                    layoutSelectedChild(lp);                    invalidate();                }
                break;            case MotionEvent.ACTION_CANCEL:
            case MotionEvent.ACTION_UP:
            default:
                if (mSelectedChild != null) {
                    if (mListener != null && mChildStartRect != null) {
                        mChildEndRect = new Rect();                        mSelectedChild.getHitRect(mChildEndRect);                        mListener.onChildMoved(mSelectedChild, mChildStartRect, mChildEndRect);                    }

                    mSelectedChild.setVisibility(View.VISIBLE);                    mSelectedChild = null;                }
                break;        }
        return true;    }

    private void prepareTouch(float x, float y) {
        mStartTouch = null;        mSelectedChild = findChildViewInsideTouch(Math.round(x), Math.round(y));        if (mSelectedChild != null) {
            bringChildToFront(mSelectedChild);            LayoutParams lp = (LayoutParams) mSelectedChild.getLayoutParams();            lp.initial = new PointF(lp.left, lp.top);            mStartTouch = new PointF(x, y);            if (mChildStartRect == null) {
                mChildStartRect = new Rect();                mSelectedChild.getHitRect(mChildStartRect);            }
        }
    }

    /**     * Search by hightest index to lowest so that the     * most recently touched child is found first     *     * @return selectedChild     */    private View findChildViewInsideTouch(int x, int y) {
        for(int i=getChildCount()-1; i>=0; i--) {
            View view = getChildAt(i);            Rect rect = new Rect();            view.getHitRect(rect);            if (rect.contains(x, y)) {
                mChildStartRect = rect;                return view;            }
        }
        return null;    }

    public static class LayoutParams extends MarginLayoutParams {
        float left;        float top;        PointF initial;        boolean moved;
        public LayoutParams(Context c, AttributeSet attrs) {
            super(c, attrs);            left = -1.0f;            top = -1.0f;            initial = new PointF(0.0f, 0.0f);            moved = false;        }
    }

    @Override    protected void onRestoreInstanceState(Parcelable state) {
        if (!(state instanceof SavedState)) {
            super.onRestoreInstanceState(state);            return;        }

        SavedState ss = (SavedState) state;        super.onRestoreInstanceState(ss.getSuperState());
        mContainer = ss.container;        for (int i=0; i<getChildCount(); i++) {
            View view = getChildAt(i);            LayoutParams lp = (LayoutParams) view.getLayoutParams();
            if (view.getId() != NO_ID) {
                SavedState s = (SavedState) mContainer.get(view.getId());                lp.left = s.left;                lp.top = s.top;                lp.moved = s.movedFlag;            }
        }
    }

    @Override    protected Parcelable onSaveInstanceState() {
        for (int i=0; i<getChildCount(); i++) {
            View view = getChildAt(i);            LayoutParams lp = (LayoutParams) view.getLayoutParams();            view.saveHierarchyState(mContainer);
            if (view.getId() != NO_ID) {
                SavedState s = new SavedState(mContainer.get(view.getId()));                s.left = lp.left;                s.top = lp.top;                s.movedFlag = lp.moved;                mContainer.put(view.getId(), s);            }
        }
        Parcelable p = super.onSaveInstanceState();        SavedState ss = new SavedState(p);        ss.container = mContainer;        return ss;    }

    private static class SavedState extends BaseSavedState {
        float left, top;        boolean movedFlag;        SparseArray<Parcelable> container;
        public SavedState(Parcelable p) {
            super(p);        }
    }

    /**     * set ChildPositionListener to receive updates whenever child is moved     *     * @param listener     */    public void setChildPositionListener(ChildPositionListener listener) {
        mListener = listener;    }

    public interface ChildPositionListener {
        /**         * this callback is invoked whenever child is moved         *         * @param childView the current child view that was dragged         * @param oldPosition the original position from where child was dragged         * @param newPosition the new position where child is currently laid         */        void onChildMoved(View childView, Rect oldPosition, Rect newPosition);    }

    @Override    public String toString() {
        StringBuilder out = new StringBuilder(128);        out.append(TAG);        out.append(" mSelectedChild: ");        if (mSelectedChild != null) {
            out.append(this.mSelectedChild.toString());        }
        return out.toString();    }
}












































22

Friday 16 June 2017

Retrofit With POST Method



STEP-1 : AndroidManifest.xml
--------------------------------------------------------------
<uses-permission android:name="android.permission.INTERNET"/>
--------------------------------------------------------------------

STEP-2 : App Level Gradle

---------------------------------------------------------------------
    // retrofit, gson
    compile 'com.google.code.gson:gson:2.8.0'
    compile 'com.squareup.retrofit2:retrofit:2.3.0'
    compile 'com.squareup.retrofit2:converter-gson:2.0.2'
------------------------------------------------------------------------

STEP-3 : ApiInterface.interface
-----------------------------------------------------------------
import retrofit2.Call;
import retrofit2.http.Field;
import retrofit2.http.FormUrlEncoded;
import retrofit2.http.POST;

public interface ApiInterface {

   @FormUrlEncoded
   @POST("/currency/cu.php")
   Call<POJOs> getRetrofitResponse(@Field("amount") String amount, @Field("fromc") String from, @Field("to") String to);
}

---------------------------------------------------------------------

STEP-4 : ApiClient.java
---------------------------------------------------------------------
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class ApiClient {

    public static final String BASE_URL = "http://192.168.1.3";
    private static Retrofit retrofit = null;

    public static Retrofit getClient() {
        if (retrofit==null) {
            retrofit = new Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
        }
        return retrofit;
    }
}

-----------------------------------------------------------------------

STEP-5 : MainActivity.java
------------------------------------------------------------------------
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MainActivity extends AppCompatActivity {

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

        ApiInterface apiService = ApiClient.getClient().create(ApiInterface.class);

        apiService.getRetrofitResponse("2", "USD", "INR").enqueue(new Callback<POJOs>() {
            @Override
            public void onResponse(Call<POJOs> call, Response<POJOs> response) {

                System.out.println(response.code());
                if(response.isSuccessful()) {

                    Double s = response.body().getValue();
                        Toast.makeText(MainActivity.this, "OUTPUT : "+s,Toast.LENGTH_LONG).show();

                }else {
                  //  int statusCode  = response.code();
                    // handle request errors depending on status code
                }
            }

            @Override
            public void onFailure(Call<POJOs> call, Throwable t) {
                Log.d("MainActivity", "error loading from API");

            }
        });
 
    }
}

---------------------------------------------------------------------------

STEP-6 : POJOs.java
---------------------------------------------------------------------
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class POJOs {

    @SerializedName("value ")
    @Expose
    private Double value;

    public Double getValue() {
        return value;
    }

    public void setValue(Double value) {
        this.value = value;
    }

}
--------------------------------------------------------------------------

STEP-7 : cu.php
---------------------------------------------------------------------------------
<?php
function currencyConvertor($amount,$from_Currency,$to_Currency) {
     $amount = urlencode($amount);
      $from_Currency = urlencode($from_Currency);
      $to_Currency = urlencode($to_Currency);
      $get = "https://www.google.com/finance/converter?a=$amount&from=$from_Currency&to=$to_Currency";
      $ch = curl_init();
                                        $url = $get;
                                        // Disable SSL verification
                                        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
                                        // Will return the response, if false it print the response
                                        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                                        // Set the url
                                        curl_setopt($ch, CURLOPT_URL,$url);
                                        // Execute
                                        $result=curl_exec($ch);
                                        // Closing
                                        curl_close($ch);
      $get = explode("<span class=bld>",$result);
      $get = explode("</span>",$get[1]); 
      $converted_amount = preg_replace("/[^0-9\.]/", null, $get[0]);
      //return number_format($converted_amount, 2, '.', '');
 return round($converted_amount, 3);
   
}

if(isset($_POST['amount']) && isset($_POST['fromc']) && isset($_POST['to']))
 {

         $amount = $_POST['amount'];
         $fromc = $_POST['fromc'];
        $to   = $_POST['to'];
        $curr =   currencyConvertor($amount,$fromc,$to);
        echo json_encode(array("value "=>$curr));
       
 }

?>
-------------------------------------------------------------------------------------

IMAGE ON POSTMAN
-------------------------------------------------------------------


-----------------------------------------------------------------------













22

Wednesday 14 June 2017

Retrofit With Nested JSON Data (GET Method)

IMPORTANT POINT ::
1.In Retrofit library, Data parse according to POJO classes. If convertor is in GSON then convert your JSON to GSON POJO class.
2. If JSON is nested then multiple POJO Class is generate. In this app, Contact is array name and sub-array is Phone then 3 POJO Class will generate.
i. MainPojo - It is first class where all JSON array and sub-array listed and return POJO type value
ii. Contact - Json array, It's value can retrieve by MainPojo class.
iii. Phone - Json Sub-Array, It's value can retrieve by Contact POJO class.


STEP-1 : AndroidManifest.xml

----------------------------------------------------------------------

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

STEP-2 : App level Gradle Library

-----------------------------------------------------------------------------

// retrofit, gsoncompile 'com.google.code.gson:gson:2.8.0'compile 'com.squareup.retrofit2:retrofit:2.3.0'compile 'com.squareup.retrofit2:converter-gson:2.0.2'
--------------------------------------------------------------------------------

STEP-3 :
ApiInterface.interface
-----------------------------------------------------------------------

import retrofit2.Call;import retrofit2.http.GET;

public interface ApiInterface {

   @GET("/contacts/")
   Call<Movie> getTopRatedMovies();
}

-----------------------------------------------------------------------------

STEP-3 : ApiClient

------------------------------------------------------------------------------

import retrofit2.Retrofit;import retrofit2.converter.gson.GsonConverterFactory;

public class ApiClient {

   // public static final String BASE_URL = "http://192.168.1.10";    public static final String BASE_URL = "http://api.androidhive.info";    private static Retrofit retrofit = null;
    public static Retrofit getClient() {
        if (retrofit==null) {
            retrofit = new Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();        }
        return retrofit;    }
}
------------------------------------------------------------------------------

STEP-5 : MainActivity.java

-----------------------------------------------------------------------------------

import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.util.Log;import android.widget.Toast;import java.util.List;import retrofit2.Call;import retrofit2.Callback;import retrofit2.Response;
public class MainActivity extends AppCompatActivity {

    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);
        ApiInterface apiService =
                ApiClient.getClient().create(ApiInterface.class);

        apiService.getTopRatedMovies().enqueue(new Callback<Movie>() {
            @Override            public void onResponse(Call<Movie> call, Response<Movie> response) {

                if(response.isSuccessful()) {
                //  Toast.makeText(MainActivity.this, "OUTPUT : "+response.body().getContacts(), Toast.LENGTH_LONG).show();                    Log.d("MainActivity", "posts loaded from API");                 List<Contact> contactList =  response.body().getContacts();                    for(int i = 0; i<contactList.size(); i++)
                    {
                        int statusCode  = response.code();                        String id = contactList.get(i).getId();                        String name = contactList.get(i).getName();                        String email = contactList.get(i).getEmail();                        String address = contactList.get(i).getAddress();                        String gender = contactList.get(i).getGender();                        Phone phone = contactList.get(i).getPhone();                        String mobile = phone.getMobile();                        String home = phone.getHome();                        String office = phone.getOffice();                        Toast.makeText(MainActivity.this, "OUTPUT : "+statusCode +", "+ id +", "+ name +", "+ email +", "+ address +", "+ gender +", "+ mobile +", "+ home +", "+ office, Toast.LENGTH_LONG).show();                    }
                }else {
                    int statusCode  = response.code();                    // handle request errors depending on status code                }
            }

            @Override            public void onFailure(Call<Movie> call, Throwable t) {
                Log.d("MainActivity", "error loading from API");
            }
        });    
    }
}
-----------------------------------------------------------------------------------

POJO CLASSES
STEP-6 : Movie.java   (POJO Class)
-------------------------------------------------------------------------
   import com.google.gson.annotations.Expose;        import com.google.gson.annotations.SerializedName;
        import java.util.List;
public class Movie {

    @SerializedName("contacts")
    @Expose    private List<Contact> contacts = null;
    public List<Contact> getContacts() {
        return contacts;    }

    public void setContacts(List<Contact> contacts) {
        this.contacts = contacts;    }

}
-------------------------------------------------------------------------------------

STEP-7 : Contact.java  (POJO Class)

----------------------------------------------------------------------

        import com.google.gson.annotations.Expose;        import com.google.gson.annotations.SerializedName;
public class Contact {

    @SerializedName("id")
    @Expose    private String id;    @SerializedName("name")
    @Expose    private String name;    @SerializedName("email")
    @Expose    private String email;    @SerializedName("address")
    @Expose    private String address;    @SerializedName("gender")
    @Expose    private String gender;    @SerializedName("phone")
    @Expose    private Phone phone;
    public String getId() {
        return id;    }

    public void setId(String id) {
        this.id = id;    }

    public String getName() {
        return name;    }

    public void setName(String name) {
        this.name = name;    }

    public String getEmail() {
        return email;    }

    public void setEmail(String email) {
        this.email = email;    }

    public String getAddress() {
        return address;    }

    public void setAddress(String address) {
        this.address = address;    }

    public String getGender() {
        return gender;    }

    public void setGender(String gender) {
        this.gender = gender;    }

    public Phone getPhone() {
        return phone;    }

    public void setPhone(Phone phone) {
        this.phone = phone;    }

}
--------------------------------------------------------------------------------------

STEP -8 : Phone.java (POJO Class)

----------------------------------------------------------

import com.google.gson.annotations.Expose;import com.google.gson.annotations.SerializedName;
public class Phone {

    @SerializedName("mobile")
    @Expose    private String mobile;    @SerializedName("home")
    @Expose    private String home;    @SerializedName("office")
    @Expose    private String office;
    public String getMobile() {
        return mobile;    }

    public void setMobile(String mobile) {
        this.mobile = mobile;    }

    public String getHome() {
        return home;    }

    public void setHome(String home) {
        this.home = home;    }

    public String getOffice() {
        return office;    }

    public void setOffice(String office) {
        this.office = office;    }

}
---------------------------------------------------------------------------------------

STEP-10, Finally JSON

----------------------------------------------------------------

{
    "contacts": [
        {
                "id": "c200",
                "name": "Ravi Tamada",
                "email": "ravi@gmail.com",
                "address": "xx-xx-xxxx,x - street, x - country",
                "gender" : "male",
                "phone": {
                    "mobile": "+91 0000000000",
                    "home": "00 000000",
                    "office": "00 000000"
                }
        },
        {
                "id": "c201",
                "name": "Johnny Depp",
                "email": "johnny_depp@gmail.com",
                "address": "xx-xx-xxxx,x - street, x - country",
                "gender" : "male",
                "phone": {
                    "mobile": "+91 0000000000",
                    "home": "00 000000",
                    "office": "00 000000"
                }
        },
        {
                "id": "c202",
                "name": "Leonardo Dicaprio",
                "email": "leonardo_dicaprio@gmail.com",
                "address": "xx-xx-xxxx,x - street, x - country",
                "gender" : "male",
                "phone": {
                    "mobile": "+91 0000000000",
                    "home": "00 000000",
                    "office": "00 000000"
                }
        },
        {
                "id": "c203",
                "name": "John Wayne",
                "email": "john_wayne@gmail.com",
                "address": "xx-xx-xxxx,x - street, x - country",
                "gender" : "male",
                "phone": {
                    "mobile": "+91 0000000000",
                    "home": "00 000000",
                    "office": "00 000000"
                }
        },
        {
                "id": "c204",
                "name": "Angelina Jolie",
                "email": "angelina_jolie@gmail.com",
                "address": "xx-xx-xxxx,x - street, x - country",
                "gender" : "female",
                "phone": {
                    "mobile": "+91 0000000000",
                    "home": "00 000000",
                    "office": "00 000000"
                }
        },
        {
                "id": "c205",
                "name": "Dido",
                "email": "dido@gmail.com",
                "address": "xx-xx-xxxx,x - street, x - country",
                "gender" : "female",
                "phone": {
                    "mobile": "+91 0000000000",
                    "home": "00 000000",
                    "office": "00 000000"
                }
        },
        {
                "id": "c206",
                "name": "Adele",
                "email": "adele@gmail.com",
                "address": "xx-xx-xxxx,x - street, x - country",
                "gender" : "female",
                "phone": {
                    "mobile": "+91 0000000000",
                    "home": "00 000000",
                    "office": "00 000000"
                }
        },
        {
                "id": "c207",
                "name": "Hugh Jackman",
                "email": "hugh_jackman@gmail.com",
                "address": "xx-xx-xxxx,x - street, x - country",
                "gender" : "male",
                "phone": {
                    "mobile": "+91 0000000000",
                    "home": "00 000000",
                    "office": "00 000000"
                }
        },
        {
                "id": "c208",
                "name": "Will Smith",
                "email": "will_smith@gmail.com",
                "address": "xx-xx-xxxx,x - street, x - country",
                "gender" : "male",
                "phone": {
                    "mobile": "+91 0000000000",
                    "home": "00 000000",
                    "office": "00 000000"
                }
        },
        {
                "id": "c209",
                "name": "Clint Eastwood",
                "email": "clint_eastwood@gmail.com",
                "address": "xx-xx-xxxx,x - street, x - country",
                "gender" : "male",
                "phone": {
                    "mobile": "+91 0000000000",
                    "home": "00 000000",
                    "office": "00 000000"
                }
        },
        {
                "id": "c2010",
                "name": "Barack Obama",
                "email": "barack_obama@gmail.com",
                "address": "xx-xx-xxxx,x - street, x - country",
                "gender" : "male",
                "phone": {
                    "mobile": "+91 0000000000",
                    "home": "00 000000",
                    "office": "00 000000"
                }
        },
        {
                "id": "c2011",
                "name": "Kate Winslet",
                "email": "kate_winslet@gmail.com",
                "address": "xx-xx-xxxx,x - street, x - country",
                "gender" : "female",
                "phone": {
                    "mobile": "+91 0000000000",
                    "home": "00 000000",
                    "office": "00 000000"
                }
        },
        {
                "id": "c2012",
                "name": "Eminem",
                "email": "eminem@gmail.com",
                "address": "xx-xx-xxxx,x - street, x - country",
                "gender" : "male",
                "phone": {
                    "mobile": "+91 0000000000",
                    "home": "00 000000",
                    "office": "00 000000"
                }
        }
    ]
}
-----------------------------------------------------------------------------------------

















22