6 Haziran 2016 Pazartesi

GIRIS.JAVA

package com.aliozn.etut1805;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Typeface;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class Giris extends AppCompatActivity implements View.OnClickListener{

    public static final String LOGIN_URL = "http://www.spoileral.com/layout/php/android/login.php";

    //HASHMAP HASHMAP HASHMAP HASHMAP HASHMAP HASHMAP HASHMAP HASHMAP HASHMAP HASHMAP HASHMAP HASHMAP    public static final String KEY_USERNAME="username";
    public static final String KEY_PASSWORD="password";
    //HASHMAP HASHMAP HASHMAP HASHMAP HASHMAP HASHMAP HASHMAP HASHMAP HASHMAP HASHMAP HASHMAP HASHMAP
    private EditText kullaniciadi;
    private EditText sifre;
    private Button buttonLogin;

    private String username;
    private String password;

    private ProgressDialog girisProgressDialog;

    private Toolbar mToolbar;

    //boolean variable to check user is logged in or not    //initially it is false    private boolean loggedIn = false;


    public void hideKeyboard(View view) {
        InputMethodManager inputMethodManager =(InputMethodManager)getSystemService(Activity.INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }

    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_giris);

        TextView tvgiris = (TextView) findViewById(R.id.tvgiris) ;
        Typeface fonttipi = Typeface.createFromAsset(getAssets(),"fonts/Roboto-Regular.ttf");
        tvgiris.setTypeface(fonttipi);

        kullaniciadi = (EditText) findViewById(R.id.kullaniciadi);
        sifre = (EditText) findViewById(R.id.sifre);

        girisProgressDialog = new ProgressDialog(Giris.this);

        buttonLogin = (Button) findViewById(R.id.gir);
        buttonLogin.setOnClickListener(this);


//EDITTEXT DIŞINA TIKLANDIĞINDA KLAVYENİN KAPANMASI OLAYI        kullaniciadi.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override            public void onFocusChange(View v, boolean hasFocus) {
                if (!hasFocus) {
                    hideKeyboard(v);
                }
            }
        });

        sifre.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override            public void onFocusChange(View v, boolean hasFocus) {
            if (!hasFocus) {
                hideKeyboard(v);
            }
            }
        });
//EDITTEXT DIŞINA TIKLANDIĞINDA KLAVYENİN KAPANMASI OLAYI    }


    //onResume() onResume() onResume() onResume() onResume() onResume() onResume() onResume() onResume() onResume()    protected void onResume() {
        super.onResume();

        SharedPreferences sharedPreferences = getSharedPreferences(config.PREFERENCES_NAME, Context.MODE_PRIVATE);


        loggedIn = sharedPreferences.getBoolean(config.LOGGEDIN_PREF, false);

        //EĞER TRUE CEVAP ALIRSAK.        if(loggedIn){
            //We will start the Profile Activity            int id = sharedPreferences.getInt(config.USER_ID_PREF, 0);
            if (id != 0) {
                config.uyeID = id;
                Intent intent = new Intent(Giris.this, Ogrenci.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); // Clear history                // TODO: Profile girince ilk tıklama FLAG_ACTIVITY_CLEAR_TASK                startActivity(intent);
            }
        }
    }
    //onResume() onResume() onResume() onResume() onResume() onResume() onResume() onResume() onResume() onResume()

    private void userLogin() {
        username = kullaniciadi.getText().toString();
        password = sifre.getText().toString();
        StringRequest stringRequest = new StringRequest(Request.Method.POST, LOGIN_URL,
                new Response.Listener<String>() {
                    @Override                    public void onResponse(String response) {

                        //SERVERDAN SUCCSES GELIRSE                        try {
                            JSONObject jObj = new JSONObject(response);

                            if (jObj.getBoolean("success")) {
                                int userId = jObj.getInt("id");
                                config.uyeID = userId;
                                int rutbe = jObj.getInt("rutbe");
                                config.rutbe = userId;
                                SharedPreferences sharedPreferences = Giris.this.getSharedPreferences(config.PREFERENCES_NAME, Context.MODE_PRIVATE);

                                SharedPreferences.Editor editor = sharedPreferences.edit();

                                editor.putBoolean(config.LOGGEDIN_PREF, true);
                                editor.putString(config.NAME_SHARED_PREF, username);
                                editor.putInt(config.USER_ID_PREF, userId);

                                editor.apply();
                                girisProgressDialog.dismiss();
                                Intent intent = new Intent(Giris.this, Ogrenci.class);
                                startActivity(intent);
                            } else {
                                girisProgressDialog.dismiss();
                                Toast.makeText(getApplicationContext(),"Hatalı kullanıcı adı veya şifre...",Toast.LENGTH_LONG).show();
                            }

                        } catch (JSONException e) {
                            Toast.makeText(getApplicationContext(),"Bağlantı başarısız...",Toast.LENGTH_LONG).show();
                            girisProgressDialog.dismiss();
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override                    public void onErrorResponse(VolleyError error) {

                        Toast.makeText(getApplicationContext(),"Bağlantı Başarısız...",Toast.LENGTH_LONG).show();
                        girisProgressDialog.dismiss();
                    }
                }){
            @Override            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String,String> map = new HashMap<String,String>();
                map.put(KEY_USERNAME,username);
                map.put(KEY_PASSWORD,password);
                return map;
            }
        };
        RequestQueue requestQueue = Volley.newRequestQueue(this);
        requestQueue.add(stringRequest);
    }



    //Giriş Yap Giriş Yap Giriş Yap Giriş Yap Giriş Yap Giriş Yap    //Giriş Yap Giriş Yap Giriş Yap Giriş Yap Giriş Yap Giriş Yap    //Giriş Yap Giriş Yap Giriş Yap Giriş Yap Giriş Yap Giriş Yap    @Override    public void onClick(View v) {
        if (!isNetworkAvailable()) {
            AlertDialog.Builder builder = new AlertDialog.Builder(Giris.this);
            builder.setMessage("İnternet Bağlantınız Bulunamadı...")
                    .setCancelable(false)
                    .setNegativeButton("Çıkış", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            Giris.this.finish();
                        }
                    });
            AlertDialog alert = builder.create();
            alert.show();
        }else{
            userLogin();

            girisProgressDialog.setMessage("Giriş Yapılıyor...");
            girisProgressDialog.show();
        }
    }
    //Giriş Yap Giriş Yap Giriş Yap Giriş Yap Giriş Yap Giriş Yap    //Giriş Yap Giriş Yap Giriş Yap Giriş Yap Giriş Yap Giriş Yap    //Giriş Yap Giriş Yap Giriş Yap Giriş Yap Giriş Yap Giriş Yap

    //İNTERNET KONTROLÜ İNTERNET KONTROLÜ İNTERNET KONTROLÜ İNTERNET KONTROLÜ İNTERNET KONTROLÜ İNTERNET KONTROLÜ    private boolean isNetworkAvailable() {
        ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
        return activeNetworkInfo != null && activeNetworkInfo.isConnected();
    }
    //İNTERNET KONTROLÜ İNTERNET KONTROLÜ İNTERNET KONTROLÜ İNTERNET KONTROLÜ İNTERNET KONTROLÜ İNTERNET KONTROLÜ



}

Hiç yorum yok:

Yorum Gönder