HttpGet Example via Retrofit 2

2 min read

images_m

 

Dear friends

Today we gonna use a very popular library for Making Http requests in Android, Called Retrofit. Its very fast. ( Even faster than Asynctask).

Its lightweight.

Easy to integrate lib. We need to add some dependencies in gradle and we are ready to use it in our application.

But yes we need to do some extra work related to calls but once that is done its turnaround time to bring results is just amazing.
Lets go step by step

we are going to hit the url for fetching data by GET request : http://relsellglobal.in/DeliveryS/CRKInsightsServer/mobilecheck.php?controlVar=32

 

if you click the above link you will find a JSON response will be printed on the screen. So you need to see JSON structure and create your POJO classes accordingly. For eg in the above link

{
“list”: {
“data”: [
{
“ID”: “1”,
“USERNAME”: “Thanks”,
“FCMID”: “eo5uBBIecUI:APA91bFUEnw-fe5Ow4xm3cY8ST2VWs2W4gg8PCG2qX4PqCzlJT1Y6eY5H2sPpUqAQlWyGW3w29VLsVQN7xjwvfeNT6OIEBfLc9gBcja-9Cubl4JnFQDtmYnd2_AZyaVEjz1ZeIKVzin2”,
“EMAILID”: “relsellglobal@gmail.com”,
“userimage”: “http://www.relsellglobal.in/chatapp/profileimages/userprofile.jpg”
}, ……….
] // data closed
} // list closed
} // root closed

So if we refer above , we have a JSON Object at root then we have another JSON Object called list.
There after we have a JSON Array called data . data array is having other JSON Objects. Similarly in Java we will be having a root class ,

Root Object has a WebList Object. WebList object has a data List. data List has WebInnerObject.

WebInnerObject will have properties like ID, EMAILID, userimage, FCMID etc.

 

Please see below files

/*
 * Copyright (c) 2017. Relsell Global
 */

package in.relsellglobal.retrofit2blogdemo;

/**
 * Created by anilkukreti on 26/06/17.
 */

public class WebPojo {
    WebList list;
}

 

/*
 * Copyright (c) 2017. Relsell Global
 */

package in.relsellglobal.retrofit2blogdemo;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by anilkukreti on 26/06/17.
 */

public class WebList {
    List<WebInnerPojo> data;
}

 

/*
 * Copyright (c) 2017. Relsell Global
 */


package in.relsellglobal.retrofit2blogdemo;

/**
 * Created by anilkukreti on 26/06/17.
 */

public class WebInnerPojo {
    String ID;
    String USERNAME;
    String FCMID;
    String EMAILID;
    String userimage;

    @Override
    public String toString() {
        return "WebInnerPojo{" +
                "ID='" + ID + '\'' +
                ", USERNAME='" + USERNAME + '\'' +
                ", FCMID='" + FCMID + '\'' +
                ", EMAILID='" + EMAILID + '\'' +
                ", userimage='" + userimage + '\'' +
                '}';
    }
}

 

WebInterfaceService class. This class is required and has methods which are used to interact with HttpServer.

 

/*
 * Copyright (c) 2017. Relsell Global
 */

package in.relsellglobal.retrofit2blogdemo;

import java.util.List;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Call;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.http.GET;
import retrofit2.http.Path;

/**
 * Created by anilkukreti on 26/06/17.
 */

public interface WebInterfaceService {
    @GET("mobilecheck.php?controlVar=32")
    Call<WebPojo> getChatUsers();

}

Simply our mainactivity is having code that calls the webserviceinterface method.

 

/*
 * Copyright (c) 2017. Relsell Global
 */

package in.relsellglobal.retrofit2blogdemo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

import java.util.List;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class MainActivity extends AppCompatActivity {

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

        HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
        logging.setLevel(HttpLoggingInterceptor.Level.BASIC);
        OkHttpClient client = new OkHttpClient.Builder()
                .addInterceptor(logging)
                .build();


        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://relsellglobal.in/DeliveryS/CRKInsightsServer/")
                .addConverterFactory(GsonConverterFactory.create())
                .client(client)
                .build();


        WebInterfaceService webInterfaceService = retrofit.create(WebInterfaceService.class);
        final Call<WebPojo> call =
                webInterfaceService.getChatUsers();
        call.enqueue(new Callback<WebPojo>() {
            @Override
            public void onResponse(Call<WebPojo> call, Response<WebPojo> response) {

                WebList list = response.body().list;

                List<WebInnerPojo> data = list.data;

                for(WebInnerPojo webInnerPojo : data) {
                    Log.v("TAG",webInnerPojo.toString());
                }

            }
            @Override
            public void onFailure(Call<WebPojo> call, Throwable t) {
                Log.v("TAG","Some thing went wrong");
            }
        });

    }
}

 

And at last but not the lease here is the working code example  Retrofit2BlogDemo

 

Happy Coding !