Use of Android Retrofit

Step one:

 

Dependent package import:

    compile 'com.squareup.retrofit2:retrofit:2.2.0'
    compile 'com.squareup.okhttp3:okhttp:3.4.1'
    compile 'com.squareup.retrofit2:converter-gson:2.0.2'

Network permissions:

<uses-permission android:name="android.permission.INTERNET"/>

 

 

The second step:

Create interface settings request type and parameters

 

New UserInfoModel class and UserMgrService interface

 

 

Common parameter notes:

@GET, @ POST: determine the request mode

@Path: request character substitution for URL

@Query: parameters to pass

@QueryMap: contains multiple @ Query annotation parameters

@Body: add entity class object

@FormUrlEncoded: URL encoding

 

 

Step 3: create Retrofit object and set data parser

Retrofit retrofit = new Retrofit.Builder()
        .baseUrl(Config.MAIN_PATH)
        .addConverterFactory(GsonConverterFactory.create())
        .build();

Step 4: generate interface objects

UserMgrService userMgrService = retrofit.create(UserMgrService.class);

Step 5: Call the interface method to return the Call object

Call<UserInfoModel> call = userMgrService.login("zhangsan", "123456");

Step 6: send request (synchronous, asynchronous)

Synchronization: Call execute() of the Call object to return the response body of the result;

Asynchronous: Call enqueue() of the Call object. The parameter is a callback;

Step 7: process the returned data

 

 

Next is the code:

Config.class
public interface Config {
    String MAIN_PATH = "http://api.XXXXXXXXXX.cn/";
}

 

UserInfoModel.class
public class UserInfoModel {

    public int code;
    public UserInfo data;
    public String message;

    public static class UserInfo{
        public int id;
        public String username;
        public String email;
        public String tel;
    }
}

 

UserMgrService.class
public interface UserMgrService {

    @GET("login.php")
    Call<UserInfoModel> login(@Query("username")String username,@Query("pwd")String pwd);

    @POST("login.php")
    @FormUrlEncoded
    Call<UserInfoModel> register(@Field("username") String username, @Field("pwd") String pwd);

}

 

 

MainActivity.class

 

 //Create a Retrofit object
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Config.MAIN_PATH)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        //Get UserMgrService object
        UserMgrService userMgrService = retrofit.create(UserMgrService.class);
        //Call login method
        Call<UserInfoModel> call = userMgrService.login("zhangsan", "123456");
        //Send request
        call.enqueue(new Callback<UserInfoModel>() {
            @Override
            public void onResponse(Call<UserInfoModel> call, Response<UserInfoModel> response) {
                Log.e("MainActivity", "response.code:" + response.body().code);
            }

            @Override
            public void onFailure(Call<UserInfoModel> call, Throwable t) {
                Log.e("MainActivity", "Throwable:" + t);
            }
        });

 

 

All content from MOOC Video: Retrofit Network Library Thank

 

 

 

 

 

 

 

 

 

Tags: Retrofit network Android PHP

Posted on Sun, 05 Jan 2020 15:59:51 -0800 by razorsedgeuk