Android Php ile Online Veritabanı İşlemleri

OnlineDBIslemler Tamamını İndir

package com.example.ali.onlinedbkullanim;

import android.content.Context;
import android.content.DialogInterface;
import android.net.Uri;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;


import org.json.JSONArray;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity implements View.OnClickListener,AdapterView.OnItemClickListener{
    Context c;
    EditText tad,tgun;
    Button btn;
    int[] id;
    String[] ad;
    ListView lv;
    ArrayList<String> liste;
    ArrayAdapter<String> adp;
    AlertDialog.Builder dialog;
    int secilen_id;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        c=this;
        liste=new ArrayList<String>();
        lv=(ListView)findViewById(R.id.list);
        lv.setOnItemClickListener(this);
        adp=new ArrayAdapter<String>(c,android.R.layout.simple_list_item_1,liste);
        lv.setAdapter(adp);
        btn=(Button)findViewById(R.id.btn_ekle);
        btn.setOnClickListener(this);
        tad=(EditText)findViewById(R.id.tad);
        veri_getir();

    }
    public  void post(final Uri.Builder builder)
    {

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    URL url = new URL("http://alikomesogutlu.com/db.php");
                    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                    conn.setReadTimeout(10000);
                    conn.setConnectTimeout(15000);
                    conn.setRequestMethod("POST");
                    conn.setDoInput(true);
                    conn.setDoOutput(true);

                    String query = builder.build().getEncodedQuery();

                    OutputStream os = conn.getOutputStream();//bağlantının cıkısı
                    BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
                    writer.write(query);
                    writer.flush();
                    writer.close();
                    os.close();

                }
                catch (Exception ex)
                {
                    Log.d("hata",ex.getMessage());
                }
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        veri_getir();
                    }
                });
            }
        }).start();
    }
public void veri_getir()
{
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    URL url = new URL("http://alikomesogutlu.com/db.php");
                    HttpURLConnection conn = (HttpURLConnection) url.openConnection();

                    StringBuilder builder=new StringBuilder();
                    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(conn.getInputStream()));

                    String json="";

                    while ((json = bufferedReader.readLine()) != null)
                        builder.append(json + "\n");

                    JSONArray jsonArray = new JSONArray(builder.toString().trim());
                     id = new int[jsonArray.length()];
                     ad = new String[jsonArray.length()];

                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject obj = jsonArray.getJSONObject(i);

                        id[i] = obj.getInt("id");
                        ad[i]=obj.getString("ad");
                    }
                }
                catch (Exception ex)
                {
                    Log.d("hata",ex.getMessage());
                }
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                     liste.clear();
                     adp.notifyDataSetChanged();
                    for (int i=0;i<id.length;i++)
                        adp.add(ad[i]);
                    }
                });
            }
        }).start();
}
    @Override
    public void onClick(View view) {
        if(tad.getText().length()>0)
        {
            Uri.Builder builder = new Uri.Builder();//post paremetreleri ayarlanıyor
            builder.appendQueryParameter("ad",tad.getText()+"");
            builder.appendQueryParameter("durum", "0");
            post(builder);
            tad.setText("");
        }

    }
    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        dialog=new AlertDialog.Builder(c);
        tgun=new EditText(c);
        tgun.setText(liste.get(i));
        secilen_id=id[i];
        dialog.setView(tgun);
        dialog.setTitle("Güncelle/Sil");
        dialog.setPositiveButton("Sil", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                Uri.Builder builder = new Uri.Builder();//post paremetreleri ayarlanıyor
                builder.appendQueryParameter("id",secilen_id+"");
                builder.appendQueryParameter("durum", "1");
                post(builder);
            }
        });
        dialog.setNegativeButton("Güncelle", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                if(tgun.getText().length()>0) {
                    Uri.Builder builder = new Uri.Builder();//post paremetreleri ayarlanıyor
                    builder.appendQueryParameter("ad", tgun.getText() + "");
                    builder.appendQueryParameter("durum", "2");
                    builder.appendQueryParameter("id", secilen_id + "");
                    post(builder);
                }
                else
                    Toast.makeText(c,"Alan Boş bırakılamaz!!",Toast.LENGTH_LONG).show();
            }
        });
        dialog.create().show();
    }
}
<span id="mce_marker" data-mce-type="bookmark" data-mce-fragment="1">​</span>
<?php
 function conn(){
	 try{
	 return	$baglanti=new PDO('mysql:host=localhost;port=3306;dbname=Veritabanı ismi;charset=utf8',"Kulanıcı ismi","Şifre");
	}
	catch(PDOException $ex)
	{
		$ex->getMessage();
	}
 }
 
 function insert($ad)
 {
	 try{
		 $baglanti=conn();
	$sorgu1=$baglanti->prepare("insert into kisiler(ad) values(?)");
    $sorgu1->bindParam(1,$ad);
    $sorgu1->execute();
    $baglanti=null;
	}
	catch(PDOException $ex)
	{
		$ex->getMessage();
	}
 }
 function get_data()
 {
	 try{
		 $baglanti=conn();
	$sorgu=$baglanti->prepare("select * from kisiler order by id desc");
    $sorgu->execute();
	$veri=$sorgu->fetchAll(PDO::FETCH_ASSOC);
    $dizi;
	$sayac=0;
	foreach($veri as $deger)
	{
		$dizi[$sayac++]=['id'=>$deger["id"],'ad'=>$deger["ad"]];
	}
	$baglanti=null;
	echo json_encode($dizi);
	}
	catch(PDOException $ex)
	{
		$ex->getMessage();
	}
 }
  function update($ad,$id)
 {
	 try{
		 $baglanti=conn();
	$sorgu1=$baglanti->prepare("update kisiler set ad=? where id=?");
    $sorgu1->bindParam(1,$ad);
	$sorgu1->bindParam(2,$id);
    $sorgu1->execute();
    $baglanti=null;
	}
	catch(PDOException $ex)
	{
		$ex->getMessage();
	}
 }
  function data_delete($id)
 {
	 try{
		 $baglanti=conn();
	$sorgu1=$baglanti->prepare("delete from kisiler where id=?");
	$sorgu1->bindParam(1,$id);
    $sorgu1->execute();
    $baglanti=null;
	}
	catch(PDOException $ex)
	{
		$ex->getMessage();
	}	 
 }
  if(isset($_POST['ad'])&&isset($_POST['durum'])&&$_POST['durum']=='0')
 {
    insert($_POST['ad']);	 
 }
 else if(isset($_POST['id'])&&isset($_POST['durum'])&&$_POST['durum']=='1')
 {
    data_delete($_POST['id']);
 }
 else if(isset($_POST['ad'])&&isset($_POST['id'])&&isset($_POST['durum'])&&$_POST['durum']=='2')
 {
    update($_POST['ad'],$_POST['id']);
 }
 else{
	get_data();
 }
?>

 

Bir cevap yazın

E-posta hesabınız yayımlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir