Langsung ke konten utama

Menambah Data Baru MySQL

Web Service: connect_db.php
<?php
$link = mysql_connect('localhost', 'root', '') or die('Cannot connect to the DB');
mysql_select_db('dbcontoh') or die('Cannot select the DB');   
?>

Web Service: setAccount.php
<?php
$username = $_GET['username'];
$password = $_GET['password'];

include "connect_db.php";

$query = "insert into tbaccount (username,password) values('".$username."','".$password."')";
$result = mysql_query($query, $link) or die('Error query:  '.$query);

echo "data berhasil disimpan";

?>

Layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:gravity="center_horizontal"    android:orientation="vertical" >
    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="vertical" >
        <TextView            android:id="@+id/textView1"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="Username" />
        <EditText            android:id="@+id/editTextUserName"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:ems="10" >
            <requestFocus />
        </EditText>
        <TextView            android:id="@+id/textView2"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="Password" />
        <EditText            android:id="@+id/editTextPassword"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:ems="10"            android:inputType="textPassword" />
    </LinearLayout>
    <Button        android:id="@+id/buttonSignup"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="SIGN UP" />
</LinearLayout>

Activity
package adehodijah.example.com.pertemuanmysql;

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

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import cz.msebera.android.httpclient.HttpResponse;
import cz.msebera.android.httpclient.client.HttpClient;
import cz.msebera.android.httpclient.client.methods.HttpGet;
import cz.msebera.android.httpclient.impl.client.DefaultHttpClient;


public class AddActivity extends AppCompatActivity implements View.OnClickListener{
    private EditText txtUser;
    private EditText txtPassword;
    private Button buttonSignup;
    public static final String URL_SETACCOUNT = "http://192.168.43.78:8080/android/setAccount.php"; //koneksi database ke localhost
    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add);
        txtUser = (EditText) findViewById(R.id.editTextUserName);
        txtPassword = (EditText) findViewById(R.id.editTextPassword);
        buttonSignup = (Button) findViewById(R.id.buttonSignup);
        buttonSignup.setOnClickListener(this);
    }

    //Method untuk Mengirimkan data keserver    public String getRequest(String Url){
        String sret;
        HttpClient client = new DefaultHttpClient();
        HttpGet request = new HttpGet(Url);
        try{
            HttpResponse response = client.execute(request);
            sret= request(response);
        }catch(Exception ex){
            sret= "Failed Connect to server!";
        }
        return sret;

    }
    //Method untuk Menerima data dari server    public static String request(HttpResponse response){
        String result = "";
        try{
            InputStream in = response.getEntity().getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(in));
            StringBuilder str = new StringBuilder();
            String line = null;
            while((line = reader.readLine()) != null){
                str.append(line + "\n");
            }
            in.close();
            result = str.toString();
        }catch(Exception ex){
            result = "Error";
        }
        return result;
    }

    @Override    public void onClick(View v) {
        CallWebPageTask task = new CallWebPageTask();
        task.applicationContext = AddActivity.this;
        String url =URL_SETACCOUNT+"?username="+txtUser.getText().toString()+"&password="+txtPassword.getText().toString();
        task.execute(new String[] { url });
    }

    //Class CallWebPageTask untuk implementasi class AscyncTask    private class CallWebPageTask extends AsyncTask<String, Void, String> {

        private ProgressDialog dialog;
        protected Context applicationContext;

        @Override        protected void onPreExecute() {
            this.dialog = ProgressDialog.show(applicationContext, "Login Process", "Please Wait...", true);
        }

        @Override        protected String doInBackground(String... urls) {
            String response = "";
            response = getRequest(urls[0]);
            return response;
        }

        @Override        protected void onPostExecute(String result) {
            this.dialog.cancel();
            txtUser.setText("");
            txtPassword.setText("");
            Toast.makeText(AddActivity.this, result, Toast.LENGTH_LONG).show();
        }
    }
}

Komentar

Postingan populer dari blog ini

Menampilkan Data MySQL

Web Service: getAccount.php <?php include "connect_db.php"; $query = "SELECT username,password FROM tbaccount"; $result = mysql_query($query, $link) or die('Errorquery:  '.$query); $rows = array(); while ($r = mysql_fetch_assoc($result)) {     $rows[] = $r; } $data = "{Account:".json_encode($rows)."}"; echo $data; ?> Layout Adapter <? xml version= "1.0" encoding= "utf-8" ?> < LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android :layout_width= "fill_parent" android :layout_height= "fill_parent" android :orientation= "vertical" > < LinearLayout android :layout_width= "fill_parent" android :layout_height= "fill_parent" android :layout_weight= "1" android :orientation= "horizontal" > < TextView android...