Advertisement
Guest User

Untitled

a guest
Apr 18th, 2017
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.93 KB | None | 0 0
  1. package com.example.examinationportal;
  2. import android.content.Intent;
  3. import android.database.Cursor;
  4. import android.database.sqlite.SQLiteDatabase;
  5. import android.database.sqlite.SQLiteOpenHelper;
  6. import android.graphics.Color;
  7. import android.net.Uri;
  8. import android.os.AsyncTask;
  9. import android.provider.Settings;
  10. import android.support.v7.app.AppCompatActivity;
  11. import android.os.Bundle;
  12. import android.support.v7.widget.Toolbar;
  13. import android.util.Log;
  14. import android.view.View;
  15. import android.widget.EditText;
  16. import android.widget.TextView;
  17. import android.app.IntentService;
  18. import android.app.PendingIntent;
  19. import org.apache.http.NameValuePair;
  20. import java.io.BufferedReader;
  21. import java.io.BufferedWriter;
  22. import java.io.IOException;
  23. import java.io.InputStreamReader;
  24. import java.io.OutputStream;
  25. import java.io.OutputStreamWriter;
  26. import java.io.PrintWriter;
  27. import java.io.UnsupportedEncodingException;
  28. import java.net.HttpURLConnection;
  29. import java.net.MalformedURLException;
  30. import java.net.URL;
  31. import java.net.URLConnection;
  32. import java.net.URLEncoder;
  33. import java.util.ArrayList;
  34. import java.io.InputStream;
  35. public class MainActivity extends AppCompatActivity {
  36. public String message = "";
  37. SQLiteDatabase myDB = null;
  38. public String TableName = "Users";
  39. public String Data;
  40. public String responseData;
  41. public String username;
  42. public String password;
  43. public String data;
  44. public String query;
  45. TextView statusBar, titleLogin;
  46. EditText un, pw;
  47. @Override
  48. protected void onCreate(Bundle savedInstanceState) {
  49. super.onCreate(savedInstanceState);
  50. setContentView(R.layout.activity_main);
  51. // Get user defined values
  52. un = (EditText) findViewById(R.id.editTextUsername);//username field
  53. pw = (EditText) findViewById(R.id.editTextPassword);//password field
  54. username = un.getText().toString();//convert username and password to string and parse
  55. password = pw.getText().toString();//them to variables
  56. //initialize the status bar textview control
  57. statusBar = (TextView) findViewById(R.id.textViewStatusMsg);
  58. titleLogin = (TextView) findViewById(R.id.textViewLogin);//title text of this UI
  59. }
  60.  
  61. private class GetText extends AsyncTask<Void, Void, Void> {//} throws UnsupportedEncodingException {
  62. @Override
  63. protected Void doInBackground(Void...params){
  64. BufferedReader reader = null;
  65. // Send data
  66. try {
  67. // Defined URL where to send data
  68. URL url = new URL("http://www.naomiedify.com/school/test/login2.php");
  69. //URLConnection con = url.openConnection();
  70. HttpURLConnection con = (HttpURLConnection) url.openConnection();
  71. con.setRequestProperty("Accept-Charset", "UTF-8");
  72. con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
  73. con.setRequestMethod("POST");
  74. con.setReadTimeout(10000);
  75. con.setConnectTimeout(15000);
  76.  
  77. //this method returns empty strings
  78. query = URLEncoder.encode("txtUserName" +username,"UTF-8");
  79. query += URLEncoder.encode("&txtPassword" +password, "UTF-8");
  80. query += URLEncoder.encode("&cmbType=Aptitude Test", "UTF-8");
  81.  
  82. //this method also returns empty strings
  83. Uri.Builder builder = new Uri.Builder()
  84. .appendQueryParameter("txtUserName", username)
  85. .appendQueryParameter("txtPassword", password)
  86. .appendQueryParameter("cmbType", "Aptitude Test");
  87. query = builder.build().getEncodedQuery();
  88.  
  89. con.setDoOutput(true);
  90. OutputStreamWriter wr = new OutputStreamWriter(con.getOutputStream());
  91.  
  92. wr.write( query );
  93. wr.flush();
  94. wr.close();
  95.  
  96. reader = new BufferedReader(new InputStreamReader(con.getInputStream()));
  97. StringBuilder sb = new StringBuilder();
  98. String line;
  99.  
  100. while((line = reader.readLine()) != null) {
  101.  
  102. sb.append(line + "n");
  103. Log.d("Response: ", "> " + line);
  104. }
  105. reader.close();
  106. responseData = sb.toString();
  107. }
  108. catch (MalformedURLException e) {
  109. e.printStackTrace();
  110. responseData = e.toString();
  111. }
  112. catch (IOException e) {
  113. e.printStackTrace();
  114. responseData = e.toString();
  115. }
  116. return null;
  117. }
  118.  
  119. @Override
  120. protected void onPostExecute(Void result){
  121. statusBar.setText(responseData);
  122. super.onPostExecute(result);
  123. }
  124. }
  125.  
  126. public void loginUser(View login) {// Called when the user taps the login button
  127. Intent intent = new Intent(this, DisplayWelcomeScreen.class);//start the ui
  128.  
  129. new GetText().execute();
  130. try{
  131. //check the login using the post back data
  132. if (responseData.equals("User Found.")) {
  133. message = "Welcome"; //message for successful login
  134. // Show respsonse on activity
  135. statusBar.setText( responseData );
  136. Bundle bundle = new Bundle();//bundle the message and parse it to the next activity
  137. bundle.putString("dispMsg", message);//bundle the message using the variable dispMsg
  138. intent.putExtras(bundle);
  139. startActivity(intent);
  140. } else {
  141. message = "Incorrect Username or Password. Try again!";
  142. statusBar.setTextColor(Color.parseColor("#ff0000"));
  143. statusBar.setBackgroundColor(Color.parseColor("#d3d3d3"));
  144. // Show response on activity
  145. statusBar.setText( responseData + ": Incorrect Username or Password.");
  146. }
  147. }
  148. catch(Exception ex) {
  149. statusBar.setText("URL Exeption! "+ex);
  150. }
  151. }
  152. }
  153.  
  154. <?php
  155. $dataComingFrom = substr($_SERVER['HTTP_USER_AGENT'], 0, 7);
  156. //echo $dataComingFrom;
  157.  
  158. $con = mysqli_connect($hostname_school,$username_school,"$password_school");//connect to server
  159. mysqli_select_db($con,$_db);
  160. if (mysqli_connect_errno()){//if error connecting to server, return error message
  161. die("Failed to connect to MySQL: " . mysqli_connect_error());
  162.  
  163. }else if(!mysqli_select_db($con,$_db)) {//if no error connecting to server, try to select db to see whether it already exists.
  164.  
  165. if ($perform = mysqli_query($con,"CREATE DATABASE IF NOT EXISTS $_db")){ //if no db exists on the server, create the db.
  166. $msg = "Database $_db has been created successfully";
  167. //echo $msg;
  168. }else { echo mysqli_errno($con) .": Error creating database: " . mysqli_error($con);}//if it can't create db, return error message
  169.  
  170. }else{ //if db was selected, it means the db already exists
  171. //$data = "You have already created this database!";//return message that db already exists
  172. //die("$data <br>");//kill the script
  173.  
  174. //you can perform the login process here
  175. //$ip = preg_replace( '#[^0-9.]#', '', getenv( 'REMOTE_ADDR' ) );
  176. $username = stripslashes( str_replace( "[^A-Z a-z0-9]", "", $_POST[ 'txtUserName' ] ) );
  177. $plainPass = stripslashes( str_replace( "[^A-Z a-z0-9]", "", $_POST[ 'txtPassword' ] ) );
  178. $password = strrev(md5($plainPass));
  179.  
  180. //run the query to search for the username and password for a match
  181. //$query = "SELECT * FROM $tbl_name WHERE first_name = '$un' AND password = '$pw'";
  182.  
  183. if(empty($username) or empty($plainPass)){die("Empty Strings Parsed.");}
  184.  
  185. $user = mysqli_query($con,"SELECT * FROM ap_student WHERE AS_Email ='".$username."' AND AS_Password ='".$password."' AND
  186. AS_Status ='Confirmed!'") or die("Unable to verify user due to: " . mysqli_error($con));
  187.  
  188. //$insert = mysqli_query($con,"UPDATE ap_student SET AS_Password = '".$password."' WHERE AS_Id = 1") or die(mysqli_error($con));
  189.  
  190. $result = mysqli_num_rows($user);
  191. if ($result > 0){$returnedString = "User Found.";}else {$returnedString = "User Not Found";echo $returnedString;}
  192.  
  193. }
  194. ?>
  195.  
  196. <?xml version="1.0" encoding="utf-8"?>
  197. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  198. package="com.example.examinationportal">
  199.  
  200. <uses-permission
  201. android:name="android.permission.INTERNET">
  202. </uses-permission>
  203.  
  204. <application
  205. android:allowBackup="true"
  206. android:icon="@mipmap/ic_launcher"
  207. android:label="@string/app_name"
  208. android:roundIcon="@mipmap/ic_launcher_round"
  209. android:supportsRtl="true"
  210. android:theme="@style/Theme.AppCompat.DayNight.DarkActionBar">
  211. <activity android:name=".MainActivity">
  212. <intent-filter>
  213. <action android:name="android.intent.action.MAIN" />
  214.  
  215. <category android:name="android.intent.category.LAUNCHER" />
  216. </intent-filter>
  217. </activity>
  218. </application>
  219. </manifest>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement