Retrofit: basic use of 2.2.0

retrofit:2.2.0 framework has powerful functions and excellent encapsulation in network loading. Today we will learn about its basic usage

1. Import dependency:

Here, we also import the excellent framework of gson

    implementation 'com.squareup.retrofit2:retrofit:2.2.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.2.0'

2. Initialize the Retrofit object

//Obviously, we can see that the framework of Retrofit adopts the builder pattern
Retrofit retrofit = new Retrofit.Builder()
        .baseUrl(Contant.BASE_URL)
        .addConverterFactory(GsonConverterFactory.create())
        .build()
/**
 *It is necessary to explain the base uurl. The string represents the top level of the server connection
 * For example: 
 * https://mp.csdn.net/mdeditor/a.png
 * https://mp.csdn.net/mdeditor/c.png
 * https://mp.csdn.net/mdeditor/mm/.png
 * https://mp.csdn.net/mdeditor/yh.png
 * https://mp.csdn.net/mdeditor/my.png
 * name The BASE_URL here is https://mp.csdn.net/mdeditor/
 * It can be understood as root url
  */        

3. Develop network request entity

    responseInfoAPI = retrofit.create(ResponseInfoAPI.class)
     /**
       * Here, we need to focus on the responseinfo API, which is named by itself. It doesn't matter what it is called. It's mainly about the contents
       */

We need to rewrite an interface

public interface ResponseInfoAPI {
    //Specify request method
    //Full link address requested
    //Request method, request parameter
    //Requested results
    //http://10.0.2.2:8080/TakeoutServiceVersion2

    @GET(Contant.HOME_URL)
    Call<ResponseInfo> getHomeInfo(@Query("latitude") String latitude, @Query("longitude") String longitude);
}
 /**
   * 1,Many people are confused when they see it. Readers can first see it as a common interface
   * getHomeInfo(String latitude,String longitude)
   * 2,Next, @ get (contact. Home? URL) means to pass in the home? URL connection. Note that the home? URL here will be spliced behind the previous base? URL to form a complete URL link. In the form of get
   * @GET(Contant.HOME_URL)
   * getHomeInfo(String latitude,String longitude)
   * 3,The complete url is formed, but relevant parameters need to be passed in. Here, the parameters are string latitude and string longitude. In retrofit, a flag is used to represent the parameters passed in get, namely @ Query("latitude")
   * 4,Last with a return value
   * @GET(Contant.HOME_URL)
   * Call<ResponseInfo> getHomeInfo(@Query("latitude") String latitude, @Query("longitude") String 
   * 5,The return value here is defined by you, but it must correspond to your data
   */

3. Trigger call

public class HomePresenter extends BasePresenter {
    @Override
    protected void showError(String message) {

    }

    @Override
    protected void parseJson(String json) {
        //Parse JSON here
        Gson gson = new Gson();
        gson.fromJson(json, HomeInfo.class)
    }
    //Trigger network request
    public void getHomeData(String lat,String lon){
        Call<ResponseInfo> homeInfo = responseInfoAPI.getHomeInfo(lat, lon);
        //Trigger successful or failed callback methods in callback
        homeInfo.enqueue(new CallBackAdapter());
    }
}

Once the getHomeData method is called, the network request will be executed. At this time, this class implements the previous class and needs to implement two callback methods, so data will come out

Keywords: Retrofit network JSON

Added by totof06 on Mon, 06 Jan 2020 06:05:24 +0200