After creating Login and Register i plan to Intergrate the page with API calls,
So started learn how to do API calls and parse the response given by the server.
BTW, I used FindMyFriend API here !
Here's the Code
package com.example.allaudeen.sa; import android.app.Dialog; import android.content.DialogInterface; import android.service.voice.VoiceInteractionSession; import android.support.v4.content.Loader; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.Window; import android.widget.EditText; import android.widget.ProgressBar; import android.widget.TextView; import android.widget.Toast; import com.android.volley.Request; import com.android.volley.RequestQueue; 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 com.android.volley.Response; import org.json.JSONException; import org.json.JSONObject; import java.lang.reflect.Method; import java.util.HashMap; import java.util.Map; import java.lang.String; public class LoginActivity extends AppCompatActivity { private final String TAG = LoginActivity.class.getSimpleName(); public final String baseUrl = "https://findmyfriend-app.herokuapp.com"; //public final String baseUrl = "http://localhost:3000"; public final String registerUser = baseUrl+"/users/registerUser"; public final String loginUser = baseUrl+"/users/loginUser"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); //Dialogue } public void showDashboard(View v) { Log.d("Log","Clicked Login Button"); setContentView(R.layout.activity_register); } public void showLogin(View v) { Log.d("Log","Clicked Login Button"); setContentView(R.layout.activity_login); } public void doLogout(View v) { Toast.makeText(getApplicationContext(), "Loggedout Successfully", Toast.LENGTH_LONG).show(); setContentView(R.layout.activity_login); } public void doLogin(View v) { //Email EditText emailTxt = (EditText)findViewById(R.id.email); final String email = emailTxt.getText().toString(); //Password EditText passwordTxt = (EditText)findViewById(R.id.password); final String password = passwordTxt.getText().toString(); RequestQueue queue = Volley.newRequestQueue(this); StringRequest postRequest = new StringRequest(Request.Method.POST, loginUser, new Response.Listener<String>() { @Override public void onResponse(String response) { try { JSONObject mainObject = new JSONObject(response); Integer successVal = mainObject.getInt("success"); if(successVal==0) { JSONObject mainObjects = new JSONObject(response); String successMsg = mainObjects.getString("message"); Toast.makeText(getApplicationContext(), successMsg, Toast.LENGTH_LONG).show(); } else { Toast.makeText(getApplicationContext(), "LoggedIn Succesfully", Toast.LENGTH_LONG).show(); setContentView(R.layout.activity_dashboard); } } catch (JSONException e) { Log.e("MYAPP", "unexpected JSON exception", e); } } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { // error Log.d("Error.Response", error.toString()); } } ) { @Override protected Map<String, String> getParams() { Map<String, String> params = new HashMap<String, String>(); params.put("email", email); params.put("password", password); return params; } }; queue.add(postRequest); } public void doRegister(View v) { //Full Name EditText fullNameTxt = (EditText)findViewById(R.id.fullName); final String fullName = fullNameTxt.getText().toString(); //Email EditText emailTxt = (EditText)findViewById(R.id.email); final String email = emailTxt.getText().toString(); //Password EditText passwordTxt = (EditText)findViewById(R.id.password); final String password = passwordTxt.getText().toString(); final TextView mTextView = (TextView) findViewById(R.id.text); RequestQueue queue = Volley.newRequestQueue(this); StringRequest postRequest = new StringRequest(Request.Method.POST, registerUser, new Response.Listener<String>() { @Override public void onResponse(String response) { try { JSONObject mainObject = new JSONObject(response); Integer successVal = mainObject.getInt("success"); if(successVal==0) { JSONObject mainObjecta = new JSONObject(response); String successMsg = mainObjecta.getString("message"); Toast.makeText(getApplicationContext(), successMsg, Toast.LENGTH_LONG).show(); } else { Toast.makeText(getApplicationContext(), "Registered Succesfully", Toast.LENGTH_LONG).show(); setContentView(R.layout.activity_login); } } catch (JSONException e) { Log.e("MYAPP", "unexpected JSON exception", e); } } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { // error Log.d("Error.Response..->", error.toString()); } } ) { @Override protected Map<String, String> getParams() { Map<String, String> params = new HashMap<String, String>(); params.put("email", email); params.put("password", password); params.put("name", fullName); return params; } }; queue.add(postRequest); } }