Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.artrelm.artrelm2;
- import android.app.Activity;
- import android.app.AlertDialog;
- import android.content.Context;
- import android.content.Intent;
- import android.os.AsyncTask;
- import android.widget.Toast;
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.io.OutputStream;
- import java.io.OutputStreamWriter;
- import java.net.HttpURLConnection;
- import java.net.MalformedURLException;
- import java.net.URL;
- import java.net.URLEncoder;
- public class BackgroundTask extends AsyncTask<String,Void,String> {
- AlertDialog alertDialog;
- Context ctx;
- BackgroundTask(Context ctx)
- {
- this.ctx =ctx;
- }
- @Override
- protected void onPreExecute() {
- alertDialog = new AlertDialog.Builder(ctx).create();
- alertDialog.setTitle("Login Information....");
- }
- @Override
- protected String doInBackground(String... params) {
- String reg_url = "http://comicsubs.com/artrelm/register.php";
- String login_url = "http://comicsubs.com/artrelm/login.php";
- String method = params[0];
- if (method.equals("register")) {
- String username = params[1];
- String password = params[2];
- try {
- 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") ;
- bufferedWriter.write(data);
- bufferedWriter.flush();
- bufferedWriter.close();
- OS.close();
- InputStream IS = httpURLConnection.getInputStream();
- IS.close();
- //httpURLConnection.connect();
- httpURLConnection.disconnect();
- return "Registration Success...";
- } catch (MalformedURLException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- else if(method.equals("login"))
- {
- String username = 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("username","UTF-8")+"="+URLEncoder.encode(username,"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) {
- //alertDialog.setMessage(result);
- //alertDialog.show();
- //Toast.makeText(ctx, result, Toast.LENGTH_LONG).show();
- if(result.equals("Registration Success..."))
- {
- Toast.makeText(ctx, result, Toast.LENGTH_LONG).show();
- }
- else {
- if (result.equals("Signedin")) {
- Toast.makeText(ctx, "Signed In Successfully", Toast.LENGTH_LONG).show();
- Intent i = new Intent(BackgroundTask.this, MainActivity.class);
- }else Toast.makeText(ctx, "Wrong Username Or Password ", Toast.LENGTH_LONG).show();
- }
- }
- }
- /*
- import android.app.AlertDialog;
- import android.content.Context;
- import android.os.AsyncTask;
- import android.widget.Toast;
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.io.OutputStream;
- import java.io.OutputStreamWriter;
- import java.net.HttpURLConnection;
- import java.net.MalformedURLException;
- import java.net.URL;
- import java.net.URLEncoder;
- /**
- * Created by Ciobotaru on 27/03/2016.
- */ /*
- public class Background extends AsyncTask<String,Void,String> {
- AlertDialog alertDialog;
- Context ctx;
- Background(Context ctx) {
- this.ctx = ctx;
- }
- protected void onPreExecute() {
- alertDialog = new AlertDialog.Builder(ctx).create();
- alertDialog.setTitle("Login information ..");
- }
- protected String doInBackground(String... params) {
- String reg_url = "http://comicsubs.com/artrelm/register.php";
- String log_url = "http://comicsubs.com/artrelm/login.php";
- String method = params[0];
- if (method.equals("register")) {
- String username = params[1];
- String password = params[2];
- try {
- URL url = new URL(reg_url);
- HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
- httpURLConnection.setRequestMethod("POST");
- httpURLConnection.setDoOutput(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");
- bufferedWriter.write(data);
- bufferedWriter.flush();
- bufferedWriter.close();
- OS.close();
- InputStream IS = httpURLConnection.getInputStream();
- IS.close();
- //reg = true;
- return "Registration Success";
- } catch (MalformedURLException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- } else if (method.equals("sign")){
- String username = params[1];
- String password = params[2];
- try {
- URL url = new URL(log_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("username","UTF-8")+"="+URLEncoder.encode(username,"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;
- }
- /*
- String username = params[1];
- String password = params[2];
- try {
- URL url = new URL(log_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("username", "UTF-8") + "=" + URLEncoder.encode(username, "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();
- inputStream.close();
- 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;
- protected void onProgressUpdate(Void... values) {
- super.onProgressUpdate(values);
- }
- protected void onPostExecute(String result) {
- if ("Registration Success".equals(result)) {
- Toast.makeText(ctx, result, Toast.LENGTH_LONG).show();
- } else {
- alertDialog.setMessage(result);
- alertDialog.show();
- }
- }
- }
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement