Advertisement
Guest User

LOGREG CLASS

a guest
Mar 28th, 2013
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.20 KB | None | 0 0
  1. package com.example.liquifyapp;
  2.  
  3. import java.io.BufferedInputStream;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.io.UnsupportedEncodingException;
  7. import java.util.ArrayList;
  8. import java.util.List;
  9.  
  10. import org.apache.http.HttpEntity;
  11. import org.apache.http.HttpResponse;
  12. import org.apache.http.NameValuePair;
  13. import org.apache.http.client.ClientProtocolException;
  14. import org.apache.http.client.HttpClient;
  15. import org.apache.http.client.entity.UrlEncodedFormEntity;
  16. import org.apache.http.client.methods.HttpPost;
  17. import org.apache.http.impl.client.DefaultHttpClient;
  18. import org.apache.http.message.BasicNameValuePair;
  19. import org.apache.http.protocol.HTTP;
  20. import org.apache.http.util.ByteArrayBuffer;
  21.  
  22. import android.app.Activity;
  23. import android.app.AlertDialog;
  24. import android.content.DialogInterface;
  25. import android.content.Intent;
  26. import android.graphics.Paint;
  27. import android.os.AsyncTask;
  28. import android.os.Bundle;
  29. import android.view.Menu;
  30. import android.view.View;
  31. import android.view.View.OnClickListener;
  32. import android.view.Window;
  33. import android.widget.Button;
  34. import android.widget.EditText;
  35. import android.widget.ImageView;
  36. import android.widget.TextView;
  37. import android.widget.Toast;
  38.  
  39. public class LogReg extends Activity {
  40.     EditText user;
  41.     EditText pass;
  42.    
  43.     String xuser;
  44.     String xpass;
  45.    
  46.     String flag;
  47.    
  48.     SessionManager session;
  49.    
  50.     public void ToastLoadShout(String msg){Toast.makeText(this, msg, Toast.LENGTH_LONG).show();}
  51.     @Override
  52.     public void onCreate(Bundle savedInstanceState) {
  53.         super.onCreate(savedInstanceState);
  54.         requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
  55.         setContentView(R.layout.activity_log_reg);
  56.         getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.titlebar);
  57.        
  58.         TextView register =(TextView) findViewById(R.id.register);
  59.         register.setPaintFlags(register.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
  60.         register.setOnClickListener(new OnClickListener() {
  61.  
  62.             public void onClick(View v) {
  63.                 Intent intent = new Intent(LogReg.this, Registration.class);
  64.                 startActivity(intent);
  65.                 finish();
  66.             }
  67.         });
  68.        
  69.         final CharSequence[] items = {"Account / Register", "Save Article", "Back", "Main Menu"};
  70.  
  71.         AlertDialog.Builder builder = new AlertDialog.Builder(LogReg.this);
  72.         builder.setTitle("Liquify News");
  73.         builder.setIcon(R.drawable.poplisticon);
  74.         builder.setItems(items, new DialogInterface.OnClickListener() {
  75.             public void onClick(DialogInterface dialog, int item) {
  76.          
  77.         if(item==0)
  78.         {
  79.             Intent intent = new Intent(LogReg.this, LogReg.class);
  80.             startActivity(intent);
  81.         }
  82.         if (item==1)
  83.         {
  84.            
  85.         }
  86.         if(item==2)
  87.         {
  88.             finish();
  89.         }
  90.         if(item==3)
  91.         {
  92.             Intent intent = new Intent(LogReg.this, MainActivity.class);
  93.             startActivity(intent);
  94.         }
  95.        
  96.             }
  97.         });
  98.         final AlertDialog alert = builder.create();
  99.     ImageView menu;
  100.     menu = (ImageView) findViewById(R.id.menu);
  101.     menu.setOnClickListener(new OnClickListener() {
  102.  
  103.         public void onClick(View v) {
  104.             alert.show();
  105.         }
  106.     });
  107.    
  108.     user =(EditText) findViewById(R.id.usertext);
  109.     pass =(EditText) findViewById(R.id.passtext);
  110.    
  111.     Button login;
  112.     login = (Button) findViewById(R.id.loginbut);
  113.     login.setOnClickListener(new OnClickListener() {
  114.  
  115.         public void onClick(View v) {
  116.              xuser=user.getText().toString();
  117.              xpass=pass.getText().toString();
  118.              
  119.             if ((!xuser.equals(""))&&(!xuser.equals(null)))
  120.             {
  121.                 if((!xpass.equals(""))&&(!xpass.equals(null)))
  122.                 {
  123.                     new PostAsyncTask().execute();
  124.                 }
  125.                 else
  126.                     ToastLoadShout("Please enter a password.");
  127.             }
  128.             else
  129.                 ToastLoadShout("Please enter a username.");
  130.         }
  131.     });
  132.     }
  133.     public String Login()
  134.     {
  135.             try
  136.             {
  137.             HttpClient httpClient = new DefaultHttpClient();
  138.             HttpPost httpPost = new HttpPost("http://www.mydomain.com/login.php");
  139.             List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(1);
  140.             nameValuePair.add(new BasicNameValuePair("user",xuser));
  141.             nameValuePair.add(new BasicNameValuePair("pass",xpass));
  142.             UrlEncodedFormEntity form;
  143.             form = new UrlEncodedFormEntity(nameValuePair);
  144.             form.setContentEncoding(HTTP.UTF_8);
  145.             try {
  146.                 httpPost.setEntity((HttpEntity) new UrlEncodedFormEntity(nameValuePair,"UTF-8"));
  147.             } catch (UnsupportedEncodingException e) {
  148.                 // writing error to Log
  149.                 e.printStackTrace();
  150.             }
  151.             try
  152.             {                    
  153.                
  154.                 HttpResponse response = httpClient.execute(httpPost);
  155.                 //HttpEntity entity = response.getEntity();
  156.  
  157.                 InputStream is = response.getEntity().getContent();
  158.                 BufferedInputStream bis = new BufferedInputStream(is);
  159.                 ByteArrayBuffer baf = new ByteArrayBuffer(20);
  160.                
  161.                 int current = 0;
  162.                  
  163.                 while((current = bis.read()) != -1){
  164.                     baf.append((byte)current);
  165.                 }  
  166.      
  167.                 /* Convert the Bytes read to a String. */
  168.                 flag=(new String(baf.toByteArray()));
  169.                
  170.                
  171.             }
  172.             catch (ClientProtocolException e) {
  173.                 // writing exception to log
  174.                
  175.                 e.printStackTrace();
  176.             } catch (IOException e) {
  177.                 // writing exception to log
  178.                
  179.                 e.printStackTrace();
  180.             }          
  181.        
  182.             }
  183.         catch (Exception e) {
  184.             // TODO: handle exception
  185.            
  186.         }
  187.         return flag;
  188.     }
  189.    
  190.    
  191.  
  192.     @Override
  193.     public boolean onCreateOptionsMenu(Menu menu) {
  194.         getMenuInflater().inflate(R.menu.activity_log_reg, menu);
  195.         return true;
  196.     }
  197.    
  198.     class PostAsyncTask extends AsyncTask<String, String, String> {
  199.         protected void onPreExecute() {
  200.              // show progress dialog
  201.          }
  202.          protected String doInBackground(String... params) {
  203.              
  204.             return Login();
  205.          }
  206.  
  207.              protected void onPostExecute(String result) {
  208.                  flag=result;
  209.                if (flag.equals("1"))
  210.                {
  211.                    //ToastLoadShout("Login Successful.");
  212.                    ToastLoadShout("Welcome Back, "+xuser+"!");
  213.                    session.createLoginSession(xuser);
  214.                    ToastLoadShout("flag: "+flag);
  215.                    finish();
  216.                }
  217.                if (flag.equals("2"))
  218.                {
  219.                  ToastLoadShout("Wrong Username or Password.");
  220.                  ToastLoadShout("flag: "+flag);
  221.                }
  222.                if(!flag.equals("1")&&!flag.equals("2"))
  223.                {
  224.                    ToastLoadShout("Could'nt Log you in. Try again later.");
  225.                    ToastLoadShout("flag: "+flag);
  226.                }
  227.                
  228.              }
  229.          }
  230.    
  231. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement