Advertisement
rachmadi

Profil

May 6th, 2015
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.27 KB | None | 0 0
  1. package com.example.dosen.myapplication;
  2.  
  3. import android.content.SharedPreferences;
  4. import android.os.AsyncTask;
  5. import android.preference.PreferenceManager;
  6. import android.support.v7.app.ActionBarActivity;
  7. import android.os.Bundle;
  8. import android.view.Menu;
  9. import android.view.MenuItem;
  10. import android.view.View;
  11. import android.widget.Button;
  12. import android.widget.EditText;
  13.  
  14. import org.apache.http.NameValuePair;
  15. import org.apache.http.message.BasicNameValuePair;
  16. import org.json.JSONArray;
  17. import org.json.JSONObject;
  18.  
  19. import java.util.ArrayList;
  20. import java.util.HashMap;
  21. import java.util.List;
  22.  
  23.  
  24. public class Profil extends ActionBarActivity {
  25.     EditText etnama, etalamat;
  26.     Button btnubah, btnsimpan;
  27.     public JSONArray mProfil;
  28.     public ArrayList<HashMap<String,String>> profilList = new ArrayList<HashMap<String, String>>();
  29.  
  30.     String URL = "http://localhost:81/getjson/profil.php";
  31.     String URL1 = "http://localhost:81/getjson/ubahprofil.php";
  32.  
  33.     @Override
  34.     protected void onCreate(Bundle savedInstanceState) {
  35.         super.onCreate(savedInstanceState);
  36.         setContentView(R.layout.activity_profil);
  37.  
  38.         etnama = (EditText) findViewById(R.id.etNama);
  39.         etalamat = (EditText) findViewById(R.id.etAlamat);
  40.  
  41.         btnubah = (Button) findViewById(R.id.btnUbah);
  42.         btnsimpan = (Button) findViewById(R.id.btnSimpan);
  43.  
  44.         btnubah.setOnClickListener(new View.OnClickListener() {
  45.             @Override
  46.             public void onClick(View v) {
  47.                 btnsimpan.setVisibility(View.VISIBLE);
  48.                 btnubah.setVisibility(View.GONE);
  49.         //etNama dan etAlamat di setEnabled"false" di XML nya
  50.         etnama.setEnabled(true);
  51.         etalamat.setEnabled(true);
  52.             }
  53.         });
  54.        
  55.         btnsimpan.setOnClickListener(new View.OnClickListener() {
  56.             @Override
  57.             public void onClick(View v) {
  58.                 btnubah.setVisibility(View.VISIBLE);
  59.                 btnsimpan.setVisibility(View.INVISIBLE);
  60.         new UbahProfil().execute();
  61.         finish();
  62.             }
  63.         });
  64.  
  65.     }
  66.  
  67.     @Override
  68.     public boolean onCreateOptionsMenu(Menu menu) {
  69.         // Inflate the menu; this adds items to the action bar if it is present.
  70.         getMenuInflater().inflate(R.menu.menu_profil, menu);
  71.         return true;
  72.     }
  73.  
  74.     @Override
  75.     public boolean onOptionsItemSelected(MenuItem item) {
  76.         // Handle action bar item clicks here. The action bar will
  77.         // automatically handle clicks on the Home/Up button, so long
  78.         // as you specify a parent activity in AndroidManifest.xml.
  79.         int id = item.getItemId();
  80.  
  81.         //noinspection SimplifiableIfStatement
  82.         if (id == R.id.action_settings) {
  83.             return true;
  84.         }
  85.  
  86.         return super.onOptionsItemSelected(item);
  87.     }
  88.  
  89.     public void updateJSONData(){
  90.         JSONParser jsonParser = new JSONParser();
  91.         int success;
  92.         String username = "budi";
  93.  
  94.         //untuk yg sudah bikin sharedpreferences
  95.         //SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(Profil.this);
  96.         //String username = sp.getString("username", "value");
  97.  
  98.         try {
  99.             List<NameValuePair> parameter = new ArrayList<NameValuePair>();
  100.             parameter.add(new BasicNameValuePair("pengguna", username));
  101.  
  102.             JSONObject jsonObject = jsonParser.makeHttpRequest(URL,"POST",parameter);
  103.             System.out.println("Mengambil data profil...");
  104.  
  105.             success = jsonObject.getInt("success");
  106.             if(success == 1){
  107.                 try {
  108.                     mProfil = jsonObject.getJSONArray("profil");
  109.                     profilList.clear();
  110.                     for(int i=0;i<mProfil.length();i++){
  111.                         JSONObject c = mProfil.getJSONObject(i);
  112.                         String nama = c.getString("nama");
  113.                         String alamat = c.getString("alamat");
  114.  
  115.                         HashMap<String,String> map = new HashMap<String,String>();
  116.                         map.put("nama", nama);
  117.                         map.put("alamat", alamat);
  118.  
  119.                         profilList.add(map);
  120.                         System.out.println("Selesai membaca profil.");
  121.                     }
  122.                 }catch (Exception e){
  123.                     e.printStackTrace();
  124.                 }
  125.             } else {
  126.                 System.out.println(jsonObject.getString("message"));
  127.             }
  128.  
  129.         } catch (Exception e){
  130.  
  131.         }
  132.     }
  133.  
  134.     public void updateProfil(){
  135.         HashMap<String,String> profilMap = profilList.get(0);
  136.         String nama = profilMap.get("nama");
  137.         String alamat = profilMap.get("alamat");
  138.  
  139.         etnama.setText(nama);
  140.         etalamat.setText(alamat);
  141.     }
  142.  
  143.     class ProfilGet extends AsyncTask<Void, Void, String>{
  144.  
  145.         @Override
  146.         protected String doInBackground(Void... params) {
  147.             updateJSONData();
  148.             return null;
  149.         }
  150.  
  151.         @Override
  152.         protected void onPostExecute(String s) {
  153.             super.onPostExecute(s);
  154.             updateProfil();
  155.         }
  156.     }
  157.  
  158.     public void updateJSONdata1() {
  159.         // instansiasi jsonparser
  160.         JSONParser jParser = new JSONParser();
  161.         // mendapatkan json object dari URL
  162.         int success;
  163.         // ini kl pakee SharedPreferences
  164.         SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(Profil.this);
  165.         String username = sp.getString("username", "value");
  166.        
  167.         try {
  168.             // Building Parameters
  169.            
  170.             String nama = etnama.getText().toString();
  171.             String alamat = etalamat.getText().toString();
  172.             String kota = etkota.getText().toString();
  173.             String telepon = ettelepon.getText().toString();
  174.            
  175.             List<NameValuePair> params = new ArrayList<NameValuePair>();
  176.             params.add(new BasicNameValuePair("pengguna", username));
  177.             params.add(new BasicNameValuePair("nama", nama));
  178.             params.add(new BasicNameValuePair("alamat", alamat));          
  179.  
  180.             Log.d("request!", "mulai...");
  181.            
  182.           // posting nama pengguna untuk mendapatkan pesan
  183.             JSONObject json = jParser.makeHttpRequest(URL1, "POST", params);
  184.  
  185.             // full json response
  186.             Log.d("Mengubah profil", json.toString());
  187.  
  188.             // json success element
  189.             sukses = json.getInt("success");
  190.             if (success == 1) {
  191.                 Log.d("Sukses :", String.valueOf("success"));
  192.             }else{
  193.                 Log.d("Pesan gagal!", json.getString("message"));
  194. //              return json.getString("message");          
  195.             }
  196.            
  197.         } catch (Exception e) {
  198.             // TODO: handle exception
  199.         }
  200.     }
  201.    
  202.     class UbahProfil extends AsyncTask<Void, Void, Boolean>{
  203.  
  204.         @Override
  205.         protected void onPreExecute() {
  206.             super.onPreExecute();
  207.             pDialog = new ProgressDialog(Profil.this);
  208.             pDialog.setMessage("Memuat profil...");
  209.             pDialog.setIndeterminate(false);
  210.             pDialog.setCancelable(true);
  211.             pDialog.show();
  212.         }
  213.        
  214.         @Override
  215.         protected Boolean doInBackground(Void... arg0) {
  216.             // TODO Auto-generated method stub
  217.             updateJSONdata1();
  218.             return null;
  219.         }
  220.        
  221.         @Override
  222.         protected void onPostExecute(Boolean result) {
  223.             super.onPostExecute(result);
  224.             pDialog.dismiss();
  225.         }
  226.        
  227.     }
  228.  
  229. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement