Hello friends
today we are going to learn about RETROFIT in android. it's s REST API in java and android. it's an easy way to upload and store json formate API data in an android application. Retrofit is a REST Client for Android and Java by Square. Let's take an example of this in android.
We need to add some dependencies in our gradle file.
compile 'com.google.code.gson:gson:2.6.2'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
compile 'com.squareup.okhttp3:logging-interceptor:3.4.1'
compile 'com.squareup.okhttp3:okhttp:3.4.1'
APIForClient.java
public class APIForClient {
private static Retrofit retrofit = null;
static Retrofit getApiCllient() {
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build();
retrofit = new Retrofit.Builder()
.baseUrl("https://demorequest/")
.addConverterFactory(GsonConverterFactory.create())
.client(client)
.build();
return retrofit;
}
}
APIInterface.java
public interface APIInterface {
@GET("/api/unknown")
Call<UserDetails> doGetListResources();
}
UserDetails.java
This is our pojo class for store api json response in android.
public class UserDetails {
@SerializedName("fname")
public Integer fname;
@SerializedName("mname")
public Integer mname;
@SerializedName("lname")
public Integer lname;
public Integer getFname() {
return fname;
}
public void setFname(Integer fname) {
this.fname = fname;
}
public Integer getMname() {
return mname;
}
public void setMname(Integer mname) {
this.mname = mname;
}
public Integer getLname() {
return lname;
}
public void setLname(Integer lname) {
this.lname = lname;
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity {
APIInterface apiInterface;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
apiInterface = APIForClient.getApiCllient().create(APIInterface.class);
retrofit2.Call<UserDetails> call = apiInterface.doGetListResources();
call.enqueue(new Callback<UserDetails>() {
@Override
public void onResponse(retrofit2.Call<UserDetails> call, Response<UserDetails> response) {
//Store response data
}
@Override
public void onFailure(retrofit2.Call<UserDetails> call, Throwable t) {
//something is wrong
}
});
}
}
today we are going to learn about RETROFIT in android. it's s REST API in java and android. it's an easy way to upload and store json formate API data in an android application. Retrofit is a REST Client for Android and Java by Square. Let's take an example of this in android.
We need to add some dependencies in our gradle file.
compile 'com.google.code.gson:gson:2.6.2'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
compile 'com.squareup.okhttp3:logging-interceptor:3.4.1'
compile 'com.squareup.okhttp3:okhttp:3.4.1'
APIForClient.java
public class APIForClient {
private static Retrofit retrofit = null;
static Retrofit getApiCllient() {
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build();
retrofit = new Retrofit.Builder()
.baseUrl("https://demorequest/")
.addConverterFactory(GsonConverterFactory.create())
.client(client)
.build();
return retrofit;
}
}
APIInterface.java
public interface APIInterface {
@GET("/api/unknown")
Call<UserDetails> doGetListResources();
}
UserDetails.java
This is our pojo class for store api json response in android.
public class UserDetails {
@SerializedName("fname")
public Integer fname;
@SerializedName("mname")
public Integer mname;
@SerializedName("lname")
public Integer lname;
public Integer getFname() {
return fname;
}
public void setFname(Integer fname) {
this.fname = fname;
}
public Integer getMname() {
return mname;
}
public void setMname(Integer mname) {
this.mname = mname;
}
public Integer getLname() {
return lname;
}
public void setLname(Integer lname) {
this.lname = lname;
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity {
APIInterface apiInterface;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
apiInterface = APIForClient.getApiCllient().create(APIInterface.class);
retrofit2.Call<UserDetails> call = apiInterface.doGetListResources();
call.enqueue(new Callback<UserDetails>() {
@Override
public void onResponse(retrofit2.Call<UserDetails> call, Response<UserDetails> response) {
//Store response data
}
@Override
public void onFailure(retrofit2.Call<UserDetails> call, Throwable t) {
//something is wrong
}
});
}
}
Comments
Post a Comment