Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.dosen.myapplication;
- import android.content.SharedPreferences;
- import android.os.AsyncTask;
- import android.preference.PreferenceManager;
- import android.support.v7.app.ActionBarActivity;
- import android.os.Bundle;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import org.apache.http.NameValuePair;
- import org.apache.http.message.BasicNameValuePair;
- import org.json.JSONArray;
- import org.json.JSONObject;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- public class Profil extends ActionBarActivity {
- EditText etnama, etalamat;
- Button btnubah, btnsimpan;
- public JSONArray mProfil;
- public ArrayList<HashMap<String,String>> profilList = new ArrayList<HashMap<String, String>>();
- String URL = "http://localhost:81/getjson/profil.php";
- String URL1 = "http://localhost:81/getjson/ubahprofil.php";
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_profil);
- etnama = (EditText) findViewById(R.id.etNama);
- etalamat = (EditText) findViewById(R.id.etAlamat);
- btnubah = (Button) findViewById(R.id.btnUbah);
- btnsimpan = (Button) findViewById(R.id.btnSimpan);
- btnubah.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- btnsimpan.setVisibility(View.VISIBLE);
- btnubah.setVisibility(View.GONE);
- //etNama dan etAlamat di setEnabled"false" di XML nya
- etnama.setEnabled(true);
- etalamat.setEnabled(true);
- }
- });
- btnsimpan.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- btnubah.setVisibility(View.VISIBLE);
- btnsimpan.setVisibility(View.INVISIBLE);
- new UbahProfil().execute();
- finish();
- }
- });
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.menu_profil, menu);
- return true;
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- // Handle action bar item clicks here. The action bar will
- // automatically handle clicks on the Home/Up button, so long
- // as you specify a parent activity in AndroidManifest.xml.
- int id = item.getItemId();
- //noinspection SimplifiableIfStatement
- if (id == R.id.action_settings) {
- return true;
- }
- return super.onOptionsItemSelected(item);
- }
- public void updateJSONData(){
- JSONParser jsonParser = new JSONParser();
- int success;
- String username = "budi";
- //untuk yg sudah bikin sharedpreferences
- //SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(Profil.this);
- //String username = sp.getString("username", "value");
- try {
- List<NameValuePair> parameter = new ArrayList<NameValuePair>();
- parameter.add(new BasicNameValuePair("pengguna", username));
- JSONObject jsonObject = jsonParser.makeHttpRequest(URL,"POST",parameter);
- System.out.println("Mengambil data profil...");
- success = jsonObject.getInt("success");
- if(success == 1){
- try {
- mProfil = jsonObject.getJSONArray("profil");
- profilList.clear();
- for(int i=0;i<mProfil.length();i++){
- JSONObject c = mProfil.getJSONObject(i);
- String nama = c.getString("nama");
- String alamat = c.getString("alamat");
- HashMap<String,String> map = new HashMap<String,String>();
- map.put("nama", nama);
- map.put("alamat", alamat);
- profilList.add(map);
- System.out.println("Selesai membaca profil.");
- }
- }catch (Exception e){
- e.printStackTrace();
- }
- } else {
- System.out.println(jsonObject.getString("message"));
- }
- } catch (Exception e){
- }
- }
- public void updateProfil(){
- HashMap<String,String> profilMap = profilList.get(0);
- String nama = profilMap.get("nama");
- String alamat = profilMap.get("alamat");
- etnama.setText(nama);
- etalamat.setText(alamat);
- }
- class ProfilGet extends AsyncTask<Void, Void, String>{
- @Override
- protected String doInBackground(Void... params) {
- updateJSONData();
- return null;
- }
- @Override
- protected void onPostExecute(String s) {
- super.onPostExecute(s);
- updateProfil();
- }
- }
- public void updateJSONdata1() {
- // instansiasi jsonparser
- JSONParser jParser = new JSONParser();
- // mendapatkan json object dari URL
- int success;
- // ini kl pakee SharedPreferences
- SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(Profil.this);
- String username = sp.getString("username", "value");
- try {
- // Building Parameters
- String nama = etnama.getText().toString();
- String alamat = etalamat.getText().toString();
- String kota = etkota.getText().toString();
- String telepon = ettelepon.getText().toString();
- List<NameValuePair> params = new ArrayList<NameValuePair>();
- params.add(new BasicNameValuePair("pengguna", username));
- params.add(new BasicNameValuePair("nama", nama));
- params.add(new BasicNameValuePair("alamat", alamat));
- Log.d("request!", "mulai...");
- // posting nama pengguna untuk mendapatkan pesan
- JSONObject json = jParser.makeHttpRequest(URL1, "POST", params);
- // full json response
- Log.d("Mengubah profil", json.toString());
- // json success element
- sukses = json.getInt("success");
- if (success == 1) {
- Log.d("Sukses :", String.valueOf("success"));
- }else{
- Log.d("Pesan gagal!", json.getString("message"));
- // return json.getString("message");
- }
- } catch (Exception e) {
- // TODO: handle exception
- }
- }
- class UbahProfil extends AsyncTask<Void, Void, Boolean>{
- @Override
- protected void onPreExecute() {
- super.onPreExecute();
- pDialog = new ProgressDialog(Profil.this);
- pDialog.setMessage("Memuat profil...");
- pDialog.setIndeterminate(false);
- pDialog.setCancelable(true);
- pDialog.show();
- }
- @Override
- protected Boolean doInBackground(Void... arg0) {
- // TODO Auto-generated method stub
- updateJSONdata1();
- return null;
- }
- @Override
- protected void onPostExecute(Boolean result) {
- super.onPostExecute(result);
- pDialog.dismiss();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement