Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.abc.intech1;
- import android.app.Activity;
- import android.app.ProgressDialog;
- import android.content.Intent;
- import android.net.Uri;
- import android.os.AsyncTask;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.TextView;
- import android.widget.Toast;
- import org.json.JSONException;
- import org.json.JSONObject;
- /**
- * Created by User on 3/7/2018.
- */
- public class loginactivity extends Activity {
- //btnlogin here is an object (same name as button id from layout)
- Button btnlogin;
- EditText tusername, tpassword;
- TextView msg;
- String username, password;
- ProgressDialog pDialog;
- JSONParser jsonParser = new JSONParser();
- private static final String url_login = "http://10.206.1.70/xamarin/loginprocess.php";
- public void onCreate(Bundle b) {
- super.onCreate(b);
- setContentView(R.layout.loginlayout);
- //mapping
- btnlogin = findViewById(R.id.btnlogin);
- tusername = findViewById(R.id.tusername);
- tpassword = findViewById(R.id.tpassword);
- msg = findViewById(R.id.msg);
- btnlogin.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- btnlogin.setText("clicked");
- username = tusername.getText().toString();
- password = tpassword.getText().toString();
- if (username.length() == 0 || password.length() == 0) {
- msg.setText("Username or password is missing");
- }
- else {
- Intent menuintent = new Intent(getApplicationContext(), menuactivity.class);
- }
- }
- });
- }
- class getlogindetails extends AsyncTask<String, String, String>
- {
- protected void onPreExecute()
- {
- super.onPreExecute();
- pDialog = new ProgressDialog(loginactivity.this);
- pDialog.setMessage("Logging in. Please wait...");
- pDialog.setIndeterminate(false);
- pDialog.setCancelable(true);
- pDialog.show();
- }
- protected String doInBackground(String... params)
- {
- int success;
- try
- {
- Uri.Builder builder = new Uri.Builder()
- .appendQueryParameter("username", username)
- .appendQueryParameter("password", password);
- String query = builder.build().getEncodedQuery();
- JSONObject json = jsonParser.makeHttpRequest(url_login, query);
- if(json !=null)
- {
- Log.d("Login", json.toString());
- success = json.getInt("result");
- if (success == 1)
- {
- Intent myintent = new Intent(getApplicationContext(), menuactivity.class);
- startActivity(myintent);
- }
- else
- {
- loginactivity.this.runOnUiThread(new Runnable()
- {
- public void run()
- { //toast similar to alert in javascript
- Toast.makeText(getApplicationContext(), "Not Found", Toast.LENGTH_LONG).show();
- }
- });
- }
- }
- else
- {
- loginactivity.this.runOnUiThread(new Runnable()
- {
- public void run()
- {
- Toast.makeText(loginactivity.this,"Unable to contact server",Toast.LENGTH_LONG).show();
- }
- });
- return null;
- }
- }
- catch (JSONException e)
- {
- e.printStackTrace();
- }
- return null;
- }
- protected void onPostExecute(String file_url)
- {
- pDialog.dismiss();
- }
- }
- }
Add Comment
Please, Sign In to add comment