Retrofit Login Page 2020


Dependency

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

private void Login() {

    String device_key = Settings.Secure.getString(activity.getApplication().getContentResolver(), Settings.Secure.ANDROID_ID);

    // String device_key = Constant.FIREBASE_TOKEN;    Log.e("Device Key=", device_key);

    progressDialog.setTitle(getResources().getString(R.string.please_wait));
    progressDialog.setMessage(getResources().getString(R.string.loading));
    progressDialog.show();

    ApiInterface apiService = ApiClient.getClient(activity).create(ApiInterface.class);
    Call<JsonObject> call = apiService.Login(Constant.ACCESSKEY, name, pass, device_key);
    call.enqueue(new Callback<JsonObject>() {
        @Override        public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
            progressDialog.dismiss();
            try {
                JSONObject object = new JSONObject(response.body().toString());
                Log.e("Login response==", response.body().toString());
                if (object.getString("Status").equalsIgnoreCase("1")) {
                    loginPreferences.setIsLogin(true);
                    loginPreferences.SetSession(object.getString("Customer_Id"),
                            object.getString("Customer_Name"),
                            object.getString("Email_Id"),
                            object.getString("Mobile_No"),
                            object.getString("Image"));
                    Toast.makeText(activity, object.getString("Message"), Toast.LENGTH_SHORT).show();

                    finish();
                    startActivity(new Intent(activity, MainActivity.class));
                } else {
                    Toast.makeText(activity, object.getString("Message").toString(), Toast.LENGTH_SHORT).show();
                }

            } catch (JSONException e) {
                e.printStackTrace();
            }

        }

        @Override        public void onFailure(Call<JsonObject> call, Throwable t) {
            // Log error here since request failed            progressDialog.dismiss();
            Log.e("NETWORK ERROR --> ", t.toString());
        }
    });
}



public class ApiClient {

    public static String BASE_URL = "https://myshoplist.com/Neomi_Fashion/";
    private static Retrofit retrofit = null;


    public static Retrofit getClient(Context context) {
        if (retrofit==null) {

            HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
            interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);


            OkHttpClient httpClient = new OkHttpClient.Builder()
                    .connectTimeout(30, TimeUnit.SECONDS)
                    .writeTimeout(30, TimeUnit.SECONDS)
                    .readTimeout(30, TimeUnit.SECONDS)
                    .addInterceptor(interceptor)
                    .build();

            Gson gson = new GsonBuilder()
                    .setLenient()
                    .create();


            retrofit = new Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create(gson))
                    .client(httpClient)
                    .build();
        }
        return retrofit;
    }
}

public interface ApiInterface {

@FormUrlEncoded@POST("customerlogin")
Call<JsonObject> Login(@Field("AccessKey") String key,
                       @Field("Mobile_No") String Mobile_No,
                       @Field("Password") String Password,
                       @Field("Device_Key") String DeviceKey);

}

XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:layout_gravity="center"    android:gravity="center"    android:orientation="vertical"    tools:context=".Activity.LoginActivity">

    <ImageView        android:layout_width="wrap_content"        android:layout_height="100dp"        android:layout_gravity="center"        android:layout_marginBottom="20dp"        android:contentDescription="@string/todo"        android:src="@drawable/ic_logo" />

    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_marginLeft="10dp"        android:layout_marginTop="20dp"        android:layout_marginRight="10dp"        android:orientation="vertical">


        <com.google.android.material.textfield.TextInputLayout            android:layout_width="match_parent"            android:layout_height="match_parent">

            <com.google.android.material.textfield.TextInputEditText                android:id="@+id/etEmailMobile"                style="@style/customfontstyle1"                android:layout_width="match_parent"                android:layout_height="wrap_content"                android:hint="@string/login_hint_mobile_or_email"                android:inputType="number"                android:paddingTop="5dp"                android:textColor="@color/colorBlack" />
        </com.google.android.material.textfield.TextInputLayout>



            <com.google.android.material.textfield.TextInputLayout                android:layout_width="match_parent"                android:layout_height="match_parent"                android:layout_marginTop="20dp"                app:passwordToggleEnabled="true"                app:passwordToggleTint="@color/colorBlack">

                <com.google.android.material.textfield.TextInputEditText                    android:id="@+id/etPassword"                    style="@style/customfontstyle1"                    android:layout_width="match_parent"                    android:layout_height="wrap_content"                    android:hint="@string/login_hint_password"                    android:inputType="textPassword"                    android:paddingTop="5dp"                    android:textColor="@color/colorBlack" />
            </com.google.android.material.textfield.TextInputLayout>



    </LinearLayout>

    <TextView        android:id="@+id/btnLogin"        style="@style/customfontstyle"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_marginLeft="10dp"        android:layout_marginTop="30dp"        android:layout_marginRight="10dp"        android:background="@drawable/bg_fill_pink"        android:gravity="center"        android:padding="14dp"        android:text="@string/login_btn_log_in"        android:textColor="@android:color/white" />

    <TextView        android:id="@+id/btnSignUp"        style="@style/customfontstyle"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_marginLeft="10dp"        android:layout_marginTop="25dp"        android:layout_marginRight="10dp"        android:background="@drawable/bg_reg_simple"        android:gravity="center"        android:padding="14dp"        android:text="@string/login_btn_sign_up"        android:textColor="@color/colorPink" />

    <TextView        android:id="@+id/btnForgotPassword"        style="@style/customfontstyle"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center"        android:layout_marginTop="25dp"        android:padding="5dp"        android:text="@string/login_btn_forgot_password"        android:textColor="@color/colorPink" />

    <LinearLayout        android:id="@+id/lnr_skip"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_gravity="center|bottom"        android:layout_marginTop="20dp"        android:layout_marginBottom="20dp"        android:gravity="center|bottom"        android:orientation="horizontal">

        <TextView            style="@style/customfontstyle"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:gravity="center"            android:text="@string/skip_now"            android:textColor="@color/colorBlack"            android:textSize="14sp" />

        <ImageView            android:layout_width="15dp"            android:layout_height="15dp"            android:layout_gravity="center"            android:layout_marginLeft="5dp"            android:layout_marginTop="2dp"            android:src="@drawable/ic_next_double"            android:tint="@color/colorBlack" />

    </LinearLayout>

</LinearLayout>

Comments

Popular posts from this blog

retrofil gjstatus lanuage

form object

Login Preference in android create class