Assalammualaikum wr. wb
Pada kesempatan kali ini saya akan membagikan tutorial cara meng-Get data dari web ke android. oke langsung saja
Pada kesempatan kali ini saya akan membagikan tutorial cara meng-Get data dari web ke android. oke langsung saja
Langkah pertama, kita menyiapkan database dengan nama penjualan. kemudian kita membuat kodiangan koneksi.php
Kodingannya:
$username = “root”;
$password =””;
$database =”penjualan”;
mysql_connect($server, $username, $password) or die(“Koneksi tidak ada”);
mysql_select_db($database) or die(“Database tidak ditemukan”); ?>
<?php $server =”localhost”;$username = “root”;
$password =””;
$database =”penjualan”;
mysql_connect($server, $username, $password) or die(“Koneksi tidak ada”);
mysql_select_db($database) or die(“Database tidak ditemukan”); ?>
simpan dengan format koneksi.php
kemudian kita membuat getdata.php
Kodingannya:
<?php
include ‘koneksi.php’;
$hasil = mysql_query(“SELECT * FROM data”) or die(mysql_error());
$json_response = array();
include ‘koneksi.php’;
$hasil = mysql_query(“SELECT * FROM data”) or die(mysql_error());
$json_response = array();
if(mysql_num_rows($hasil)> 0){
while ($row = mysql_fetch_array($hasil)) {
$json_response[] = $row;
}
echo json_encode(array(‘Data’ => $json_response));
}
?>
while ($row = mysql_fetch_array($hasil)) {
$json_response[] = $row;
}
echo json_encode(array(‘Data’ => $json_response));
}
?>
simpan dengan format getData.php
setelah kita membuat kodingan koneksi dan get data pada web. langkah selanjutnya kita membuat tampilan pada android
pertama kita membuat kodingan activity_main.xml :
kodingan:
kodingan:
<?xml version=”1.0″ encoding=”utf-8″?>
<RelativeLayout 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:id=”@+id/activity_main”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:paddingBottom=”@dimen/activity_vertical_margin”
android:paddingLeft=”@dimen/activity_horizontal_margin”
android:paddingRight=”@dimen/activity_horizontal_margin”
android:paddingTop=”@dimen/activity_vertical_margin”
tools:context=”android.tracerfamily.MainActivity”>
<RelativeLayout 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:id=”@+id/activity_main”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:paddingBottom=”@dimen/activity_vertical_margin”
android:paddingLeft=”@dimen/activity_horizontal_margin”
android:paddingRight=”@dimen/activity_horizontal_margin”
android:paddingTop=”@dimen/activity_vertical_margin”
tools:context=”android.tracerfamily.MainActivity”>
<ImageView
android:layout_width=”match_parent”
android:layout_height=”300dp”
app:srcCompat=”@mipmap/ic_launcher”
android:layout_alignParentTop=”true”
android:layout_alignParentLeft=”true”
android:layout_alignParentStart=”true”
android:id=”@+id/txtnama” />
android:layout_width=”match_parent”
android:layout_height=”300dp”
app:srcCompat=”@mipmap/ic_launcher”
android:layout_alignParentTop=”true”
android:layout_alignParentLeft=”true”
android:layout_alignParentStart=”true”
android:id=”@+id/txtnama” />
<TextView
android:text=”Email"
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:layout_below=”@+id/imghp”
android:layout_alignParentLeft=”true”
android:layout_alignParentStart=”true”
android:layout_marginTop=”16dp”
android:id=”@+id/txtemail” />
android:text=”Email"
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:layout_below=”@+id/imghp”
android:layout_alignParentLeft=”true”
android:layout_alignParentStart=”true”
android:layout_marginTop=”16dp”
android:id=”@+id/txtemail” />
<TextView
android:text=”Username”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:layout_below=”@+id/txtmerk”
android:layout_alignParentLeft=”true”
android:layout_alignParentStart=”true”
android:layout_marginTop=”16dp”
android:id=”@+id/txtusername” />
android:text=”Username”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:layout_below=”@+id/txtmerk”
android:layout_alignParentLeft=”true”
android:layout_alignParentStart=”true”
android:layout_marginTop=”16dp”
android:id=”@+id/txtusername” />
<TextView
android:text=”Password”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:layout_below=”@+id/txttipe”
android:layout_alignParentLeft=”true”
android:layout_alignParentStart=”true”
android:layout_marginTop=”19dp”
android:id=”@+id/txtpassword” />
</RelativeLayout>
android:text=”Password”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:layout_below=”@+id/txttipe”
android:layout_alignParentLeft=”true”
android:layout_alignParentStart=”true”
android:layout_marginTop=”19dp”
android:id=”@+id/txtpassword” />
</RelativeLayout>
selanjutnya kita akan membuat logika program java, buka MainActivity.java
kodingannya:
kodingannya:
package android.tracerfamily;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
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 com.bumptech.glide.Glide;
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 com.bumptech.glide.Glide;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashMap;
public class MainActivity extends AppCompatActivity {
private ImageView imghp;
private TextView txtmerk, txttipe, txtketerangan;
private TextView txtmerk, txttipe, txtketerangan;
private RequestQueue requestQueue;
private StringRequest stringRequest;
private StringRequest stringRequest;
ArrayList<HashMap<String, String>> list_data;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String url = “http://10.0.2.2/penjualan/getdata.php”;
txtnama = (TextView)findViewById(R.id.nama);
txtemail = (TextView)findViewById(R.id.email);
txtusername = (TextView)findViewById(R.id.username);
txtpassword = (TextView)findViewById(R.id.password);
txtemail = (TextView)findViewById(R.id.email);
txtusername = (TextView)findViewById(R.id.username);
txtpassword = (TextView)findViewById(R.id.password);
requestQueue = Volley.newRequestQueue(MainActivity.this);
list_data = new ArrayList<HashMap<String, String>>();
stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonObject = new JSONObject(response);
JSONArray jsonArray = jsonObject.getJSONArray(“Data”);
for (int a = 0; a < jsonArray.length(); a ++){
JSONObject json = jsonArray.getJSONObject(a);
HashMap<String, String> map = new HashMap<String, String>();
map.put(“nama”, json.getString(“nama”));
map.put(“email”, json.getString(“email”));
map.put(“username”, json.getString(“username”));
map.put(“password”, json.getString(“password”));
map.put(“keterangan”, json.getString(“keterangan”));
list_data.add(map);
}
Glide.with(getApplicationContext())
.load(“http://10.0.2.2/penjualan/img/” + list_data.get(0).get(“foto”))
.crossFade()
.placeholder(R.mipmap.ic_launcher)
.into(imgfoto);
txtmerk.setText(list_data.get(0).get(“nama”));
txttipe.setText(list_data.get(0).get(“alamat”));
txtketerangan.setText(list_data.get(0).get(“keterangan”));
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this, error.getMessage(), Toast.LENGTH_SHORT).show();
}
});
@Override
public void onResponse(String response) {
try {
JSONObject jsonObject = new JSONObject(response);
JSONArray jsonArray = jsonObject.getJSONArray(“Data”);
for (int a = 0; a < jsonArray.length(); a ++){
JSONObject json = jsonArray.getJSONObject(a);
HashMap<String, String> map = new HashMap<String, String>();
map.put(“nama”, json.getString(“nama”));
map.put(“email”, json.getString(“email”));
map.put(“username”, json.getString(“username”));
map.put(“password”, json.getString(“password”));
map.put(“keterangan”, json.getString(“keterangan”));
list_data.add(map);
}
Glide.with(getApplicationContext())
.load(“http://10.0.2.2/penjualan/img/” + list_data.get(0).get(“foto”))
.crossFade()
.placeholder(R.mipmap.ic_launcher)
.into(imgfoto);
txtmerk.setText(list_data.get(0).get(“nama”));
txttipe.setText(list_data.get(0).get(“alamat”));
txtketerangan.setText(list_data.get(0).get(“keterangan”));
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this, error.getMessage(), Toast.LENGTH_SHORT).show();
}
});
requestQueue.add(stringRequest);
}
}
}
}
Hasilnya :
itulah tutorial get data dari saya, apabila ada kekurangan mohon dimaafkan.
Wassalammualaiku wr. wb
Tidak ada komentar:
Posting Komentar