Java Volley Package Post Request
package com.example.android.ui;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;
public class LoginActivity extends AppCompatActivity {
ImageView show_pass_btn;
private EditText inputUserid,inputPassword;
private Button loginBtn;
private ISharedPreference sharedPreference;
LinearLayout NoInternetlayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
MainViewModel viewModel = new MainViewModel(getApplication(), LoginActivity.this, LoginActivity.this);
viewModel.observeLiveSimState().observe(this, value -> {
if (value) {
Utility.customeToastRedBottom("sim card is available.",getApplicationContext());
} else {
Utility.customeToastRedBottom("Please Insert a sim card to proceed.",getApplicationContext());
System.exit(0);
}
});
sharedPreference = new SecureSharedPref(this, "UserSession");
inputUserid = findViewById(R.id.inputUserid);
show_pass_btn = findViewById(R.id.show_pass_btn);
show_pass_btn.setOnClickListener(view -> ShowHidePass());
inputPassword = findViewById(R.id.inputPassword);
inputPassword.addTextChangedListener(PasswordTextWatcher);
TextView btnForget = findViewById(R.id.btnForget);
btnForget.setOnClickListener(view -> MoveToForgetPassword());
loginBtn = findViewById(R.id.loginBtn);
inputUserid.addTextChangedListener(textWatcher);
NoInternetlayout = findViewById(R.id.NoInternetlayout);
loginBtn.setOnClickListener(view -> {
if (checkinternet2()) {
Auth();
} else {
Utility.customeToastRedBottom("Please check your internet connection", getApplicationContext());
}
});
}
@Override
protected void onStart() {
super.onStart();
String Username = sharedPreference.get("UserId");
String Password = sharedPreference.get("userPass");
if(!Username.equals("null") && !Password.equals("null")){
Intent i = new Intent(LoginActivity.this, MainActivity.class);
startActivity(i); finish();
}
}
private final TextWatcher textWatcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
@Override
public void onTextChanged(CharSequence charSequence, int start, int count, int after) {
String username = inputUserid.getText().toString().trim();
String NewPasswordtext = inputPassword.getText().toString().trim();
loginBtn.setEnabled(!username.isEmpty() && inputUserid.length() > 5 && !NewPasswordtext.isEmpty() && NewPasswordtext.length() > 5);
}
@Override
public void afterTextChanged(Editable s) {
}
};
private final TextWatcher PasswordTextWatcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence charSequence, int start, int count, int after) {
String username = inputUserid.getText().toString().trim();
String NewPasswordtext = inputPassword.getText().toString().trim();
loginBtn.setEnabled(!username.isEmpty() && inputUserid.length() > 5 && !NewPasswordtext.isEmpty() && NewPasswordtext.length() > 5);
}
@Override
public void afterTextChanged(Editable s) {
}
};
public void ShowHidePass() {
EditText inputPassword = findViewById(R.id.inputPassword);
if(inputPassword.getTransformationMethod().equals(PasswordTransformationMethod.getInstance())){
show_pass_btn.setImageResource(R.drawable.ic_baseline_close_24);
//Show Password
inputPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
}
else{
show_pass_btn.setImageResource(R.drawable.ic_baseline_remove_red_eye_24);
//Hide Password
inputPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
}
}
public void Auth() {
NoInternetlayout.setVisibility(View.VISIBLE);
String useridText = String.valueOf(inputUserid.getText());
String passwordText = String.valueOf(inputPassword.getText());
Api api = new Api();
String url = api.getAuthUrl();
String encryption_key = api.getEncryption_key();
JSONObject request = new JSONObject();
try {
String encryption_key2 = encryption_key;
String useridText2 = useridText;
String passwordText2 = passwordText;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
encryption_key2 = Java_AES_Cipher.encrypt("3625142536154524","3625142536154524",encryption_key);
useridText2 = Java_AES_Cipher.encrypt("3625142536154524","3625142536154524",useridText);
passwordText2 = Java_AES_Cipher.encrypt("3625142536154524","3625142536154524",passwordText);
}
request.put("encryption_key", encryption_key2);
request.put("userid", useridText2);
request.put("password", passwordText2);
} catch (JSONException e) {
e.printStackTrace();
Log.d("printStackTrace ram", "e: "+e);
}
RequestQueue requestQueue;
requestQueue = Volley.newRequestQueue(this);
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST,
"https://api.example.com/login",
request,
response -> {
Log.d("Shubham", "onResponse: "+response);
NoInternetlayout.setVisibility(View.INVISIBLE);
}, error -> {
Log.d("Shubham", "Something went wrong");
Toast.makeText(LoginActivity.this, "Something went wrong!", Toast.LENGTH_SHORT).show();
NoInternetlayout.setVisibility(View.INVISIBLE);
});
requestQueue.add(jsonObjectRequest);
RequestQueue requestQueue;
requestQueue = Volley.newRequestQueue(LoginActivity.this);
StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
response -> {
try {
JSONObject jsonObject = new JSONObject(response);
String result = jsonObject.getString("status");
String status = jsonObject.getString("msg");
if(result.equals("1")){
String Id = jsonObject.getString("id");
String ProfilePic = jsonObject.getString("imageUrl");
String Name = jsonObject.getString("name");
String mobileNo = jsonObject.getString("mobile_no");
String UserId = jsonObject.getString("username");
String userPass = jsonObject.getString("userpass");
String CSupport = jsonObject.getString("customer_support");
String Status2 = jsonObject.getString("status2");
// Storing data into SharedPreferences
sharedPreference.put("Id", Id);
sharedPreference.put("ProfilePic", ProfilePic);
sharedPreference.put("Name", Name);
sharedPreference.put("mobileNo", mobileNo);
sharedPreference.put("UserId", UserId);
sharedPreference.put("userPass", userPass);
sharedPreference.put("CSupport", CSupport);
sharedPreference.put("Status", Status2);
// This is Message of Registered User
Utility.customeToastGreenBottom(status, getApplicationContext());
Intent i = new Intent(LoginActivity.this, MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
finish();
}
if (result.equals("2")){
NoInternetlayout.setVisibility(View.INVISIBLE);
Utility.customeToastRedBottom(status, getApplicationContext());
}
if (result.equals("3")){
NoInternetlayout.setVisibility(View.INVISIBLE);
Utility.customeToastRedBottom(status, getApplicationContext());
}
} catch (JSONException e) {
e.printStackTrace();
NoInternetlayout.setVisibility(View.INVISIBLE);
Utility.customeToastRedBottom("Something Went Wrong.", getApplicationContext());
}
},
error -> {
NoInternetlayout.setVisibility(View.INVISIBLE);
Utility.customeToastRedBottom("Something Went Wrong.", getApplicationContext());
}
){
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<>();
params.put("encryption_key", encryption_key);
params.put("UserId", useridText);
params.put("UserPassword", passwordText);
return params;
}
};
requestQueue.add(stringRequest);
}
private void MoveToForgetPassword() {
Intent i = new Intent(LoginActivity.this, ForgetPassword.class);
overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
startActivity(i);
}
@Override
public void finish() {
super.finish();
overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right);
}
public boolean checkinternet2(){
ConnectivityManager connectivityManager= (ConnectivityManager) getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo wifi = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
NetworkInfo mobile = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (wifi.isConnected()) {
return true;
} else return mobile.isConnected();
}
}
Comments
No comments yet.
Add Comment