Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.liquifyapp;
- import java.io.BufferedInputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.UnsupportedEncodingException;
- import java.util.ArrayList;
- import java.util.List;
- import org.apache.http.HttpEntity;
- import org.apache.http.HttpResponse;
- import org.apache.http.NameValuePair;
- import org.apache.http.client.ClientProtocolException;
- import org.apache.http.client.HttpClient;
- import org.apache.http.client.entity.UrlEncodedFormEntity;
- import org.apache.http.client.methods.HttpPost;
- import org.apache.http.impl.client.DefaultHttpClient;
- import org.apache.http.message.BasicNameValuePair;
- import org.apache.http.protocol.HTTP;
- import org.apache.http.util.ByteArrayBuffer;
- import android.app.Activity;
- import android.app.AlertDialog;
- import android.content.DialogInterface;
- import android.content.Intent;
- import android.graphics.Paint;
- import android.os.AsyncTask;
- import android.os.Bundle;
- import android.view.Menu;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.view.Window;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.ImageView;
- import android.widget.TextView;
- import android.widget.Toast;
- public class LogReg extends Activity {
- EditText user;
- EditText pass;
- String xuser;
- String xpass;
- String flag;
- SessionManager session;
- public void ToastLoadShout(String msg){Toast.makeText(this, msg, Toast.LENGTH_LONG).show();}
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
- setContentView(R.layout.activity_log_reg);
- getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.titlebar);
- TextView register =(TextView) findViewById(R.id.register);
- register.setPaintFlags(register.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
- register.setOnClickListener(new OnClickListener() {
- public void onClick(View v) {
- Intent intent = new Intent(LogReg.this, Registration.class);
- startActivity(intent);
- finish();
- }
- });
- final CharSequence[] items = {"Account / Register", "Save Article", "Back", "Main Menu"};
- AlertDialog.Builder builder = new AlertDialog.Builder(LogReg.this);
- builder.setTitle("Liquify News");
- builder.setIcon(R.drawable.poplisticon);
- builder.setItems(items, new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int item) {
- if(item==0)
- {
- Intent intent = new Intent(LogReg.this, LogReg.class);
- startActivity(intent);
- }
- if (item==1)
- {
- }
- if(item==2)
- {
- finish();
- }
- if(item==3)
- {
- Intent intent = new Intent(LogReg.this, MainActivity.class);
- startActivity(intent);
- }
- }
- });
- final AlertDialog alert = builder.create();
- ImageView menu;
- menu = (ImageView) findViewById(R.id.menu);
- menu.setOnClickListener(new OnClickListener() {
- public void onClick(View v) {
- alert.show();
- }
- });
- user =(EditText) findViewById(R.id.usertext);
- pass =(EditText) findViewById(R.id.passtext);
- Button login;
- login = (Button) findViewById(R.id.loginbut);
- login.setOnClickListener(new OnClickListener() {
- public void onClick(View v) {
- xuser=user.getText().toString();
- xpass=pass.getText().toString();
- if ((!xuser.equals(""))&&(!xuser.equals(null)))
- {
- if((!xpass.equals(""))&&(!xpass.equals(null)))
- {
- new PostAsyncTask().execute();
- }
- else
- ToastLoadShout("Please enter a password.");
- }
- else
- ToastLoadShout("Please enter a username.");
- }
- });
- }
- public String Login()
- {
- try
- {
- HttpClient httpClient = new DefaultHttpClient();
- HttpPost httpPost = new HttpPost("http://www.mydomain.com/login.php");
- List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(1);
- nameValuePair.add(new BasicNameValuePair("user",xuser));
- nameValuePair.add(new BasicNameValuePair("pass",xpass));
- UrlEncodedFormEntity form;
- form = new UrlEncodedFormEntity(nameValuePair);
- form.setContentEncoding(HTTP.UTF_8);
- try {
- httpPost.setEntity((HttpEntity) new UrlEncodedFormEntity(nameValuePair,"UTF-8"));
- } catch (UnsupportedEncodingException e) {
- // writing error to Log
- e.printStackTrace();
- }
- try
- {
- HttpResponse response = httpClient.execute(httpPost);
- //HttpEntity entity = response.getEntity();
- InputStream is = response.getEntity().getContent();
- BufferedInputStream bis = new BufferedInputStream(is);
- ByteArrayBuffer baf = new ByteArrayBuffer(20);
- int current = 0;
- while((current = bis.read()) != -1){
- baf.append((byte)current);
- }
- /* Convert the Bytes read to a String. */
- flag=(new String(baf.toByteArray()));
- }
- catch (ClientProtocolException e) {
- // writing exception to log
- e.printStackTrace();
- } catch (IOException e) {
- // writing exception to log
- e.printStackTrace();
- }
- }
- catch (Exception e) {
- // TODO: handle exception
- }
- return flag;
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- getMenuInflater().inflate(R.menu.activity_log_reg, menu);
- return true;
- }
- class PostAsyncTask extends AsyncTask<String, String, String> {
- protected void onPreExecute() {
- // show progress dialog
- }
- protected String doInBackground(String... params) {
- return Login();
- }
- protected void onPostExecute(String result) {
- flag=result;
- if (flag.equals("1"))
- {
- //ToastLoadShout("Login Successful.");
- ToastLoadShout("Welcome Back, "+xuser+"!");
- session.createLoginSession(xuser);
- ToastLoadShout("flag: "+flag);
- finish();
- }
- if (flag.equals("2"))
- {
- ToastLoadShout("Wrong Username or Password.");
- ToastLoadShout("flag: "+flag);
- }
- if(!flag.equals("1")&&!flag.equals("2"))
- {
- ToastLoadShout("Could'nt Log you in. Try again later.");
- ToastLoadShout("flag: "+flag);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement