Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.examinationportal;
- import android.content.Intent;
- import android.database.Cursor;
- import android.database.sqlite.SQLiteDatabase;
- import android.database.sqlite.SQLiteOpenHelper;
- import android.graphics.Color;
- import android.net.Uri;
- import android.os.AsyncTask;
- import android.provider.Settings;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.support.v7.widget.Toolbar;
- import android.util.Log;
- import android.view.View;
- import android.widget.EditText;
- import android.widget.TextView;
- import android.app.IntentService;
- import android.app.PendingIntent;
- import org.apache.http.NameValuePair;
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.OutputStream;
- import java.io.OutputStreamWriter;
- import java.io.PrintWriter;
- import java.io.UnsupportedEncodingException;
- import java.net.HttpURLConnection;
- import java.net.MalformedURLException;
- import java.net.URL;
- import java.net.URLConnection;
- import java.net.URLEncoder;
- import java.util.ArrayList;
- import java.io.InputStream;
- public class MainActivity extends AppCompatActivity {
- public String message = "";
- SQLiteDatabase myDB = null;
- public String TableName = "Users";
- public String Data;
- public String responseData;
- public String username;
- public String password;
- public String data;
- public String query;
- TextView statusBar, titleLogin;
- EditText un, pw;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- // Get user defined values
- un = (EditText) findViewById(R.id.editTextUsername);//username field
- pw = (EditText) findViewById(R.id.editTextPassword);//password field
- username = un.getText().toString();//convert username and password to string and parse
- password = pw.getText().toString();//them to variables
- //initialize the status bar textview control
- statusBar = (TextView) findViewById(R.id.textViewStatusMsg);
- titleLogin = (TextView) findViewById(R.id.textViewLogin);//title text of this UI
- }
- private class GetText extends AsyncTask<Void, Void, Void> {//} throws UnsupportedEncodingException {
- @Override
- protected Void doInBackground(Void...params){
- BufferedReader reader = null;
- // Send data
- try {
- // Defined URL where to send data
- URL url = new URL("http://www.naomiedify.com/school/test/login2.php");
- //URLConnection con = url.openConnection();
- HttpURLConnection con = (HttpURLConnection) url.openConnection();
- con.setRequestProperty("Accept-Charset", "UTF-8");
- con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
- con.setRequestMethod("POST");
- con.setReadTimeout(10000);
- con.setConnectTimeout(15000);
- //this method returns empty strings
- query = URLEncoder.encode("txtUserName" +username,"UTF-8");
- query += URLEncoder.encode("&txtPassword" +password, "UTF-8");
- query += URLEncoder.encode("&cmbType=Aptitude Test", "UTF-8");
- //this method also returns empty strings
- Uri.Builder builder = new Uri.Builder()
- .appendQueryParameter("txtUserName", username)
- .appendQueryParameter("txtPassword", password)
- .appendQueryParameter("cmbType", "Aptitude Test");
- query = builder.build().getEncodedQuery();
- con.setDoOutput(true);
- OutputStreamWriter wr = new OutputStreamWriter(con.getOutputStream());
- wr.write( query );
- wr.flush();
- wr.close();
- reader = new BufferedReader(new InputStreamReader(con.getInputStream()));
- StringBuilder sb = new StringBuilder();
- String line;
- while((line = reader.readLine()) != null) {
- sb.append(line + "n");
- Log.d("Response: ", "> " + line);
- }
- reader.close();
- responseData = sb.toString();
- }
- catch (MalformedURLException e) {
- e.printStackTrace();
- responseData = e.toString();
- }
- catch (IOException e) {
- e.printStackTrace();
- responseData = e.toString();
- }
- return null;
- }
- @Override
- protected void onPostExecute(Void result){
- statusBar.setText(responseData);
- super.onPostExecute(result);
- }
- }
- public void loginUser(View login) {// Called when the user taps the login button
- Intent intent = new Intent(this, DisplayWelcomeScreen.class);//start the ui
- new GetText().execute();
- try{
- //check the login using the post back data
- if (responseData.equals("User Found.")) {
- message = "Welcome"; //message for successful login
- // Show respsonse on activity
- statusBar.setText( responseData );
- Bundle bundle = new Bundle();//bundle the message and parse it to the next activity
- bundle.putString("dispMsg", message);//bundle the message using the variable dispMsg
- intent.putExtras(bundle);
- startActivity(intent);
- } else {
- message = "Incorrect Username or Password. Try again!";
- statusBar.setTextColor(Color.parseColor("#ff0000"));
- statusBar.setBackgroundColor(Color.parseColor("#d3d3d3"));
- // Show response on activity
- statusBar.setText( responseData + ": Incorrect Username or Password.");
- }
- }
- catch(Exception ex) {
- statusBar.setText("URL Exeption! "+ex);
- }
- }
- }
- <?php
- $dataComingFrom = substr($_SERVER['HTTP_USER_AGENT'], 0, 7);
- //echo $dataComingFrom;
- $con = mysqli_connect($hostname_school,$username_school,"$password_school");//connect to server
- mysqli_select_db($con,$_db);
- if (mysqli_connect_errno()){//if error connecting to server, return error message
- die("Failed to connect to MySQL: " . mysqli_connect_error());
- }else if(!mysqli_select_db($con,$_db)) {//if no error connecting to server, try to select db to see whether it already exists.
- if ($perform = mysqli_query($con,"CREATE DATABASE IF NOT EXISTS $_db")){ //if no db exists on the server, create the db.
- $msg = "Database $_db has been created successfully";
- //echo $msg;
- }else { echo mysqli_errno($con) .": Error creating database: " . mysqli_error($con);}//if it can't create db, return error message
- }else{ //if db was selected, it means the db already exists
- //$data = "You have already created this database!";//return message that db already exists
- //die("$data <br>");//kill the script
- //you can perform the login process here
- //$ip = preg_replace( '#[^0-9.]#', '', getenv( 'REMOTE_ADDR' ) );
- $username = stripslashes( str_replace( "[^A-Z a-z0-9]", "", $_POST[ 'txtUserName' ] ) );
- $plainPass = stripslashes( str_replace( "[^A-Z a-z0-9]", "", $_POST[ 'txtPassword' ] ) );
- $password = strrev(md5($plainPass));
- //run the query to search for the username and password for a match
- //$query = "SELECT * FROM $tbl_name WHERE first_name = '$un' AND password = '$pw'";
- if(empty($username) or empty($plainPass)){die("Empty Strings Parsed.");}
- $user = mysqli_query($con,"SELECT * FROM ap_student WHERE AS_Email ='".$username."' AND AS_Password ='".$password."' AND
- AS_Status ='Confirmed!'") or die("Unable to verify user due to: " . mysqli_error($con));
- //$insert = mysqli_query($con,"UPDATE ap_student SET AS_Password = '".$password."' WHERE AS_Id = 1") or die(mysqli_error($con));
- $result = mysqli_num_rows($user);
- if ($result > 0){$returnedString = "User Found.";}else {$returnedString = "User Not Found";echo $returnedString;}
- }
- ?>
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.example.examinationportal">
- <uses-permission
- android:name="android.permission.INTERNET">
- </uses-permission>
- <application
- android:allowBackup="true"
- android:icon="@mipmap/ic_launcher"
- android:label="@string/app_name"
- android:roundIcon="@mipmap/ic_launcher_round"
- android:supportsRtl="true"
- android:theme="@style/Theme.AppCompat.DayNight.DarkActionBar">
- <activity android:name=".MainActivity">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- </application>
- </manifest>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement