Retrofit concepts

Retrofit is a wrapper around a RESTful HTTP Web request framework. The web request work is essentially done by OkHttp, while Retrofit is only responsible for the wrapper around the web request interface.

Retrofit Framework Network request flowchart:

Integration with Android

1. Add dependencies

Build. Gradle: The first is to add Retrofit, the second and third are to add JSON parsing, here using GSON

Implementation 'com. Squareup. Retrofit2: retrofit: 2.1.0' implementation 'com. Squareup. Retrofit2: converter - gson: 2.0.2' Implementation 'com. Google. Code. Gson: gson: 2.7' implementation 'com. Squareup. Okhttp3: okhttp: 3.2.0'Copy the code
2. Add the OkHttp configuration
// create okHttpClient.builder Builder = new okHttpClient.builder (); builder.connectTimeout(1000*60, TimeUnit.SECONDS); WriteTimeout (1000*60, timeunit.seconds); ReadTimeout (1000*60, timeunit.seconds); // Write timeout builder.readTimeout(1000*60, timeunit.seconds); // Read operation timeoutCopy the code
3. Add retroFIT configuration
ApiConfig.BASE_URL = "https://wanandroid.com/"; // create OKHttpClient okHttpClient.builder httpBuilder = new okHttpClient.builder (); httpBuilder.connectTimeout(1000*60, TimeUnit.SECONDS); Httpbuilder.writetimeout (1000*60, timeUnit.seconds); // Write timeout httpBuilder.readTimeout(1000*60, timeunit.seconds); MRetrofit = new retrofit.builder ().client(httpBuilder.build()) .addConverterFactory(GsonConverterFactory.create()) .baseUrl(ApiConfig.BASE_URL) .build();Copy the code
4. Define the interface corresponding to the HTTP API:

 public interface ApiService {
 
   @GET("wenda/comments/{questionId}/json") 
   Call<CommentResponse> getWendaList(@Path("questionId") String questionId); 
}
Copy the code

The interface definition above is called the server URL for wanandroid.com/wenda/comme… Return the defined object

Return object definition

public class CommentResponse { private Integer errorCode; private String errorMsg; private CommentBean date; } public class CommentBean { private int total; private int size; private List<CommentData> datas; class CommentData{ private int articleId; private boolean canEdit; private String content; private int id; private String niceDate; private Date publishDate; private int rootCommentId; private int status; private int toUserId; private String toUserName; private int userId; private String userName; private int zan; }}Copy the code
5. Request the interface

The Call object has two request methods: 1.Enqueue asynchronous request method; 2. Synchronous request method Exectue

Public void postAsnHttp(){ApiService = mretrofit.create (ApiService. Class); Call the implemented method to make synchronous or asynchronous HTTP requests: Call<CommentResponse> wendaList = httplist.getwendalist ("14500"); wendaList.enqueue(new Callback<CommentResponse>() { @Override public void onResponse(Call<Map<Object, Object>> call, Response<CommentResponse> response) { Log.i("111111","onResponse:"+response.body().toString()); } @Override public void onFailure(Call<Map<Object, Object>> call, Throwable t) { } }); }} Sync request try {// Sync request Response<CommentResponse> Response = call1.execute();}} Sync request try {// Sync request Response<CommentResponse> Response = call1.execute(); If (response.isSuccessful()) { Log.d("Retrofit2Example", response.body().getDesc()); } } catch (IOException e) { e.printStackTrace(); }Copy the code