Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Override
- protected String doInBackground(String... params) {
- String reg_url = "http://10.0.2.2/app/register.php";
- String login_url = "http://10.0.2.2/app/login.php";
- String method = params[0];
- if (method.equals("register")) {
- String username = params[1]; //parameters for register
- String password = params[2];
- String phone_no = params[3];
- try {
- //connection to mysql database
- URL url = new URL(reg_url);
- HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
- httpURLConnection.setRequestMethod("POST");
- httpURLConnection.setDoOutput(true);
- httpURLConnection.setDoInput(true);
- OutputStream OS = httpURLConnection.getOutputStream();
- BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(OS, "UTF-8"));
- String data = URLEncoder.encode("username", "UTF-8") + "=" + URLEncoder.encode(username, "UTF-8") + "&" +
- URLEncoder.encode("password", "UTF-8") + "=" + URLEncoder.encode(password, "UTF-8") + "&" +
- URLEncoder.encode("phone_no", "UTF-8") + "=" + URLEncoder.encode(phone_no, "UTF-8");
- bufferedWriter.write(data);
- bufferedWriter.flush();
- bufferedWriter.close();
- OS.close();
- InputStream IS = httpURLConnection.getInputStream();
- IS.close();
- //httpURLConnection.connect();
- httpURLConnection.disconnect();
- return "Registration Successful";
- } catch (MalformedURLException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }else if(method.equals("login")) //login class
- {
- String id = params[1];
- String password = params[2];
- try {
- URL url = new URL(login_url);
- HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
- httpURLConnection.setRequestMethod("POST");
- httpURLConnection.setDoOutput(true);
- httpURLConnection.setDoInput(true);
- OutputStream outputStream = httpURLConnection.getOutputStream();
- BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream,"UTF-8"));
- String data = URLEncoder.encode("id","UTF-8")+"="+URLEncoder.encode(id,"UTF-8")+"&"+
- URLEncoder.encode("password","UTF-8")+"="+URLEncoder.encode(password,"UTF-8");
- bufferedWriter.write(data);
- bufferedWriter.flush();
- bufferedWriter.close();
- outputStream.close();
- InputStream inputStream = httpURLConnection.getInputStream();
- BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));
- String response = "";
- String line = "";
- while ((line = bufferedReader.readLine())!=null)
- {
- response+= line;
- }
- bufferedReader.close();
- inputStream.close();
- httpURLConnection.disconnect();
- return response;
- } catch (MalformedURLException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- return null;
- }
- @Override
- protected void onProgressUpdate(Void... values) {
- super.onProgressUpdate(values);
- }
- @Override
- protected void onPostExecute(String result) {
- super.onPostExecute(result);
- if(result.equals("Registration Successful"))
- {
- Toast.makeText(ctx, result, Toast.LENGTH_LONG).show();
- }
- else if(result.equals("login successful"))
- //if login is successful, it sends the user to another activity
- {
- Toast.makeText(ctx, result, Toast.LENGTH_LONG).show();
- ctx.startActivity(new Intent(ctx, sync.class));
- }
- else{
- Toast.makeText(ctx, result, Toast.LENGTH_LONG).show();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement