1. What is Retrofit?
Retrofit is a REST Client library
Helper library used in android and java to create an HTTP request
and also process the HTTP response from a REST API.
2. Create an Interface Class.
public interface endpointservice {
// GET AND SET two method use to depend on you.
// put the endpoint file name in GET("") . that means the last name of the base URL
@GET("users.php")
// first my module convert the list then get the value by listresponse()
Call<List<mymodule> listresponse(@Field("User_name"),String name)
}
3. Create the Instance Class.
public class myretrofitInstance{
public final String BASE_URL = "https://example.com/fruit";
Retrofit retrofit = new Retrofit.Builder().baseUrl(BASE_URL )
.addConverterFactory(GsonConverterFactory.create()).build();
}
4. Now Final MainActivity class
// service is initialization in this class
// first initialization instance class.
// second store creates service in the service variable.
// then call the method which is created in service class and pass the parameter.
// now the last process call the enqueue method which is inside two methods 1. onResponse 2. onFailure
// MainActivity
public myretrofitInstance adbs;
endpointservice andpoint = adbs.create(endpointservice .class);
Call<mymodule> call = andpoint.listresponse("API_KEY");
call.enqueue(new Callback<mymodule>(){
@Override
public void onResponse(Call<mymodule> call,Response<mymodule> response)
{
List<mymodule> mymodule = response.body.getResult();
recyclerview.setAdapter(new MyAdapter(mymodule,R.layout.list_item_movie,getApplicationContext()));
}
@Override
public void onFailure(Call<mymodule> call, Throwable throwable)
{
Log.e(TAG, throwable.tostring());
}
});
5. dependencies
compile fileTree(dir: ‘libs’, include: [‘*.jar’])
testCompile ‘junit:junit:4.12’
compile ‘com.android.support:appcompat-v7:23.3.0’
// retrofit, gson
compile ‘com.squareup.picasso:picasso:2.5.2’
compile ‘com.squareup.retrofit2:retrofit:2.1.0’
compile ‘com.squareup.retrofit2:converter-gson:2.1.0’
compile ‘com.android.support:recyclerview-v7:25.0.0’