Twitter Facebook Delicious Digg Stumbleupon Favorites More

Tuesday, 25 July 2017

online registration and find in android database


1)main activity.java
package com.example.viraj.demodatabase;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public void register(View view)
    {
        Intent intent=new Intent(MainActivity.this,Register.class);
        startActivity(intent);
    }
}
2)register.xmlpage
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    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"
    tools:context="com.example.viraj.demodatabase.Register">

    <EditText
        android:id="@+id/id_name"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="50dp"
        android:ems="10"
        android:hint="NAME"
        android:inputType="textPersonName"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp" />

    <EditText
        android:id="@+id/id_password"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="24dp"
        android:ems="10"
        android:hint="PASWORD"
        android:inputType="textPersonName"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/id_name"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp" />

    <EditText
        android:id="@+id/id_contact"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="24dp"
        android:ems="10"
        android:inputType="textPersonName"
        android:hint="CONTACT"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/id_password"
        app:layout_constraintHorizontal_bias="0.0"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp" />

    <EditText
        android:id="@+id/id_country"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="24dp"
        android:ems="10"
        android:inputType="textPersonName"
        android:hint="COUNTRY"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/id_contact"
        app:layout_constraintHorizontal_bias="0.0"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp" />

    <Button
        android:id="@+id/id_button"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="32dp"
        android:onClick="reguser"
        android:text="Button"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/id_country"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp" />

    <EditText
        android:id="@+id/id_find_name"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:ems="10"
        android:hint="NAME"
        android:inputType="textPersonName"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/id_button" />

    <Button
        android:id="@+id/id_find_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="8dp"
        android:layout_marginTop="24dp"
        android:onClick="finddata"
        android:text="Button"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/id_find_name"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp"
        app:layout_constraintLeft_toRightOf="@+id/id_find_password"
        android:layout_marginLeft="8dp" />

    <EditText
        android:id="@+id/id_find_password"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="36dp"
        android:layout_marginTop="29dp"
        android:ems="10"
        android:hint="PASSWORD"
        android:inputType="textPersonName"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/id_find_name" />
</android.support.constraint.ConstraintLayout>


3)register .java page
package com.example.viraj.demodatabase;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Register extends AppCompatActivity {
    EditText e_name, e_password, e_contact, e_country, find_name,find_mobilenumber ;
    Button e_button,find_button;
    String name, password, country, contact,f_name,f_pass;

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

        e_name = (EditText) findViewById(R.id.id_name);
        e_password = (EditText) findViewById(R.id.id_password);
        e_contact = (EditText) findViewById(R.id.id_contact);
        e_country = (EditText) findViewById(R.id.id_country);
       //-----------------------------------------------------
        find_name = (EditText) findViewById(R.id.id_find_name);
        find_mobilenumber=(EditText)findViewById(R.id.id_find_password);
        find_button=(Button)findViewById(R.id.id_button);



    }
//mainfeast me internet use ki permission bhi de
    public void reguser(View view) {
        name = e_name.getText().toString();
        password = e_password.getText().toString();
        contact = e_contact.getText().toString();
        country = e_country.getText().toString();
        //to cALL BACKGROUNDTASK FORM METHOD WE CREATE THIS 1)
       // Toast.makeText(getApplicationContext(),name,Toast.LENGTH_LONG).show();
        String method ="Register";
                //object of background task
        Backgroundtask backgroundtask=new Backgroundtask(this);
        backgroundtask.execute(method,name,password,contact,country);
        //finish();

    }

    public void finddata(View view) {
        f_name=find_name.getText().toString();
        f_pass=find_mobilenumber.getText().toString();
        String method ="find_transfer_code";
        //object of background task
        Backgroundtask backgroundtask=new Backgroundtask(this);
        backgroundtask.execute(method,f_name,f_pass);
    }
}

4)backgroundtask page java
package com.example.viraj.demodatabase;

import android.content.Context;
import android.os.AsyncTask;
import android.support.v7.app.AlertDialog;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;

/**
 * Created by viraj on 26-Jun-17.
 */

public class Backgroundtask extends AsyncTask<String, Void, String> {
    //constructor created
    Context ctx;
    AlertDialog alertDialog;

    Backgroundtask(Context ctx) {
        this.ctx = ctx;
    }


    @Override
    protected void onPreExecute() {
        alertDialog = new AlertDialog.Builder(ctx).create();
        alertDialog.setTitle("CONTACT INFORMATION");

    }


    @Override
    //sab jagha string kara he void se change karke
    protected String doInBackground(String... params) {
//http://localhost/udemy/register.php http://10.0.2.2/udemy/register.php http://192.168.56.1/udemy/register.php  String reg_url = "https://applicatory-shadow.000webhostapp.com/register.php ";
        // String reg_url = "https://applicatory-shadow.000webhostapp.com/register.php ";
        //insert data ke liye php page
        String reg_url = "http://192.168.56.1/udemy/register.php";
        //find ke liye php page
        String find_url = "http://192.168.56.1/udemy/login.php";


        //EXTRACT KAR RAHE HE METHOD SE
        String method = params[0];//this param 0 is for method

        if (method.equals("Register")) {

            String name = params[1];
            String password = params[2];
            String contact = params[3];
            String country = params[4];

            try {
                //create object of url
                URL url = new URL(reg_url);
                //OBJECT OF HTTP URL CONN
                HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                httpURLConnection.setRequestMethod("POST");
                httpURLConnection.setDoOutput(true);
                OutputStream os = httpURLConnection.getOutputStream();
                BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
                String data = URLEncoder.encode("name", "UTF-8") + "=" + URLEncoder.encode(name, "UTF-8") + "&" +
                        URLEncoder.encode("password", "UTF-8") + "=" + URLEncoder.encode(password, "UTF-8") + "&" +
                        URLEncoder.encode("contact", "UTF-8") + "=" + URLEncoder.encode(contact, "UTF-8") + "&" +
                        URLEncoder.encode("country", "UTF-8") + "=" + URLEncoder.encode(country, "UTF-8");
                bufferedWriter.write(data);
                bufferedWriter.flush();
                bufferedWriter.close();
                os.close();
                InputStream IS = httpURLConnection.getInputStream();
                IS.close();
                return "REGISTRATION SUCESS";
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
//niche code same he
        else if (method.equals("find_transfer_code")) {

            String find_name = params[1];
            String find_password = params[2];


            try {
                //create object of url find url
                URL url = new URL(find_url);
                //OBJECT OF HTTP URL CONN
                HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                httpURLConnection.setRequestMethod("POST");
                httpURLConnection.setDoOutput(true);
                OutputStream os = httpURLConnection.getOutputStream();
                BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
                String data = URLEncoder.encode("name", "UTF-8") + "=" + URLEncoder.encode(find_name, "UTF-8") + "&" +
                        URLEncoder.encode("password", "UTF-8") + "=" + URLEncoder.encode(find_password, "UTF-8");
                bufferedWriter.write(data);
                bufferedWriter.flush();
                bufferedWriter.close();
                os.close();
                InputStream IS = httpURLConnection.getInputStream();
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(IS, "iso-8859-1"));
                String response = "";
                String line = "";
                while ((line = bufferedReader.readLine()) != null) {
                    response += line;
                }
                bufferedReader.close();
                IS.close();
                httpURLConnection.disconnect();
                return response;


            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }


    @Override
    //sab jagha string kara he void se change karke
    protected void onPostExecute(String result1) {
        if (result1.equals("REGISTRATION SUCESS"))
        {
            Toast.makeText(ctx, result1, Toast.LENGTH_LONG).show();
        }
        else
            {
            alertDialog.setMessage(result1);
            alertDialog.show();
        }}


    @Override
    protected void onProgressUpdate(Void... values) {
        super.onProgressUpdate(values);
    }}

Share:

0 comments:

Post a Comment

Blogger Tutorials

Blogger Templates

Sample Text

Copyright © ANDROID TUTORIAL CODE | Powered by Blogger
Design by SimpleWpThemes | Blogger Theme by NewBloggerThemes.com & Distributed By Protemplateslab