Advertisement
Guest User

Untitled

a guest
Dec 3rd, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.82 KB | None | 0 0
  1. package lbalzs.soft.moviedb;
  2.  
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import android.os.AsyncTask;
  6. import android.support.design.widget.Snackbar;
  7. import android.view.View;
  8. import android.widget.TextView;
  9.  
  10. import org.apache.http.HttpResponse;
  11. import org.apache.http.client.HttpClient;
  12. import org.apache.http.client.methods.HttpGet;
  13. import org.apache.http.impl.client.DefaultHttpClient;
  14.  
  15. import java.io.BufferedReader;
  16. import java.io.InputStreamReader;
  17. import java.net.URI;
  18. import java.net.URL;
  19.  
  20.  
  21. public class SignIn extends AsyncTask{
  22.  
  23.     private String userName;
  24.     private String password;
  25.     private Context context;
  26.     private TextView status;
  27.  
  28.     public SignIn(String username, String password, TextView status, Context context){
  29.         this.userName = username;
  30.         this.password = password;
  31.         this.context = context;
  32.         this.status = status;
  33.     }
  34.  
  35.     @Override
  36.     protected Object doInBackground(Object[] objects) {
  37.         try{
  38.             String link = "http://users.atw.hu/moviedb/validatelogin.php?username="+userName+"&&password="+password;
  39.             URL url = new URL(link);
  40.             HttpClient client = new DefaultHttpClient();
  41.             HttpGet request = new HttpGet();
  42.             request.setURI(new URI(link));
  43.             HttpResponse response = client.execute(request);
  44.             BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
  45.             StringBuffer sb = new StringBuffer("");
  46.             String line = "";
  47.             while ((line = in.readLine()) != null){
  48.                 sb.append(line);
  49.             }
  50.             in.close();
  51.             if(userName.equals("") || password.equals("")){
  52.                 return "entercredentials";
  53.             }
  54.             if(sb.toString().length() != 0){
  55.                 if(sb.toString().equals("nouserfound")){
  56.                     return "nouser";
  57.                 }
  58.                 else return sb.toString();
  59.             }
  60.             else {
  61.                 return "enterusername";
  62.             }
  63.  
  64.  
  65.         } catch(Exception e){
  66.             return new String("fail");
  67.         }
  68.     }
  69.  
  70.     @Override
  71.     protected void onPostExecute(Object o){
  72.         String a = o.toString();
  73.         if(!a.equals("nouser") && !a.equals("entercredentials")){
  74.             Intent intent = new Intent(context, MainActivity.class);
  75.             intent.putExtra("UNAME", a);
  76.             context.startActivity(intent);
  77.         }
  78.         else if(a.equals("nouser")){
  79.             Snackbar mySnackbar = Snackbar.make(this.findViewById(R.id.login_lay) , "User not found!", Snackbar.LENGTH_SHORT);
  80.             mySnackbar.show();
  81.         }
  82.         else if(a.equals("entercredentials")){
  83.             status.setText("Please enter your credentials");
  84.         }
  85.  
  86.     }
  87.  
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement