September 6,2016
Today i have worked on the coding of login page activity i.e. the main part.
void showDialog(){
dia=new ProgressDialog(this);
dia.setTitle("Loading");
dia.show();
}
public void loginButton(View v){
String usrnm=ed_usrnm.getText().toString();
String pwd=ed_pwd.getText().toString();
if(valid_pwd() && valid_usrnm()){
FetchData f=new FetchData();
f.execute(ed_usrnm.getText().toString(),ed_pwd.getText().toString());
}
}
public boolean valid_usrnm(){
if(ed_usrnm.equals(null)){
ed_usrnm.setError("Enter username!!");
return false;
}
else{
return true;
}
}
public boolean valid_pwd(){
if(ed_pwd.length()==0){
ed_pwd.setError("Enter Password!! ");
return false;
}
else{
return true;
}
}
class FetchData extends AsyncTask<String,Integer,Boolean>{
@Override
protected void onPreExecute() {
super.onPreExecute();
showDialog();
}
@Override
protected Boolean doInBackground(String... params) {
ArrayList<NameValuePair> list= new ArrayList<>();
list.add(new BasicNameValuePair("Username",params[0]));
list.add(new BasicNameValuePair("Password",params[1]));
try{
HttpClient client=new DefaultHttpClient();
HttpPost post=new HttpPost(getString(R.string.path)+"login.php");
post.setEntity(new UrlEncodedFormEntity(list));
HttpResponse r=client.execute(post);
HttpEntity e=r.getEntity();
InputStream is=e.getContent();
BufferedReader bf=new BufferedReader(new InputStreamReader(is));
StringBuilder b=new StringBuilder();
String line;
while ((line=bf.readLine())!=null){
b.append(line);
}
String result=b.toString();
Log.d("result",result);
JSONObject jsonObject=new JSONObject(result);
resp=jsonObject.getString("response");
if(resp.equalsIgnoreCase("ok")) {
Type = jsonObject.getString("type");
if (Type.equalsIgnoreCase("Student")) {
Name = jsonObject.getString("name");
Rollno = jsonObject.getString("roll");
Stream=jsonObject.getString("stream");
Semester=jsonObject.getString("semester");
} else {
Tchr_name = jsonObject.getString("tch_name");
Dept = jsonObject.getString("dept");
}
return true;
}
}
catch(Exception e){
e.printStackTrace();
}
return false;
}
@Override
protected void onPostExecute(Boolean aBoolean) {
if(aBoolean==true){
SharedPreferences sh=getSharedPreferences("preference_ht", Context.MODE_PRIVATE);
SharedPreferences.Editor editor=sh.edit();
if(Type.equalsIgnoreCase("Student")){
editor.putString("name_pref",Name);
editor.putString("roll_pref",Rollno);
editor.putString("stream_pref",Stream);
editor.putString("sem_pref",Semester);
editor.apply();
Intent i=new Intent(Login.this,Navigation.class);
startActivity(i);
}
else{
editor.putString("tch_name_pref",Tchr_name);
editor.putString("dept_pref",Dept);
editor.apply();
Intent i=new Intent(Login.this,TchrNav.class);
startActivity(i);
}
}
super.onPostExecute(aBoolean);
}
}