• Android tizimida malumotlarni saqlash
  • MySQL va Android Studio misoli
  • Mobil ilovalarni ishlab chiqish




    Download 242,56 Kb.
    Sana18.05.2024
    Hajmi242,56 Kb.
    #241973
    Bog'liq
    3-topshiriq


    MUHAMMAD AL-XOZAZMIY NOMIDAGI TOSHKENT AXBOROT TEXNOLOGIYALARI UNIVERSITETI



    Mobil ilovalarni ishlab chiqish
    fanidan
    3-topshiriq

    Guruh: 318-21­ talabasi
    Bajardi: Jo’rayev Ibratjon
    Tekshirdi: Abdurazzoqov Faxriddin

    Toshkent 2024
    Android tizimida ma'lumotlarni saqlash

    Ilovalardan foydalanish jarayonida ilovaga tegishli biror ma'lumotni vaqtinchalik qayerdadir saqlashga va unga qayta murojaat qilishga toʻgʻri keladi.

    Android tizimida 3 xil koʻrinishda ma'lumotlarni saqlash mumkin:
    1- Kichik oʻlchamdagi va tez-tez murojaat qilish uchun ishlatiladigan ob'ektidan foydalanish.
    2- Fayllar tizimidan foydalanish.
    3- Relasion ma'lumotlar bazasini boshqarish tizimi SQLite ma'lumotlar bazasidan foydalanish.

    MySQL va Android Studio misoli
    Men mySQL ma'lumotlar bazasiga ma'lumotlarni kiritishi mumkin bo'lgan mobil ilovani qanday ishlab chiqishni ko'rsataman. Men foydalangan server XAMPP serveridir.
    Mobil ilova quyidagi kabi. U ism, foydalanuvchi nomi va parol kabi foydalanuvchini ro'yxatdan o'tkazish uchun foydalanilgan. Keyin u login hisobi sifatida ishlatilishi mumkin.
       
    Bu foydalanuvchi ro'yxatdan o'tganda.

    1-qadam: phpMyAdmin-da mySQL ma'lumotlar bazasini yarating

    • “ Talabalar ” nomli ma’lumotlar bazasini yarating .

    • “ ID ”, “ ism ”, “ foydalanuvchi nomi ” va “ parol ” boʻlgan uchta ustunli roʻyxat nomli jadval yarating .


    • Ma'lumotlar bazasi va jadval yaratish bo'yicha oldingi o'quv qo'llanmamga murojaat qilishingiz mumkin.

    2-qadam: 3 ta PHP fayl yarating

    • conn.php nomli PHP faylini yarating .

    $servername = "localhost";


    $username = "root";
    $parol = "";
    $ma'lumotlar bazasi = "talabalar";

    // Ulanish yaratish


    $conn = mysqli_connect($servername, $foydalanuvchi nomi, $parol,$ma'lumotlar bazasi);
    ?>

    • insertData.ph p nomli PHP fayl yarating . Keyin quyida kodlashni yozing. Ism, foydalanuvchi nomi va parol uchun soxta qiymat qo'yish orqali PHP-ni sinab ko'rishingiz mumkin. $name, $user_name va $password oʻzgaruvchisiga izoh qoldirishni unutmang.

    "conn.php" ni talab qilish;
    $name = $_POST["ism"];
    $user_name = $_POST["user_name"];
    $parol = $_POST["parol"];
    //$name = "shaharil";
    //$user_name = "shah";
    //$parol = "1234";

    $mysqli_query = "RO'YAT QIYMATLARIGA QO'SHISH (NULL,'$name','$user_name','$parol');";


    $natija = mysqli_query($conn,$mysqli_query);


    //AVTOINCREMENTni qayta o'rnating


    $natija = mysqli_query($conn,"SET @num := 0;");
    $natija = mysqli_query($conn,"Yangilanish ro'yxati SET identifikatori @num := (@num+1)");
    $natija = mysqli_query($conn,"ALTER TABLE ro'yxati AUTO_INCREMENT =1");

    agar($natija){


    print("Siz ro'yxatdan o'tgansiz!");
    }
    boshqa{
    chop etish ("Muvaffaqiyatli EMAS");
    }

    ?>


    • validateData.php nomli boshqa PHP faylini yarating . Keyin quyida kodlashni yozing. Siz PHP-ni foydalanuvchi nomi va parol uchun soxta qiymat qo'yish orqali sinab ko'rishingiz mumkin. $user_name va $password oʻzgaruvchisiga izoh qoldirishni unutmang.

    "conn.php" ni talab qilish;


    $user_name = $_POST["user_name"];
    $parol = $_POST["parol"];
    //$user_name = "shah";
    //$parol = "1234";

    $mysqli_query = "Foydalanuvchi nomi '$user_name' va parol kabi '$password' kabi ro'yxatda SELECT * FROM";


    $natija = mysqli_query($conn,$mysqli_query);


    if(mysqli_num_rows($result)>0){


    //chop etish ("Kirish muvaffaqiyatli");
    $ qator = mysqli_fetch_assoc ($ natija);
    $name =$satr["ism"];
    Chop etish ("Kirish muvaffaqiyatli..Xush kelibsiz ".$name);

    }
    boshqa{


    print("Kirish muvaffaqiyatli emas");
    }

    ?>


    3-qadam: Komponentlar Android Studio-da bo'lishi kerak

    • Loyihangizda quyidagi kabi barcha fayllar bo'lishi kerak. Gradle fayli o'zgarmaydi.


    4-qadam: activity_main.xml ni loyihalash :



    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    asboblar: context=".MainActivity">


    android: id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dp"
    android:ems="10"
    android: maslahat = "ism"
    android:inputType="textPersonName"
    ilova:layout_constraintEnd_toEndOf="ota-ona"
    ilova:layout_constraintStart_toStartOf="ota-ona"
    ilova:layout_constraintTop_toTopOf="ota-ona" />


    android: id="@+id/editText2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="16dp"
    android:ems="10"
    android: maslahat = "foydalanuvchi nomi"
    android:inputType="textPersonName"
    ilova:layout_constraintEnd_toEndOf="ota-ona"
    ilova:layout_constraintStart_toStartOf="ota-ona"
    ilova:layout_constraintTop_toBottomOf="@+id/editText1" />


    android: id="@+id/editText3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="16dp"
    android:ems="10"
    android: maslahat = "parol"
    android:inputType="textPersonName"
    ilova:layout_constraintStart_toStartOf="@+id/editText2"
    ilova:layout_constraintTop_toBottomOf="@+id/editText2" />


    android: id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dp"
    android:text="Ro'yxatdan o'tish"
    ilova:layout_constraintEnd_toEndOf="ota-ona"
    ilova:layout_constraintStart_toStartOf="ota-ona"
    ilova:layout_constraintTop_toBottomOf="@+id/editText3" />


    android: id="@+id/editText4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="100dp"
    android:ems="10"
    android: maslahat = "foydalanuvchi nomi"
    android:inputType="textPersonName"
    ilova:layout_constraintEnd_toEndOf="ota-ona"
    ilova:layout_constraintStart_toStartOf="ota-ona"
    ilova:layout_constraintTop_toBottomOf="@+id/button1" />


    android: id="@+id/editText5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="16dp"
    android:ems="10"
    android: maslahat = "parol"
    android:inputType="textPassword"
    ilova:layout_constraintStart_toStartOf="@+id/editText4"
    ilova:layout_constraintTop_toBottomOf="@+id/editText4" />


    android: id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dp"
    android:text="Kirish"
    ilova:layout_constraintEnd_toEndOf="ota-ona"
    ilova:layout_constraintStart_toStartOf="ota-ona"
    ilova:layout_constraintTop_toBottomOf="@+id/editText5" />



    5-qadam: Design the BackgroundWorker.java nomli sinf yarating :
    com.example.a06_mysql_reg_login paketi;

    android.app.AlertDialog import;


    android.content.Context import;
    android.os.AsyncTask import;
    import android.widget.Toast;

    import java.io.BufferedReader;


    import java.io.BufferedWriter;
    import java.io.IOException;
    java.io.InputStreamni import qilish;
    java.io.InputStreamReader dasturini import qilish;
    java.io.OutputStreamni import qilish;
    java.io.OutputStreamWriterni import qilish;
    import java.net.HttpURLConnection;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.net.URLEncoder;

    umumiy sinf BackgroundWorker AsyncTask-ni kengaytiradi {


    Kontekst konteksti;
    AlertDialog alertDialog;
    BackgroundWorker (Context ctx){
    kontekst = ctx;
    }
    @Override
    himoyalangan String doInBackground(String... params) {
    String login_url = params[0];
    String turi = params[1];
    String nomi = params[2];
    String username = params[3];
    String parol = params[4];
    harakat qilib ko'ring {

    URL url = yangi URL (login_url);


    HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
    httpURLConnection.setRequestMethod("POST");
    httpURLConnection.setDoOutput(to'g'ri);
    httpURLConnection.setDoInput(to'g'ri);
    OutputStream outputStream = httpURLConnection.getOutputStream();
    BufferedWriter bufferedWriter = yangi BufferedWriter (yangi OutputStreamWriter(outputStream, "UTF-8"));

    string post_data = "";


    if(type.equals("ro'yxatdan o'tish")) {
    post_data = URLEncoder.encode("ism", "UTF-8") + "=" + URLEncoder.encode(nom, "UTF-8") + "&"
    + URLEncoder.encode("user_name", "UTF-8") + "=" + URLEncoder.encode(foydalanuvchi nomi, "UTF-8")+ "&"
    + URLEncoder.encode("parol", "UTF-8") + "=" + URLEncoder.encode(parol, "UTF-8");
    }
    else if(type.equals("login")){
    post_data = URLEncoder.encode("user_name", "UTF-8") + "=" + URLEncoder.encode(foydalanuvchi nomi, "UTF-8") + "&"
    + URLEncoder.encode("parol", "UTF-8") + "=" + URLEncoder.encode(parol, "UTF-8");
    }

    bufferedWriter.write(post_ma'lumotlar);


    bufferedWriter.flush();
    bufferedWriter.close();
    outputStream.close();
    InputStream inputStream = httpURLConnection.getInputStream();
    BufferedReader bufferedReader = yangi BufferedReader (yangi InputStreamReader (inputStream, "iso-8859-1"));
    String natijasi = "";
    String line = "";
    while ((line = bufferedReader.readLine()) != null) {
    natija += chiziq;
    }
    bufferedReader.close();
    inputStream.close();
    httpURLConnection.disconnect();
    natijani qaytarish;

    } catch (MalformedURLException e) {


    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }

    nullni qaytarish;


    }

    @Override


    himoyalangan bekor onPreExecute() {
    alertDialog = new AlertDialog.Builder(context).create();
    alertDialog.setTitle("Kirish holati");
    }

    @Override


    himoyalangan void onPostExecute (String natijasi) {

    alertDialog.setMessage(natija);


    alertDialog.show();
    //Toast.makeText(context.getApplicationContext(),natija,Toast.LENGTH_LONG).show();

    }
    @Override


    himoyalangan void onProgressUpdate (Void... qiymatlari) {
    super.onProgressUpdate(qiymatlar);
    }
    }
    6-qadam: MainActivity.java- ga kodni yozing :
    com.example.a06_mysql_reg_login paketi;

    import androidx.appcompat.app.AppCompatActivity;


    android.os.Bundle import;


    android.view.View import;
    import android.widget.Button;
    android.widget.EditText import;

    umumiy klass MainActivity AppCompatActivity-ni kengaytiradi {


    EditText nameRegister;


    EditText foydalanuvchi nomi Ro'yxatdan o'tish;
    EditText passwordRegister;
    Registr tugmasi;

    EditText usernameLogin;


    EditText passwordLogin;
    Kirish tugmasi;

    @Override


    himoyalangan void onCreate (to'plam savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    nameRegister = findViewById(R.id.editText1);
    usernameRegister = findViewById(R.id.editText2);
    passwordRegister = findViewById(R.id.editText3);
    registratsiya = findViewById(R.id.button1);

    usernameLogin = findViewById(R.id.editText4);


    passwordLogin = findViewById(R.id.editText5);
    login = findViewById(R.id.button2);
    register.setOnClickListener(yangi View.OnClickListener() {
    @Override
    public void onClick(View v) {
    String nomi = nameRegister.getText().toString();
    String username = usernameRegister.getText().toString();
    String parol = passwordRegister.getText().toString();
    String url = "http://192.168.43.41/ArinProject/ANDROID_MYSQL/insertData.php";
    String turi = "ro'yxatdan o'tish";
    BackgroundWorker backgroundWorker = yangi BackgroundWorker(MainActivity.this);
    backgroundWorker.execute (url, turi, nomi, foydalanuvchi nomi, parol);
    }
    });

    login.setOnClickListener(yangi View.OnClickListener() {


    @Override
    public void onClick(View v) {
    String nomi = nameRegister.getText().toString();
    String username = usernameLogin.getText().toString();
    String parol = passwordLogin.getText().toString();
    String url = "http://192.168.43.41/ArinProject/ANDROID_MYSQL/validateData.php";
    String turi = "login";
    BackgroundWorker backgroundWorker = yangi BackgroundWorker(MainActivity.this);
    backgroundWorker.execute (url, turi, nomi, foydalanuvchi nomi, parol);
    }
    });


    }
    }
    Download 242,56 Kb.




    Download 242,56 Kb.