Register Activity
package net.samaysoftware.listviewdemo; import android.content.Intent; import android.os.AsyncTask; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import utility.HttpManager; import utility.RequestPackage; public class RegisterActivity extends AppCompatActivity implements View.OnClickListener { EditText etName, etMobile, etEmail, etUsername, etPassword, etAddress, etCity; Button btnRegister; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_register); findAllViews(); btnRegister.setOnClickListener(this); } private void findAllViews() { etName = (EditText)findViewById(R.id.etCName); etEmail = (EditText)findViewById(R.id.etCEmail); etMobile = (EditText)findViewById(R.id.etCMobile); etUsername = (EditText)findViewById(R.id.etCUsername); etPassword = (EditText)findViewById(R.id.etCPassword); etAddress = (EditText)findViewById(R.id.etCAddress); etCity = (EditText)findViewById(R.id.etCCity); btnRegister = (Button) findViewById(R.id.btnRegister); } @Override public void onClick(View view) { new MyTask().execute(); } class MyTask extends AsyncTask<String, String, String>{ String name, email, mobile, address, city, username, password; @Override protected void onPreExecute() { super.onPreExecute(); name = etName.getText().toString(); email = etEmail.getText().toString(); mobile = etMobile.getText().toString(); address = etAddress.getText().toString(); city = etCity.getText().toString(); username = etUsername.getText().toString(); password = etPassword.getText().toString(); } @Override protected String doInBackground(String... strings) { RequestPackage rp = new RequestPackage(); rp.setMethod("GET"); rp.setUri("http://192.168.31.163:49962/MobileApp/SignUp"); rp.setParam("Cname",name); rp.setParam("Email",email); rp.setParam("Mobile",mobile); rp.setParam("Username",username); rp.setParam("Password",password); rp.setParam("City",city); rp.setParam("Address",address); String ans = HttpManager.getData(rp); return ans.trim(); } @Override protected void onPostExecute(String s) { super.onPostExecute(s); if(s.equals("success")){ Toast.makeText(RegisterActivity.this, "Registration is successful !!", Toast.LENGTH_LONG).show(); Intent i = new Intent(RegisterActivity.this, LoginActivityDemo.class); startActivity(i); finish(); } else{ Toast.makeText(RegisterActivity.this, "There seems to be some problem, please try again later !!", Toast.LENGTH_LONG).show(); } } } }
LoginActivity
package net.samaysoftware.listviewdemo; import android.app.ProgressDialog; import android.content.Intent; import android.content.SharedPreferences; import android.os.AsyncTask; import android.preference.PreferenceManager; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import utility.HttpManager; import utility.RequestPackage; public class LoginActivityDemo extends AppCompatActivity implements View.OnClickListener { String un, pw; EditText etUser, etPass; Button btnLogin; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login_demo); etUser = (EditText) findViewById(R.id.etUser); etPass = (EditText) findViewById(R.id.etPass); btnLogin = (Button) findViewById(R.id.btnMyLogin); btnLogin.setOnClickListener(this); /* SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(LoginActivityDemo.this); int oldid = sp.getInt("id", -1000); if(oldid!=-1000){ Intent i = new Intent(LoginActivityDemo.this, CategoryListActivity.class); startActivity(i); finish(); }*/ } @Override public void onClick(View view) { new LoginTask().execute(); } class LoginTask extends AsyncTask<String, String, String>{ ProgressDialog pd; String un, pw; @Override protected void onPreExecute() { super.onPreExecute(); un = etUser.getText().toString(); pw = etPass.getText().toString(); pd = new ProgressDialog(LoginActivityDemo.this); pd.setTitle("Please Wait"); pd.setMessage("Loading"); pd.setIndeterminate(true); pd.setCancelable(false); pd.show(); } @Override protected String doInBackground(String... arr) { RequestPackage rp = new RequestPackage(); rp.setMethod("GET"); rp.setUri("http://192.168.31.163:49962/MobileApp/SignIn"); rp.setParam("Username", un); rp.setParam("Password", pw); String ans = HttpManager.getData(rp); return ans.trim(); } @Override protected void onPostExecute(String ans) { super.onPostExecute(ans); if(pd!=null){ pd.dismiss(); } /* int id; try { id = Integer.parseInt(ans); }catch (Exception ee){ Toast.makeText(LoginActivityDemo.this, "Cannot connect to server", Toast.LENGTH_LONG).show(); return; } if(id==0){ Toast.makeText(LoginActivityDemo.this, "Invalid username or password !!", Toast.LENGTH_LONG).show(); return; } SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(LoginActivityDemo.this); sp.edit().putInt("id", id).apply();*/ if(ans.equals("success")) { Intent i = new Intent(LoginActivityDemo.this, CategoryListActivity.class); startActivity(i); finish(); } else{ Toast.makeText(LoginActivityDemo.this, "Invalid username or password !!", Toast.LENGTH_LONG).show(); } } } }CATEGORY LISTpackage net.samaysoftware.listviewdemo; import android.app.ProgressDialog; import android.content.DialogInterface; import android.content.Intent; import android.os.AsyncTask; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.*; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import java.util.ArrayList; import utility.HttpManager; import utility.RequestPackage; public class CategoryListActivity extends AppCompatActivity implements AdapterView.OnItemClickListener { ListView listView; ArrayList<String> arrlist = new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_country_list); listView = (ListView) findViewById(R.id.lvDynamicCountries); new MyTask().execute(); } @Override public void onItemClick(AdapterView<?> adapterView, View view, int pos, long l) { String catname = arrlist.get(pos); Intent i = new Intent(this, ProductListActivity.class); i.putExtra("catname", catname); startActivity(i); /* AlertDialog.Builder builder1 = new AlertDialog.Builder(this); builder1.setMessage("Are you sure you want to delete"); builder1.setCancelable(true); builder1.setPositiveButton( "Yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.cancel(); } }); builder1.setNegativeButton( "No", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.cancel(); } }); AlertDialog alert11 = builder1.create(); alert11.show();*/ /* AlertDialog.Builder builderSingle = new AlertDialog.Builder(this); builderSingle.setIcon(R.drawable.flagindia); builderSingle.setTitle("Select One Name:"); final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.select_dialog_singlechoice); arrayAdapter.add("Hardik"); arrayAdapter.add("Archit"); arrayAdapter.add("Jignesh"); arrayAdapter.add("Umang"); arrayAdapter.add("Gatti"); builderSingle.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); builderSingle.setAdapter(arrayAdapter, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { String strName = arrayAdapter.getItem(which); AlertDialog.Builder builderInner = new AlertDialog.Builder(CategoryListActivity.this); builderInner.setMessage(strName); builderInner.setTitle("Your Selected Item is"); builderInner.setPositiveButton("Ok", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog,int which) { dialog.dismiss(); } }); builderInner.show(); } }); builderSingle.show(); */ } class MyTask extends AsyncTask<String, String, String>{ ProgressDialog pd = null; @Override protected String doInBackground(String... strings) { RequestPackage rp = new RequestPackage(); rp.setMethod("GET"); rp.setUri("http://192.168.31.163:49962/MobileApp/GetAllCategories"); String ans = HttpManager.getData(rp); return ans.trim(); } @Override protected void onPreExecute() { super.onPreExecute(); pd = new ProgressDialog(CategoryListActivity.this); pd.setIndeterminate(true); pd.setMessage("Loading...."); pd.setCancelable(false); pd.setTitle("Please Wait"); pd.show(); } @Override protected void onPostExecute(String ans) { super.onPostExecute(ans); //arr = ans.split(","); JSONArray arr = null; try { arr = new JSONArray(ans); for(int i = 0; i < arr.length(); i++){ JSONObject obj = arr.getJSONObject(i); String catname = obj.getString("CatName"); arrlist.add(catname); } } catch (JSONException e) { e.printStackTrace(); } ArrayAdapter<String> aa = new ArrayAdapter<>(CategoryListActivity.this, android.R.layout.simple_list_item_1, android.R.id.text1, arrlist); listView.setAdapter(aa); listView.setOnItemClickListener(CategoryListActivity.this); pd.dismiss(); } } }