Understanding of Retrofit
Retrofit is a RESTful web request framework wrapped around OkHttp
Retrofit is a package made on the basis of OKHTTP, which can be used directly in the project.
The difference between Retrofit and Retrofit 2.0:
Retrofit is a RESTful encapsulation of HTTP web request framework, focusing on interface encapsulation.
Retrofit 2.0 starts with built-in OkHttp, focusing on the efficiency of web requests.
How to request a network through Retrofit: The Retrofit interface layer is used to encapsulate the request parameters, headers, URLS, and other information, and OkHttp completes the subsequent operation of the request. After the server returns the data, OkHttp passes the original results to Retrofit, which parses the results according to the user’s requirements.
Retrofit is a RESTful encapsulation of the HTTP web request framework. The work of the web request is essentially done by OkHttp, and Retrofit is only responsible for the encapsulation of the web request interface.
Retrofit Framework Network request flowchart:
1. Integration with Android
- Add dependencies (in the build.gradle file)
Build. Gradle: The first is to add retrofit, the second and third are to add JSON parsing, in this case is 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
- Add the OkHttp configuration
// Create OKHttpClient okHttpClient.builder Builder = new okHttpClient.builder (); builder.connectTimeout(1000*60, TimeUnit.SECONDS); Builder.writetimeout (1000*60, timeunit.seconds); Build.readtimeout (1000*60, timeunit.seconds); //* Build.readTimeout (1000*60, timeunit.seconds); //* Read timeout *Copy the code
- 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); HttpBuilder. ReadTimeout (1000*60, timeUnit.seconds); // Create Retrofit mRetrofit = new Retrofit.builder ().client(httpBuilder.build()) .addConverterFactory(GsonConverterFactory.create()) .baseUrl(ApiConfig.BASE_URL) .build();Copy the code
- Define the HTTP API corresponding to the interface:
public interface ApiService {
@GET("wenda/comments/{questionId}/json")
Call<Map<Object,Object>> getWendaList(@Path("questionId") String questionId);
Copy the code
The interface definition above is called the server URL for wanandroid.com/wenda/comme… Problem ID /json needs to return a defined object!
- Request interface
The Call object has two request methods:
1.Enqueue Asynchronous request method
2.Exectue Synchronous request method
Asynchronous request:
public void postAsnHttp(){ ApiService httpList = mRetrofit.create(ApiService.class); Call the implementation method to make synchronous or asynchronous HTTP requests: Call<Map<Object, Object>> wendaList = Httplist.getwendalist ("14500"); wendaList.enqueue(new Callback<Map<Object, Object>>() { @Override public void onResponse(Call<Map<Object, Object>> call, Response<Map<Object, Object>> response) { Log.i("111111","onResponse:"+response.body().toString()); } @Override public void onFailure(Call<Map<Object, Object>> call, Throwable t) { } }); }}Copy the code
A synchronous request
Try {// Synchronize request Response<Map<Object, Object>> Response = call1.execute(); If (response.isSuccessful()) { Log.d("Retrofit2Example", response.body().getDesc()); } } catch (IOException e) { e.printStackTrace(); }Copy the code