Sending JSON in POST request with Retrofit2

后端 未结 4 1484
既然无缘
既然无缘 2020-12-03 11:24

I\'m using Retrofit to integrate my Web services and I do not understand how to send a JSON object to the server using a POST request. I\'m currently stuck, here is my code:

4条回答
  •  隐瞒了意图╮
    2020-12-03 11:56

    Use these in gradle

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

    Use these two POJO class ........

    LoginData.class

    public class LoginData {
    
        private String email;
        private String password;
    
        public LoginData(String email, String password) {
            this.email = email;
            this.password = password;
        }
    
        /**
         *
         * @return
         * The email
         */
        public String getEmail() {
            return email;
        }
    
        /**
         *
         * @param email
         * The email
         */
        public void setEmail(String email) {
            this.email = email;
        }
    
        /**
         *
         * @return
         * The password
         */
        public String getPassword() {
            return password;
        }
    
        /**
         *
         * @param password
         * The password
         */
        public void setPassword(String password) {
            this.password = password;
        }
    
    }
    

    LoginResult.class

    public class LoginResult {
    
        private Boolean error;
        private String message;
        private Integer doctorid;
        private Boolean active;
    
        /**
         *
         * @return
         * The error
         */
        public Boolean getError() {
            return error;
        }
    
        /**
         *
         * @param error
         * The error
         */
        public void setError(Boolean error) {
            this.error = error;
        }
    
        /**
         *
         * @return
         * The message
         */
        public String getMessage() {
            return message;
        }
    
        /**
         *
         * @param message
         * The message
         */
        public void setMessage(String message) {
            this.message = message;
        }
    
        /**
         *
         * @return
         * The doctorid
         */
        public Integer getDoctorid() {
            return doctorid;
        }
    
        /**
         *
         * @param doctorid
         * The doctorid
         */
        public void setDoctorid(Integer doctorid) {
            this.doctorid = doctorid;
        }
    
        /**
         *
         * @return
         * The active
         */
        public Boolean getActive() {
            return active;
        }
    
        /**
         *
         * @param active
         * The active
         */
        public void setActive(Boolean active) {
            this.active = active;
        }
    
    }
    

    Use API like this

    public interface RetrofitInterface {
         @POST("User/DoctorLogin")
            Call getStringScalar(@Body LoginData body);
    }
    

    use call like this ....

    Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("Your domain URL here")
                .addConverterFactory(ScalarsConverterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    
           RetrofitInterface service = retrofit.create(RetrofitInterface .class);
    
     Call call=service.getStringScalar(new LoginData(email,password));
        call.enqueue(new Callback() {
                    @Override
                    public void onResponse(Call call, Response response) { 
                   //response.body() have your LoginResult fields and methods  (example you have to access error then try like this response.body().getError() )
    
                  }
    
                    @Override
                    public void onFailure(Call call, Throwable t) {
               //for getting error in network put here Toast, so get the error on network 
                    }
                });
    

    EDIT:-

    put this inside the success() ....

    if(response.body().getError()){
       Toast.makeText(getBaseContext(),response.body().getMessage(),Toast.LENGTH_SHORT).show();
    
    
    }else {
              //response.body() have your LoginResult fields and methods  (example you have to access error then try like this response.body().getError() )
                    String msg = response.body().getMessage();
                    int docId = response.body().getDoctorid();
                    boolean error = response.body().getError();  
    
                    boolean activie = response.body().getActive()();   
    }
    

    Note :- Always use POJO classes , it remove the JSON data parsing in the retrofit .

提交回复
热议问题