Advertisement
Guest User

Untitled

a guest
Dec 7th, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.38 KB | None | 0 0
  1. package com.buurt.buurtapp;
  2.  
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import android.net.Uri;
  6. import android.os.AsyncTask;
  7. import android.util.Log;
  8. import android.widget.Toast;
  9.  
  10. import java.io.BufferedReader;
  11. import java.io.DataOutputStream;
  12. import java.io.IOException;
  13. import java.io.InputStream;
  14. import java.io.InputStreamReader;
  15. import java.net.HttpURLConnection;
  16. import java.net.URL;
  17. import java.util.Objects;
  18.  
  19. /**
  20.  * Created by daan on 28-5-17.
  21.  */
  22.  
  23. public class LoginTask extends AsyncTask<String, Integer, String> {
  24.     Context appContext;
  25.  
  26.     public LoginTask(Context applicationContext) {
  27.         appContext = applicationContext;
  28.     }
  29.  
  30.     @Override
  31.     protected String doInBackground(String... params) {
  32.         int statusCode;
  33.         String username = params[0];
  34.         String password = params[1];
  35.         try {
  36.             Uri.Builder builder = new Uri.Builder()
  37.                     .appendQueryParameter("gebruikersnaam", username)
  38.                     .appendQueryParameter("wachtwoord", password);
  39.  
  40.             URL url = new URL("http://buurtapp.uptools.nl:8080/foto_app_backend/login.php");
  41.             HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
  42.             urlConnection.setReadTimeout(30000);
  43.             urlConnection.setConnectTimeout(30000);
  44.             String query = builder.build().getEncodedQuery();
  45.  
  46.             urlConnection.setRequestMethod("POST");
  47.             urlConnection.setDoInput(true);
  48.             urlConnection.setDoOutput(true);
  49.  
  50.             urlConnection.connect();
  51.  
  52.             DataOutputStream wr = new DataOutputStream(
  53.                     urlConnection.getOutputStream());
  54.             wr.writeBytes(query);
  55.             wr.flush();
  56.             wr.close();
  57.  
  58.             InputStream isNew = urlConnection.getInputStream();
  59.             BufferedReader rd = new BufferedReader(new InputStreamReader(isNew));
  60.  
  61.  
  62.             String line;
  63.             StringBuilder response = new StringBuilder();
  64.             while ((line = rd.readLine()) != null) {
  65.                 response.append(line);
  66.  
  67.             }
  68.             rd.close();
  69.  
  70.             Log.d("LoginTask", response.toString());
  71.             statusCode = Integer.parseInt(response.toString());
  72.         } catch (IOException e) {
  73.             e.printStackTrace();
  74.             statusCode = 100;
  75.         }
  76.  
  77.         switch (statusCode) {
  78.             case 0:
  79.                 return "Login successful";
  80.             case 1:
  81.                 return "Our server is currently experiencing issues, please try again in a short moment";
  82.             case 2:
  83.                 return "The username and password combination is incorrect";
  84.             case 3:
  85.                 return "Something weird happened, please tell us how you got to this message :)";
  86.             case 100:
  87.                 return "We were not able to connect to our server, do you have an active internet connection?";
  88.             default:
  89.                 Log.d("LOGIN", "Unhandled status code: " + statusCode);
  90.         }
  91.  
  92.         return "a";
  93.     }
  94.  
  95.     @Override
  96.     protected void onPostExecute(String s) {
  97.         Toast.makeText(appContext, s, Toast.LENGTH_LONG).show();
  98.         if (Objects.equals(s, "Login successful")) {
  99.             Intent intent = new Intent(appContext, DashboardActivity.class);
  100.             appContext.startActivity(intent);
  101.         }
  102.     }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement