Jumat, 20 September 2019

JSON Parsing 0

<?php

$respon = array();
$respon["sukses"] = 0;
$respon["pesan"] = "0 record";
echo json_encode($respon);

?>

<?php
$record = array();
            $record["kode_mahasiswa"] = "Kode 1";
    $record["nama"] = "Nama 1";

            $respon["sukses"] = 1;
            $respon["record"] = array();

            array_push($respon["record"], $record);
            $respon["pesan"] = "$jum record";
    echo json_encode($respon);
?>

++++++++++++++++

package com.example.pelayanananggotakspsi;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

public class JSONParser {

static InputStream is = null;
static JSONObject jObj = null;
static String json = "";

public JSONParser() {}

String getIP() {
return "http://192.168.1.11/Siswa/distrokids/";
}

public JSONObject makeHttpRequest(String url, String method,List<NameValuePair> params) {
try {
if(method == "POST"){
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}
else if(method == "GET"){
DefaultHttpClient httpClient = new DefaultHttpClient();
String paramString = URLEncodedUtils.format(params, "utf-8");
url += "?" + paramString;
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}

} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
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();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}

try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}

return jObj;
}
}


+++++++++++++++++++++++++++


package com.example.pelayanananggotakspsi;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Html;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class Detail_anggota extends Activity {
String ip="";
String kode_anggota;
String kode_anggota0="";
EditText txtkode_anggota;
EditText txtnama_anggota;
EditText txtnik;
EditText txttgl_lahir;
EditText txtjenis_kelamin;
EditText txtdepartemen;
EditText txttelepon;
EditText txtemail;
EditText txtalamat;
EditText txtusername;
EditText txtpassword;
EditText txtgambar;
EditText txtketerangan;
EditText txtstatus;
Button btnProses;
Button btnHapus;

private ProgressDialog pDialog;
JSONParser jsonParser = new JSONParser();

private static final String TAG_SUKSES = "sukses";
private static final String TAG_record = "record";
private static final String TAG_nama_anggota = "nama_anggota";
private static final String TAG_nik = "nik";
private static final String TAG_tgl_lahir= "tgl_lahir";
private static final String TAG_jenis_kelamin = "jenis_kelamin";
private static final String TAG_departemen = "departemen";
private static final String TAG_alamat = "alamat";
private static final String TAG_username = "username";
private static final String TAG_password = "password";
private static final String TAG_gambar = "gambar";
private static final String TAG_keterangan = "keterangan";
private static final String TAG_status = "status";

@Override  
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.detail_anggota);
ip=jsonParser.getIP();
callMarquee();
txtkode_anggota = (EditText) findViewById(R.id.txtkode_anggota);
txtnama_anggota= (EditText) findViewById(R.id.txtnama_anggota);
txtalamat= (EditText) findViewById(R.id.txtalamat);
txtnik= (EditText) findViewById(R.id.txtnik);
txttgl_lahir= (EditText) findViewById(R.id.txttgl_lahir);
txtjenis_kelamin= (EditText) findViewById(R.id.txtjenis_kelamin);
txtdepartemen= (EditText) findViewById(R.id.txtdepartemen);
txtusername = (EditText) findViewById(R.id.txtusername);
txtpassword= (EditText) findViewById(R.id.txtpassword);
txtgambar= (EditText) findViewById(R.id.txtgambar);
txtketerangan= (EditText) findViewById(R.id.txtketerangan);
txtstatus= (EditText) findViewById(R.id.txtstatus);
btnProses= (Button) findViewById(R.id.btnproses);
btnHapus = (Button) findViewById(R.id.btnhapus);
Intent i = getIntent(); 
kode_anggota0 = i.getStringExtra("pk");
if(kode_anggota0.length()>0){
new get().execute();
btnProses.setText("Update Data");
btnHapus.setVisibility(View.VISIBLE);
}
else{
btnProses.setText("Add New Data");
btnHapus.setVisibility(View.GONE);
}

btnProses.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
kode_anggota= txtkode_anggota.getText().toString();
String lnama_anggota= txtnama_anggota.getText().toString();
String lalamat= txtalamat.getText().toString();
String lnik= txtnik.getText().toString();
String ltgl_lahir= txttgl_lahir.getText().toString();
String ljenis_kelamin= txtjenis_kelamin.getText().toString();
String ldepartemen= txtdepartemen.getText().toString();
String lusername= txtusername.getText().toString();
String lpassword= txtpassword.getText().toString();
String lgambar= txtgambar.getText().toString();
String lketerangan= txtketerangan.getText().toString();
String lstatus= txtstatus.getText().toString();
if(kode_anggota.length()<1){lengkapi("kode_anggota");}
else if(lnama_anggota.length()<1){lengkapi("nama_anggota");}
else if(lalamat.length()<1){lengkapi("alamat");}
else if(lnik.length()<1){lengkapi("nik");}
else if(ltgl_lahir.length()<1){lengkapi("tgl_lahir");}
else if(ljenis_kelamin.length()<1){lengkapi("jenis_kelamin");}
else if(ldepartemen.length()<1){lengkapi("departemen");}
else if(lusername.length()<1){lengkapi("username");}
else if(lpassword.length()<1){lengkapi("password");}
else if(lgambar.length()<1){lengkapi("gambar");}
else if(lketerangan.length()<1){lengkapi("keterangan");}
else if(lstatus.length()<1){lengkapi("status");}
else{
if(kode_anggota0.length()>0){
new update().execute();
}
else{
new save().execute();
}
}//else
}});

btnHapus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
new del().execute();
}});
}

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
class get extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(Detail_anggota.this);
pDialog.setMessage("Load data detail. Silahkan tunggu...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
protected String doInBackground(String... params) {
int sukses;
try {
List<NameValuePair> params1 = new ArrayList<NameValuePair>();
params1.add(new BasicNameValuePair("kode_anggota", kode_anggota0));
String url=ip+"anggota/anggota_detail.php";
Log.v("detail",url);
JSONObject json = jsonParser.makeHttpRequest(url, "GET", params1);
Log.d("detail", json.toString());
sukses = json.getInt(TAG_SUKSES);
if (sukses == 1) {
JSONArray myObj = json.getJSONArray(TAG_record); // JSON Array
final JSONObject myJSON = myObj.getJSONObject(0);
runOnUiThread(new Runnable() {
public void run() {
try {
txtkode_anggota.setText(kode_anggota0);
txtnama_anggota.setText(myJSON.getString(TAG_nama_anggota));
txtgambar.setText(myJSON.getString(TAG_gambar));
txtketerangan.setText(myJSON.getString(TAG_keterangan));
txtalamat.setText(myJSON.getString(TAG_alamat));
txtnik.setText(myJSON.getString(TAG_nik));
txttgl_lahir.setText(myJSON.getString(TAG_tgl_lahir));
txtjenis_kelamin.setText(myJSON.getString(TAG_jenis_kelamin));
txtdepartemen.setText(myJSON.getString(TAG_departemen));
txtusername.setText(myJSON.getString(TAG_username));
txtpassword.setText(myJSON.getString(TAG_password));
txtstatus.setText(myJSON.getString(TAG_status));
catch (JSONException e) {e.printStackTrace();}
}});
}
else{
// jika id tidak ditemukan
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String file_url) {pDialog.dismiss();}
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

class save extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(Detail_anggota.this);
pDialog.setMessage("Menyimpan data ...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
protected String doInBackground(String... args) {
kode_anggota = txtkode_anggota.getText().toString();
String lnama_anggota= txtnama_anggota.getText().toString();
String lnik= txtnik.getText().toString();
String lalamat= txtalamat.getText().toString();
String ltgl_lahir= txttgl_lahir.getText().toString();
String ljenis_kelamin= txtjenis_kelamin.getText().toString();
String ldepartemen= txtdepartemen.getText().toString();
String lgambar= txtgambar.getText().toString();
String lketerangan= txtketerangan.getText().toString();
String lusername= txtusername.getText().toString();
String lpassword= txtpassword.getText().toString();
String lstatus= txtstatus.getText().toString();
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("kode_anggota0", kode_anggota0));
params.add(new BasicNameValuePair("kode_anggota", kode_anggota));
params.add(new BasicNameValuePair("nama_anggota", lnama_anggota));
params.add(new BasicNameValuePair("alamat", lalamat));
params.add(new BasicNameValuePair("nik", lnik));
params.add(new BasicNameValuePair("tgl_lahir", ltgl_lahir));
params.add(new BasicNameValuePair("jenis_kelamin", ljenis_kelamin));
params.add(new BasicNameValuePair("departemen", ldepartemen));
params.add(new BasicNameValuePair("gambar", lgambar));
params.add(new BasicNameValuePair("keterangan", lketerangan));
params.add(new BasicNameValuePair("username", lusername));
params.add(new BasicNameValuePair("password", lpassword));
params.add(new BasicNameValuePair("status", lstatus));
String url=ip+"anggota/anggota_add.php";
Log.v("add",url);
JSONObject json = jsonParser.makeHttpRequest(url,"POST", params);
Log.d("add", json.toString());
try {
int sukses = json.getInt(TAG_SUKSES);
if (sukses == 1) {
Intent i = getIntent();
setResult(100, i);
finish();
} else {
// gagal update data
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}

protected void onPostExecute(String file_url) {pDialog.dismiss();}
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

class update extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(Detail_anggota.this);
pDialog.setMessage("Mengubah data ...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
protected String doInBackground(String... args) {
kode_anggota = txtkode_anggota.getText().toString();
String lnama_anggota= txtnama_anggota.getText().toString();
String lalamat= txtalamat.getText().toString();
String lgambar= txtgambar.getText().toString();
String lnik= txtnik.getText().toString();
String ltgl_lahir= txttgl_lahir.getText().toString();
String ljenis_kelamin= txtjenis_kelamin.getText().toString();
String ldepartemen= txtdepartemen.getText().toString();
String lketerangan= txtketerangan.getText().toString();
String lusername= txtusername.getText().toString();
String lpassword= txtpassword.getText().toString();
String lstatus= txtstatus.getText().toString();
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("kode_anggota0", kode_anggota0));
params.add(new BasicNameValuePair("kode_anggota", kode_anggota));
params.add(new BasicNameValuePair("nama_anggota", lnama_anggota));
params.add(new BasicNameValuePair("alamat", lalamat));
params.add(new BasicNameValuePair("nik", lnik));
params.add(new BasicNameValuePair("tgl_lahir", ltgl_lahir));
params.add(new BasicNameValuePair("jenis_kelamin", ljenis_kelamin));
params.add(new BasicNameValuePair("departemen", ldepartemen));
params.add(new BasicNameValuePair("gambar", lgambar));
params.add(new BasicNameValuePair("username", lusername));
params.add(new BasicNameValuePair("status", lstatus));
String url=ip+"anggota/anggota_update.php";
Log.v("update",url);
JSONObject json = jsonParser.makeHttpRequest(url,"POST", params);
Log.d("add", json.toString());
try {
int sukses = json.getInt(TAG_SUKSES);
if (sukses == 1) {
Intent i = getIntent();
setResult(100, i);
finish();
} else {
// gagal update data
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}

protected void onPostExecute(String file_url) {pDialog.dismiss();}
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
class del extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(Detail_anggota.this);
pDialog.setMessage("Menghapus data...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}

protected String doInBackground(String... args) {
int sukses;
try {
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("kode_anggota", kode_anggota0));

String url=ip+"anggota/anggota_del.php";
Log.v("delete",url);
JSONObject json = jsonParser.makeHttpRequest(url, "GET", params);
Log.d("delete", json.toString());
sukses = json.getInt(TAG_SUKSES);
if (sukses == 1) {
Intent i = getIntent();
setResult(100, i);
finish();
}
catch (JSONException e) {e.printStackTrace();}
return null;  
}

protected void onPostExecute(String file_url) {pDialog.dismiss();}
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
public void lengkapi(String item){
  new AlertDialog.Builder(this)
  .setTitle("Lengkapi Data")
  .setMessage("Silakan lengkapi data "+item +" !")
  .setNeutralButton("Tutup", new DialogInterface.OnClickListener() {
   public void onClick(DialogInterface dlg, int sumthin) {
    finish();
   }}).show();
 
 
void callMarquee(){
         Calendar cal = Calendar.getInstance();          
         int jam = cal.get(Calendar.HOUR);
         int menit= cal.get(Calendar.MINUTE);
         int detik= cal.get(Calendar.SECOND);

         int tgl= cal.get(Calendar.DATE);
         int bln= cal.get(Calendar.MONTH);
         int thn= cal.get(Calendar.YEAR);

         String stgl=String.valueOf(tgl)+"-"+String.valueOf(bln)+"-"+String.valueOf(thn);
         String sjam=String.valueOf(jam)+":"+String.valueOf(menit)+":"+String.valueOf(detik);
        
         TextView  txtMarquee=(TextView)findViewById(R.id.txtMarquee);
         txtMarquee.setSelected(true);
         String kata="Selamat Datang @lp2maray.com Aplikasi Android  "+stgl+"/"+sjam+" #";
         String kalimat=String.format("%1$s",TextUtils.htmlEncode(kata));
         txtMarquee.setText(Html.fromHtml(kalimat+kalimat+kalimat)); 
  }
 
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
finish();
return true;
}
return super.onKeyDown(keyCode, event);
}   
 
}
+++++++++++++++++++





Tidak ada komentar:

Posting Komentar