Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- EditText Username, Password;
- Button login;
- String username, password;
- String login_url = "http://abc/api/UserLoginctrl";
- AlertDialog.Builder builder;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- builder = new AlertDialog.Builder(MainActivity.this);
- Username = (EditText)findViewById(R.id.editText);
- Password = (EditText)findViewById(R.id.editText2);
- login = (Button)findViewById(R.id.button);
- login.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- username = Username.getText().toString();
- password = Password.getText().toString();
- if(username.equals("") || password.equals(""))
- {
- builder.setTitle("Something went wrong...");
- displayAlert("Enter valid credentials");
- }
- else
- {
- StringRequest stringRequest = new StringRequest(Request.Method.POST, login_url,
- new Response.Listener<String>() {
- @Override
- public void onResponse(String response) {
- try {
- JSONArray jsonArray = new JSONArray(response);
- JSONObject jsonObject = jsonArray.getJSONObject(0);
- String code = jsonObject.getString("code");
- if (code.equals("Login failed"))
- {
- builder.setTitle("Login Error..");
- displayAlert(jsonObject.getString("message"));
- }
- else
- {
- Intent intent = new Intent(MainActivity.this,LoginSuccess.class);
- Bundle bundle = new Bundle();
- bundle.putString("name", jsonObject.getString("name"));
- bundle.putString("email", jsonObject.getString("email"));
- intent.putExtras(bundle);
- startActivity(intent);
- }
- } catch (JSONException e) {
- e.printStackTrace();
- }
- }
- }, new Response.ErrorListener() {
- @Override
- public void onErrorResponse(VolleyError error) {
- Toast.makeText(MainActivity.this,"Erorrrrrr....",Toast.LENGTH_LONG).show();
- error.printStackTrace();
- }
- })
- {
- @Override
- protected Map<String, String> getParams() throws AuthFailureError {
- Map<String, String> params = new HashMap<String, String>();
- params.put("user_name", username);
- params.put("password", password);
- return params;
- }
- };
- RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);
- requestQueue.add(stringRequest);
- }
- }
- });
- }
- public void displayAlert(String message)
- {
- builder.setMessage(message);
- builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialogInterface, int i) {
- Username.setText("");
- Password.setText("");
- }
- });
- AlertDialog alertDialog = builder.create();
- alertDialog.show();
- }
- TextView name, email;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_login_success);
- name = (TextView)findViewById(R.id.name);
- email = (TextView)findViewById(R.id.email);
- Bundle bundle = getIntent().getExtras();
- name.setText("Welcome" + bundle.getString("name"));
- email.setText("Email" + bundle.getString("email"));
- }
- <application
- android:allowBackup="true"
- android:icon="@mipmap/ic_launcher"
- android:label="@string/app_name"
- android:supportsRtl="true"
- android:theme="@style/AppTheme">
- <activity android:name=".MainActivity">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- <activity android:name=".LoginSuccess"></activity>
- </application>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement