0

Here is my login activity

    import android.os.Bundle;
    import android.view.View;
    import android.widget.EditText;
    import android.widget.Toast;
    
    import androidx.appcompat.app.AppCompatActivity;
    
    import com.example.sti.Models.responsemodel;
    import com.example.sti.Retrofit.ApiClient;
    import com.example.sti.Retrofit.ApiInterface;
    
    import butterknife.BindView;
    import butterknife.ButterKnife;
    import retrofit2.Call;
    import retrofit2.Callback;
    import retrofit2.Response;
    
    public class LoginActivity extends AppCompatActivity {
        ApiInterface apiInterface;
    
        @BindView(R.id.editTextEmail) EditText editTextEmail;
        @BindView(R.id.editTextPassword) EditText editTextPassword;
    
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_login);
            ButterKnife.bind(this);
            apiInterface= ApiClient.getApiClient().create(ApiInterface.class);
    
        }
        public void  loginUser(View view)
        {
            Call<responsemodel> responsemodelCall=apiInterface.loginUser(editTextEmail.getText().toString(),editTextPassword.getText().toString());
    
            responsemodelCall.enqueue(new Callback<responsemodel>() {
                @Override
                public void onResponse(Call<responsemodel> call, Response<responsemodel> response)
                {
                    if (response.body()!=null)
                    {
                        responsemodel responsemodel=response.body();
    
                        if (responsemodel.isresponse_code())
                        {
                            Toast.makeText(LoginActivity.this,"login successful",Toast.LENGTH_LONG).show();
                        }
                        else {
                            Toast.makeText(LoginActivity.this,"user not found",Toast.LENGTH_LONG).show();
    
                        }
                    }
    
                }
    
                @Override
                public void onFailure(Call<responsemodel> call, Throwable t)
                {
                    Toast.makeText(LoginActivity.this,"Error Could Not Connect",Toast.LENGTH_LONG).show();
    
                }
            });
        }
    }

Here is my login api file

<?php
    class Api extends CI_Controller {
            
            public function __construct()
            {
                parent::__construct();
                $this->load->model('admin_model');
                $this->load->helper('url_helper');
                $this->load->library('session');
            }
        
            public function login() {
    
                $login=array(
    
                  'email'=>$this->input->post('email'),
    
                  'password'=>md5($this->input->post('password')),
    
                );
    
                if($login['email']=="" || $login['password']=="")
    
                {
                  $temp["response_code"]="0";
                  $temp["message"]="Enter Data";
                  $temp["status"]="failure";
    
                  echo json_encode($temp);
                }
                else
                {
                    $temp = array();
                    $data=$this->admin_model->login_usera($login['email'],$login['password']);
    
                    if($data)
                    {
                        $temp["response_code"]="1";
                        $temp["message"]="user login success";
                        $temp["status"]="success";
                        $temp["user_id"]=$data['id'];
                        $temp["user_token"]=md5(uniqid(rand(), true));
    
                        echo json_encode($temp);
                  }
                  else
                  {
                     $temp["response_code"]="0";
                     $temp["message"]="user login failure";
                     $temp["status"]="failure";
                     $temp["user_token"]=md5(uniqid(rand(), true));
    
                     echo json_encode($temp);
                  }
                }
            }
    }    

Here is my apiclient.java file:

    public class ApiClient {
        public static final String BASE_URL="https://sparkstoideas.com/student/";  // actual base url is here..
        public static Retrofit retrofit=null;
    
        public  static Retrofit getApiClient(){
            if(retrofit==null){
                Gson gson=new GsonBuilder()
                        .setLenient()
                        .create();
    
                retrofit =new Retrofit.Builder().baseUrl(BASE_URL)
                        .addConverterFactory(GsonConverterFactory.create(gson))
                        .build();
            }
            return retrofit;
        }
    }

This is the apiinterface.java file:

    public interface ApiInterface
    {
        @FormUrlEncoded
        @POST("api/login")  //next url upon base url to continue..
        Call<responsemodel> loginUser(@Field("email")String email,
                                      @Field("password")String password);
    
    }

And here is responsemodel of the api:

    import com.google.gson.annotations.Expose;
    import com.google.gson.annotations.SerializedName;
    
    public class responsemodel {
        @Expose
        @SerializedName("email")
        private String email;
    
        @Expose
        @SerializedName("password")
        private String password;
    
        @Expose
        @SerializedName("response_code")
        private boolean response_code;
    
        @Expose
        @SerializedName("message")
        private String message;
    
        @Expose
        @SerializedName("status")
        private String status;
    
        @Expose
        @SerializedName("user_id")
        private int user_id;
    
        @Expose
        @SerializedName("user_token")
        private String user_token;
    
        public String getEmail() {
            return email;
        }
    
        public void setEmail(String email) {
            this.email = email;
        }
    
        public String getPassword() {
            return password;
        }
    
        public void setPassword(String password) {
            this.password = password;
        }
    
        public boolean isresponse_code() {
            return response_code;
        }
    
        public void setresponse_code(boolean response_code) {
            this.response_code = response_code;
        }
    
        public String getMessage() {
            return message;
        }
    
        public void setMessage(String message) {
            this.message = message;
        }
    
        public String getStatus() {
            return status;
        }
    
        public void setStatus(String status) {
            this.status = status;
        }
    
        public int getUser_id() {
            return user_id;
        }
    
        public void setUser_id(int user_id) {
            this.user_id = user_id;
        }
    
        public String getUser_token() {
            return user_token;
        }
    
        public void setUser_token(String user_token) {
            this.user_token = user_token;
        }
    }

I am a total beginner in android please help me why is not working..

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • have you debugged your code? – Stultuske Jul 29 '21 at 06:20
  • @Stultuske yes i debug my code still is not working for me anything any idea about this question.please teach me how to solve this. – sparksshubham Jul 29 '21 at 06:24
  • @sparksshubham can you attach the response you get from the server and how isrespose_code() method work? – Bhavin Jul 29 '21 at 06:45
  • obviously debugging isn't going to solve your issue, it's supposed to help you understand where the problem is. What did you find out? – Stultuske Jul 29 '21 at 07:05
  • @Bhavin i did`t understand how to check response form server so if i give my api key you will be help me to find the response form server here is my api key "https://sparkstoideas.com/student/api/login" – sparksshubham Jul 29 '21 at 07:08
  • for checking response you can debug your app else use HttpLoggingInterceptor as shown in following [link](https://stackoverflow.com/a/33256827/10429259). and as i can check the api, your response_code is not being true try to check the response and debug app and check what ResponseModel object values are. – Bhavin Jul 29 '21 at 07:20

0 Answers0