Asyntack
Asyntack adalah sebuah class yang memungkinkan kita dalam melakukan sebuah operasi pada data secara background, Asyntack punya 3 method yaitu "onPreExecute()","doInBackground(String... args) ",dan " onPostExecute() ",dan untuk menjalankan Asyntack, kita harus memanggil method "execute
Oke tanpa panjang lebar, mari kita buat projectnya, langkah awal kita buat tablenya dulu.
()"
Oke tanpa panjang lebar, mari kita buat projectnya, langkah awal kita buat tablenya dulu.
/*
Navicat MySQL Data Transfer
Source Server : mysql
Source Server Version : 50018
Source Host : localhost:3306
Source Database : surat_pos
Target Server Type : MYSQL
Target Server Version : 50018
File Encoding : 65001
Date: 2015-05-27 12:21:52
*/
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for `surat_masuk`
-- ----------------------------
DROP TABLE IF EXISTS `surat_masuk`;
CREATE TABLE `surat_masuk` (
`no_surat` int(30) NOT NULL,
`kode_mitra` int(30) NOT NULL,
`tgl_suratmasuk` varchar(30) NOT NULL,
PRIMARY KEY (`no_surat`),
KEY `kode_mitra` (`kode_mitra`),
KEY `no_surat` (`no_surat`),
KEY `no_surat_2` (`no_surat`),
KEY `no_surat_3` (`no_surat`),
KEY `no_surat_4` (`no_surat`),
KEY `no_surat_5` (`no_surat`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- ----------------------------
-- Records of surat_masuk
-- ----------------------------
INSERT INTO surat_masuk VALUES ('1', '2', '11-1-2014');
INSERT INTO surat_masuk VALUES ('22', '2', '2015-05-01');
<?php
$link = mysql_connect('localhost', 'root', 'root') or die('Cannot connect to the DB');
mysql_select_db('surat_pos', $link) or die('Cannot select the DB');
/* grab the posts from the db */
$query = "SELECT * FROM Surat_masuk";
$result = mysql_query($query, $link) or die('Errorquery: '.$query);
$rows = array();
while ($r = mysql_fetch_assoc($result))
{
$rows[] = str_replace('{=}','', $r);
}
$data = "{master:".json_encode($rows)."}";
echo $data;
?>
package com.example.demo_asyntack;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;
import android.util.Log;
public class JSONfunctions
{
public static JSONObject getJSONfromURL(String url){
InputStream is = null;
String result = "";
JSONObject jArray = null;
//http post
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
}
//convert response to string
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result=sb.toString();
}catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}
try{
jArray = new JSONObject(result);
}catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
}
return jArray;
}
}
package com.example.demo_asyntack;
import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
public class MainActivity extends ListActivity
{
ListAdapter adapter;
private ProgressDialog pDialog;
static String in_judul = "judul";
static String in_waktu = "tanggal";
JSONArray Arjson = null;
public String konek;
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new Load_data().execute();
}
class Load_data extends AsyncTask<String, String, String>
{
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Loading...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
protected String doInBackground(String... args)
{
String link_url ="http://10.0.2.2/POS/surat_masuk.php";
JSONfunctions jParser = new JSONfunctions();
JSONObject json = jParser.getJSONfromURL(link_url);
try {
Arjson = json.getJSONArray("master");
for(int i = 0; i < Arjson.length(); i++)
{
JSONObject jsonobj = Arjson.getJSONObject(i);
HashMap<String, String> map = new HashMap<String, String>();
map.put("nomor_surat", jsonobj.getString("no_surat"));
map.put("Kode_mitra", jsonobj.getString("kode_mitra"));
map.put("Tgl_surat_masuk", jsonobj.getString("tgl_suratmasuk"));
mylist.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String file_url)
{
pDialog.dismiss();
runOnUiThread(new Runnable()
{
public void run()
{
ListAdapter adapter = new SimpleAdapter
(
MainActivity.this, mylist,R.layout.row_daftar_stat_surat,
new String[] {"nomor_surat","Kode_mitra","Tgl_surat_masuk"},
new int[] { R.id.no_surat,R.id.kd_mitra,R.id.tgl_surat});
setListAdapter(adapter);
}
});
}
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="@id/android:list"
android:layout_width="match_parent"
android:layout_height="468dp"
android:layout_weight="0.77"
android:drawSelectorOnTop="false"
android:textColor="#FFFFFF" >
</ListView>
</LinearLayout>
<?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="50dp"
android:background="@drawable/round_green"
android:orientation="vertical"
android:padding="7dp" >
<TextView
android:id="@+id/TextView01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="2dp"
android:text="No Surat :"
android:textColor="#ffffff"
android:textSize="10sp" />
<TextView
android:id="@+id/no_surat"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#ffffff"
android:textSize="15sp" />
<TextView
android:id="@+id/kd_mitra"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="2dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#00FF00"
android:textSize="0sp" />
<TextView
android:id="@+id/tgl_surat"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="2dp"
android:textColor="#00FF00"
android:textSize="0sp" />
<TextView
android:id="@+id/tgl_diterima"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="2dp"
android:textColor="#00FF00"
android:textSize="0sp" />
<TextView
android:id="@+id/pengirim"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="2dp"
android:textColor="#00FF00"
android:textSize="0sp" />
<TextView
android:id="@+id/alamat_pengirim"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="2dp"
android:textColor="#00FF00"
android:textSize="0sp" />
<TextView
android:id="@+id/penerima"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="2dp"
android:textColor="#00FF00"
android:textSize="0sp" />
<TextView
android:id="@+id/alamat_penerima"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="2dp"
android:textColor="#00FF00"
android:textSize="0sp" />
<TextView
android:id="@+id/Status"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="2dp"
android:textColor="#00FF00"
android:textSize="0sp" />
<TextView
android:id="@+id/Keterangan"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="2dp"
android:textColor="#00FF00"
android:textSize="0sp" />
</LinearLayout>
Komentar
Posting Komentar