Rectrofit 2.0

January 28, 2016 1 Comments

Here is my suggestion on the new URL declaration pattern in Retrofit 2.0:

- Base URL: always ends with /

- @Url    :       DO NOT start with /

Gradle Dependancy : compile 'com.squareup.retrofit:retrofit:2.0.0-beta3'

Synchronous and Asynchronous ====>

// Synchronous Call in Retrofit 2.0

Call<Repo> call = service.loadRepo();
Repo repo = call.execute();

// Asynchronous Call in Retrofit 2.0

Call<Repo> call = service.loadRepo();
call.enqueue(new Callback<Repo>() {
@Override
public void onResponse(Response<Repo> response) {
// Get result Repo from response.body()
}

@Override
public void onFailure(Throwable t) {

}
});

I suggest you to use enqueue. It fits Android OS behavior best.

Ongoing Transaction Cancellation :

call.cancel();

Converter is now excluded from Retrofit.=>

In Retrofit 1.9, GsonConverter is included in the package and is automatically initiated upon RestAdapter creation.
As a result, the json result from server would be automatically parsed to the defined Data Access Object (DAO).

But in Retrofit 2.0, Converter is not included in the package anymore. You need to plug a Converter in yourself
or Retrofit will be able to accept only the String result.As a result, Retrofit 2.0 doesn't depend on Gson anymore.

AddConvertor on Init:

Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://api.nuuneoi.com/base/")
.addConverterFactory(GsonConverterFactory.create())
.build();

Choose one that fits your requirement best.

Gson: com.squareup.retrofit:converter-gson
Jackson: com.squareup.retrofit:converter-jackson
Moshi: com.squareup.retrofit:converter-moshi
Protobuf: com.squareup.retrofit:converter-protobuf
Wire: com.squareup.retrofit:converter-wire
Simple XML: com.squareup.retrofit:converter-simplexml
For Different Purpose set Different Annotation :

->POST
->BODY
->DELETE
->Field
->FieldMap
->GET
->HEAD
->Multipart
->Path
->POST
->PUT
->Query
->QueryMap

  Conclusion


There are also some other changes, the official change log as detailed as possible. Anyway, I covered the major issues of this article believe.



You still have time to turn up to 2.0 may be surprising?

GitHub has been removed from the site 1.9 square Transformation official documents note. I just started learning the latest version 2.0 of transformation and in the near future, I think the move command.



Its new launching so can't get perfect document as well as No help.


So you just want to start new version with new changes than definitely use this but its not a stable version its just beta version so changes are available.


Benefit of this

- No need to Use GSON library to convert data

- Response Data directly On parsing model.

- we get more method to modify as well as get more info.



=====================

XMLCONVERTOR Check this



Demo Also In it :


 

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

1 comments:

CreativeDeveloper said...

http://mvnrepository.com/
for all the repository json gradle and all of this.

Hot-product

Best Watch Store

Popular Posts

Contact Form

Name

Email *

Message *