Json_ login
//json login
public class login extends AppCompatActivity {
EditText email, password;
Button login;
InputStream is = null;
JSONObject jobj = null;
String json = "", s1, s2;
public boolean islogin = false;
public static String sharedpref = "MyPref";
public static String adminlogin = "AdminLogin";
public static String adminshare = "AdminPref";
public static String adminemail = "email";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
email = findViewById(R.id.email);
password = findViewById(R.id.password);
login = findViewById(R.id.login);
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
s1 = email.getText().toString();
s2 = password.getText().toString();
LoginTask lt = new LoginTask();
lt.execute();
}
});
}
@Override
protected void onResume() {
super.onResume();
SharedPreferences preferences = getSharedPreferences(sharedpref, MODE_PRIVATE);
islogin = preferences.getBoolean(adminlogin, false);
if (islogin) {
Intent i = new Intent(login.this, afterlogin.class);
startActivity(i);
finish();
}
}
private class LoginTask extends AsyncTask {
String url = "http://192.168.43.34/json/login.php";
@Override
protected Object doInBackground(Object[] objects) {
ArrayList<NameValuePair> list = new ArrayList<>();
list.add(new BasicNameValuePair("email", email.getText().toString()));
list.add(new BasicNameValuePair("password", password.getText().toString()));
DefaultHttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(url);
try {
post.setEntity(new UrlEncodedFormEntity(list));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
try {
HttpResponse response = client.execute(post);
HttpEntity entity = response.getEntity();
is = entity.getContent();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "ISO-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = "";
while ((line = reader.readLine()) != null) {
sb.append(line + "");
}
is.close();
json = sb.toString();
jobj = new JSONObject(json);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return json.trim();
}
@Override
protected void onPostExecute(Object o) {
super.onPostExecute(o);
String s = json.trim();
if (s.equalsIgnoreCase("success")) {
SharedPreferences preferences = getSharedPreferences(sharedpref, MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean(adminlogin, true);
editor.putString(adminshare, s);
editor.putString(adminemail, email.getText().toString());
editor.commit();
Toast.makeText(login.this, "Login is done", Toast.LENGTH_SHORT).show();
Intent i = new Intent(login.this, afterlogin.class);
startActivity(i);
finish();
} else
Toast.makeText(login.this, "Login is not done", Toast.LENGTH_SHORT).show();
}
}
}
public class login extends AppCompatActivity {
EditText email, password;
Button login;
InputStream is = null;
JSONObject jobj = null;
String json = "", s1, s2;
public boolean islogin = false;
public static String sharedpref = "MyPref";
public static String adminlogin = "AdminLogin";
public static String adminshare = "AdminPref";
public static String adminemail = "email";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
email = findViewById(R.id.email);
password = findViewById(R.id.password);
login = findViewById(R.id.login);
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
s1 = email.getText().toString();
s2 = password.getText().toString();
LoginTask lt = new LoginTask();
lt.execute();
}
});
}
@Override
protected void onResume() {
super.onResume();
SharedPreferences preferences = getSharedPreferences(sharedpref, MODE_PRIVATE);
islogin = preferences.getBoolean(adminlogin, false);
if (islogin) {
Intent i = new Intent(login.this, afterlogin.class);
startActivity(i);
finish();
}
}
private class LoginTask extends AsyncTask {
String url = "http://192.168.43.34/json/login.php";
@Override
protected Object doInBackground(Object[] objects) {
ArrayList<NameValuePair> list = new ArrayList<>();
list.add(new BasicNameValuePair("email", email.getText().toString()));
list.add(new BasicNameValuePair("password", password.getText().toString()));
DefaultHttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(url);
try {
post.setEntity(new UrlEncodedFormEntity(list));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
try {
HttpResponse response = client.execute(post);
HttpEntity entity = response.getEntity();
is = entity.getContent();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "ISO-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = "";
while ((line = reader.readLine()) != null) {
sb.append(line + "");
}
is.close();
json = sb.toString();
jobj = new JSONObject(json);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return json.trim();
}
@Override
protected void onPostExecute(Object o) {
super.onPostExecute(o);
String s = json.trim();
if (s.equalsIgnoreCase("success")) {
SharedPreferences preferences = getSharedPreferences(sharedpref, MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean(adminlogin, true);
editor.putString(adminshare, s);
editor.putString(adminemail, email.getText().toString());
editor.commit();
Toast.makeText(login.this, "Login is done", Toast.LENGTH_SHORT).show();
Intent i = new Intent(login.this, afterlogin.class);
startActivity(i);
finish();
} else
Toast.makeText(login.this, "Login is not done", Toast.LENGTH_SHORT).show();
}
}
}
Comments
Post a Comment