View difference between Paste ID: qFJj4Q2L and Mmsbh1gb
SHOW: | | - or go back to the newest paste.
1
// main class which draws the app + ties the actions on the front end XML files such as onClick for buttons //and calls the associated method, in this case onLogin which then creates a background worker object thingy.
2
3
package hfents.hopefullyworks;
4
5
import android.os.Bundle;
6
import android.os.StrictMode;
7
import android.support.v7.app.AppCompatActivity;
8
import android.view.View;
9
import android.widget.EditText;
10
11
public class MainActivity extends AppCompatActivity {
12
13
    EditText UserNameEt, PasswordEt;
14
15
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
16
17
18
19
20
    @Override
21
    protected void onCreate(Bundle savedInstanceState) {
22
        super.onCreate(savedInstanceState);
23
24
25
        StrictMode.setThreadPolicy(policy);
26
27
28
        setContentView(R.layout.activity_main);
29
        //connecting to xml buttons.
30
        UserNameEt = (EditText) findViewById(R.id.etLogin);
31
        PasswordEt = (EditText) findViewById(R.id.etPassword);
32
    }
33
34
    public void OnLogin(View view){
35
        String userName = UserNameEt.getText().toString();
36
        String password = PasswordEt.getText().toString();
37
        String type = "login";
38
39
        BackgroundWorker backgroundWorker = new BackgroundWorker(this);
40
        backgroundWorker.doInBackground(type, userName, password);
41
42
43
    }
44
}
45
46
47
// background worker class which is responsible for starting all the Http streams and stuff like that, passing the arguments through e.g. arg[0] is login, which the logic then calls a php script I've named login.php or something. then passes everything else accordingly.
48
49
package hfents.hopefullyworks;
50
51
import android.app.AlertDialog;
52
import android.content.Context;
53
import android.os.AsyncTask;
54
import android.util.Log;
55
56
import java.io.BufferedReader;
57
import java.io.BufferedWriter;
58
import java.io.IOException;
59
import java.io.InputStream;
60
import java.io.InputStreamReader;
61
import java.io.OutputStream;
62
import java.io.OutputStreamWriter;
63
import java.net.HttpURLConnection;
64
import java.net.MalformedURLException;
65
import java.net.URL;
66
import java.net.URLEncoder;
67
68
/**
69
 * Created by jonathanbain on 01/09/2017.
70
 */
71
72
public class BackgroundWorker extends AsyncTask<String,Void,String>{
73
74
    Context context;
75
    AlertDialog alertDialog;
76
    BackgroundWorker(Context ctx){
77
        context = ctx;
78
    }
79
80
    @Override
81
    protected String doInBackground(String... params) {
82
        String type = params[0]; //first arg (login)
83
        String user_name = params[1];
84
        String user_password = params[2];
85
        String login_url = "http://root@138.68.168.141/login.php";
86
        if(type.equals("login")){
87
            //code for logging in
88
89
            try {
90
                URL url = new URL(login_url);
91
92
                    HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
93
94
95
96
                httpURLConnection.setRequestMethod("POST");
97
                httpURLConnection.setDoOutput(true);
98
                httpURLConnection.setDoInput(true);
99
                OutputStream outputStream = httpURLConnection.getOutputStream();
100
                BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream,"UTF-8"));
101
                String post_data = URLEncoder.encode("user_name","UTF-8")+"="+URLEncoder.encode(user_name,"UTF-8")+"&"
102
                    +URLEncoder.encode("user_password","UTF-8")+"="+URLEncoder.encode(user_password,"UTF-8");
103
                 //   Log.d("U", user_name);
104
                 //   Log.d("P", user_password);
105
                bufferedWriter.write(post_data);
106
107
                bufferedWriter.flush();
108
                bufferedWriter.close();
109
                outputStream.close();
110
111
                InputStream inputStream = httpURLConnection.getInputStream();
112
113
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));
114
                String result="";
115
                String line;
116
                while((line = bufferedReader.readLine())!=null){
117
                    result+=line;
118
                }
119
120
                bufferedReader.close();
121
                inputStream.close();
122
                httpURLConnection.disconnect();
123
                Log.d("Username=", user_name);
124
                Log.d("Password=", user_password);
125
                Log.d("RESULT", result);
126
                return result;
127
            } catch (MalformedURLException e){
128
                        Log.d( "Malformed URL EXCEPTION",": Background Worker Login");
129
130
            }
131
            catch (IOException e) {
132
133
                Log.d("IO Exception:", Log.getStackTraceString(e));
134
            }
135
136
        }
137
        return "";
138
    }
139
140
    @Override
141
    protected void onPreExecute(){
142
        alertDialog = new AlertDialog.Builder(context).create();
143
        alertDialog.setTitle("Login Status");
144
145
    }
146
    @Override
147
    protected void onPostExecute(String result){
148
        alertDialog.setMessage(result);
149
        alertDialog.show();
150
    }
151
152
   
153
}
154
155
156
// this is the login php file (ignore the config and connection ones as they work i think...
157
158
<?php
159
require "conn.php";
160
$user_name = $_POST["user_name"];
161
$user_pass = $_POST["user_password"];
162
echo $_POST["user_name"];
163
echo $_POST["user_password"];
164
echo $user_name;
165
echo $user_pass;
166
167
$mysql_qry = "SELECT * FROM users WHERE email LIKE '$user_name' AND password LIKE '$user_pass';";
168
$result = mysqli_query($conn ,$mysql_qry);
169
if(mysqli_num_rows($result)>0){
170
echo "login success, welcome user" ;
171
}
172
else{
173
echo "loooooogin not success";
174
}
175
?>