Friday, 27 October 2017

Retrofit Sample in android

Hello friends today we are learn about how to use Retrofit lib

Create a new project and follow below steps.

build.gradle

/* Retrofit*/
final RETROFIT_VERSION = '2.3.0'
compile "com.squareup.retrofit2:retrofit:$RETROFIT_VERSION"
compile "com.squareup.retrofit2:converter-gson:$RETROFIT_VERSION"
compile "com.squareup.retrofit2:retrofit-mock:$RETROFIT_VERSION"
compile 'com.squareup.okhttp3:logging-interceptor:3.4.1'

compile 'com.android.support:recyclerview-v7:25.3.1'
compile 'com.android.support:cardview-v7:25.3.1'




MainActivity.java

package com.samset.retrofitexample;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.AppCompatButton;
import android.support.v7.widget.AppCompatEditText;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ProgressBar;

import com.samset.retrofitexample.adapter.RepositoryAdapter;
import com.samset.retrofitexample.model.Repository;
import com.samset.retrofitexample.network.ApiService;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MainActivity extends AppCompatActivity {
    private AppCompatButton btnSearch;
    private AppCompatEditText etSearch;
    private RecyclerView recyclerView;
    private ProgressBar progressBar;

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

        // by default call
        getDatafromServer("samsetdev");
    }

    private void callApi() {
        btnSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (etSearch.getText().toString() != null && !etSearch.getText().toString().isEmpty()) {
                    getDatafromServer(etSearch.getText().toString());
                }
            }
        });
    }

    private void initView() {
        progressBar = (ProgressBar) findViewById(R.id.progressbar);
        recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
        btnSearch = (AppCompatButton) findViewById(R.id.btnsearch);
        etSearch = (AppCompatEditText) findViewById(R.id.etrepository);
    }

    private void getDatafromServer(String username) {

        ApiService usersService = AppApplication.getInstance().getApiService();

        usersService.getRepositories(username).enqueue(new Callback<List<Repository>>() {
            @Override
            public void onResponse(Call<List<Repository>> call, Response<List<Repository>> response) {
                progressBar.setVisibility(View.GONE);
                Log.e("TAG", " Response " + response.body().size());
                setupRecyclerView(response.body());
            }

            @Override
            public void onFailure(Call<List<Repository>> call, Throwable t) {
                progressBar.setVisibility(View.GONE);
            }
        });
    }

    private void setupRecyclerView(List<Repository> repositories) {
        RepositoryAdapter adapter = new RepositoryAdapter(repositories);
        recyclerView.setAdapter(adapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
    }
}


activity_main.xml


<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.samset.retrofitexample.MainActivity">

    <ProgressBar
        android:id="@+id/progressbar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


    <android.support.v7.widget.AppCompatEditText
        android:id="@+id/etrepository"
        android:layout_width="280dp"
        android:text="samsetdev"
        android:layout_height="wrap_content"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <android.support.v7.widget.AppCompatButton
        android:id="@+id/btnsearch"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="10dp"
        android:text="Search"
        android:textColor="@color/colorAccent"
        app:layout_constraintLeft_toRightOf="@+id/etrepository" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toBottomOf="@+id/etrepository" />

</android.support.constraint.ConstraintLayout>


Thank you 

FullSourceCode DownloadRetrofitSample




















No comments:

Post a Comment