How to bring to your project

Слайд 2

How to bring to your project

compile 'com.squareup.retrofit2:retrofit:2.3.0' compile 'com.squareup.retrofit2:converter-gson:2.3.0'

How to bring to your project compile 'com.squareup.retrofit2:retrofit:2.3.0' compile 'com.squareup.retrofit2:converter-gson:2.3.0'

Слайд 3

What you need
Model class which is used to map the JSON data

What you need Model class which is used to map the JSON
to
Interfaces which defines the possible HTTP operations
Retrofit.Builder class - Instance which uses the interface and the Builder API which allows defining the URL end point for the HTTP operation.

Слайд 4

Model
Simple class with setters and getters

public class TimeZoneApiResponse { @SerializedName("status") @Expose private

Model Simple class with setters and getters public class TimeZoneApiResponse { @SerializedName("status")
String status; @SerializedName("message") @Expose private String message; @SerializedName("countryCode") @Expose private String countryCode;

public String getStatus() { return status; } public void setStatus(String status) { this.status = status; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public String getCountryCode() { return countryCode; } public void setCountryCode(String countryCode) { this.countryCode = countryCode; }

Слайд 5

Interface

public interface TimeZoneAPI { @GET("get-time-zone") Call getTimeZone(@Query("key") String apiKey, @Query("format") String format,

Interface public interface TimeZoneAPI { @GET("get-time-zone") Call getTimeZone(@Query("key") String apiKey, @Query("format") String
@Query("by") String searchBy, @Query("lat") String latitude, @Query("lng") String longitude); }

There are five built-in annotations: GET, POST, PUT, DELETE, and HEAD
Another annotations for data providing: Query, Path, Body

Слайд 6

Retrofit.Builder

public TimeZoneAPI getTimeZoneAPI() { return new Retrofit.Builder() .baseUrl("http://api.timezonedb.com/v2/") .client(initClient()) .addConverterFactory(GsonConverterFactory.create()) .build().create(TimeZoneAPI.class); }

@NonNull private OkHttpClient

Retrofit.Builder public TimeZoneAPI getTimeZoneAPI() { return new Retrofit.Builder() .baseUrl("http://api.timezonedb.com/v2/") .client(initClient()) .addConverterFactory(GsonConverterFactory.create()) .build().create(TimeZoneAPI.class);
initClient() { HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(); interceptor.setLevel(HttpLoggingInterceptor.Level.BODY); return new OkHttpClient.Builder() .connectTimeout(CLIENT_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS) .addNetworkInterceptor(new StethoInterceptor()) .addInterceptor(interceptor) .build(); }

Слайд 7

Authorization

Authorization

Слайд 8

/** * An invocation of a Retrofit method that sends a request

/** * An invocation of a Retrofit method that sends a request
to a webserver and returns a response. * Each call yields its own HTTP request and response pair. Use {@link #clone} to make multiple * calls with the same parameters to the same webserver; this may be used to implement polling or * to retry a failed call. * *

Calls may be executed synchronously with {@link #execute}, or asynchronously with {@link * #enqueue}. In either case the call can be canceled at any time with {@link #cancel}. A call that * is busy writing its request or reading its response may receive a {@link IOException}; this is * working as designed. * * @param Successful response body type. */ public interface Call extends Cloneable { /** * Synchronously send the request and return its response. * * @throws IOException if a problem occurred talking to the server. * @throws RuntimeException (and subclasses) if an unexpected error occurs creating the request * or decoding the response. */ Response execute() throws IOException; /** * Asynchronously send the request and notify {@code callback} of its response or if an error * occurred talking to the server, creating the request, or processing the response. */ void enqueue(Callback callback);

What you get when create a request

Слайд 9

How to deal with a Call

public interface Callback { /** * Invoked

How to deal with a Call public interface Callback { /** *
for a received HTTP response. *

* Note: An HTTP response may still indicate an application-level failure such as a 404 or 500. * Call {@link Response#isSuccessful()} to determine if the response indicates success. */ void onResponse(Call call, Response response); /** * Invoked when a network exception occurred talking to the server or when an unexpected * exception occurred creating the request or processing the response. */ void onFailure(Call call, Throwable t); }

Имя файла: How-to-bring-to-your-project.pptx
Количество просмотров: 30
Количество скачиваний: 0