How to get JSON object without converting in Retrofit 2.0?

October 06, 2017 , 0 Comments

Hello friends,
Today we giving simple snippet to get Json Object without converting in Rectrofit 2.0,

Let's Start Step by step :

STEP 1: GRADLE Dependency:

compile 'com.squareup.retrofit2:retrofit:2.2.0'
compile 'com.squareup.retrofit2:converter-gson:2.2.0'
compile 'com.squareup.okhttp3:logging-interceptor:3.4.1'


STEP 2: ApiInterface

public interface ApiService {
String BASEURL = "https://api.androidhive.info/";
@POST("contacts/")
Call getContactList();
}


STEP 3: How to SETUP:


private static void setupRestClient() {
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).readTimeout(60, TimeUnit.SECONDS)
.connectTimeout(60, TimeUnit.SECONDS).build();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(ApiService.BASEURL)
.client(client)
.addConverterFactory(GsonConverterFactory.create())
.build();
ApiService REST_CLIENT = retrofit.create(ApiService.class);

}



STEP 4 : HOW TO USED :



private void getContactList() {

Call getContact = RestClient.post().getContactList();
getContact.enqueue(new Callback() {
@Override
public void onResponse(Call call, Response response) {
JsonElement jsonElement = response.body();
Log.e("Contact_List", jsonElement.toString());
}

@Override
public void onFailure(Call call, Throwable t) {
Log.e("Contact_List", "failed");
}
});

}



Now Call : getContactList();

DEMO HERE

Contributer

Some say he’s half man half fish, others say he’s more of a seventy/thirty split. Either way he’s a fishy bastard. Google

0 comments:

Hot-product

Best Watch Store

Popular Posts

Contact Form

Name

Email *

Message *