Kamis, 26 November 2015

Menu Delete

Class Java
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.params.HttpClientParams;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class DeleteActivity extends Activity implements OnItemClickListener{
    private final String urlDisplay = "http://10.0.2.2/contoh/getUser.php";     //koneksi database ke localhost
    private final String urlDelete = "http://10.0.2.2/contoh/deleteUser.php";     //koneksi database ke localhost
    private UserAdapter adapter;
    private ListView listUser;
   
   
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_display);               
        adapter = new UserAdapter(this, R.id.textView1);
        loadDataUser();       
        listUser = (ListView) findViewById(R.id.listViewUser);       
        listUser.setAdapter(adapter);
        listUser.setOnItemClickListener(this);
    }
   
   
   
    private void loadDataUser(){
        try {    String jsonString = DeleteActivity.getRequestFromServer(urlDisplay);           
                JSONObject jObject = new JSONObject(jsonString);
                JSONArray newsJsonArray = jObject.getJSONArray("user");           
                for (int i = 0; i < newsJsonArray.length(); i++) {
                    User user = new User(newsJsonArray.getJSONObject(i));
                    adapter.add(user);
                }
                adapter.notifyDataSetChanged();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
   
    class UserAdapter extends ArrayAdapter<User>{       
        public UserAdapter(Context context, int resource) {
            super(context, resource);           
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            if(convertView==null){
                convertView = ((LayoutInflater)getSystemService(Activity.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.activity_adapter, parent,false);
            }                       
            TextView usernameUser = (TextView) convertView.findViewById(R.id.textViewUsername);           
            TextView passwordUser = (TextView) convertView.findViewById(R.id.textViewPassword);
            User entityUser = getItem(position);
            usernameUser.setText(entityUser.username);
            passwordUser.setText(entityUser.password);                       
            return convertView;
        }
    }
   
       
    public void onItemClick(AdapterView<?> adapterV, View view, int position, long id) {       
        try {    String url = DeleteActivity.getRequestFromServer(urlDelete+"?username="+adapter.getItem(position).username);               
                Toast.makeText(this, url, Toast.LENGTH_LONG).show();       
        } catch (Exception e) {
            Toast.makeText(this, "Error: "+ e.getMessage(), Toast.LENGTH_LONG).show();
        }
        startActivity(new Intent(this, DeleteActivity.class));
        finish();
    }
   
    public static String getRequestFromServer(String url){   
        try {
            Class<?> strictModeClass = Class.forName("android.os.StrictMode", true, Thread.currentThread()
                    .getContextClassLoader());

            Class<?> threadPolicyClass = Class.forName("android.os.StrictMode$ThreadPolicy", true, Thread
                    .currentThread().getContextClassLoader());

            Class<?> threadPolicyBuilderClass = Class.forName("android.os.StrictMode$ThreadPolicy$Builder", true,
                    Thread.currentThread().getContextClassLoader());

            Method setThreadPolicyMethod = strictModeClass.getMethod("setThreadPolicy", threadPolicyClass);

            Method detectAllMethod = threadPolicyBuilderClass.getMethod("detectAll");
            Method penaltyMethod = threadPolicyBuilderClass.getMethod("penaltyLog");
            Method buildMethod = threadPolicyBuilderClass.getMethod("build");

            Constructor<?> threadPolicyBuilderConstructor = threadPolicyBuilderClass.getConstructor();
            Object threadPolicyBuilderObject = threadPolicyBuilderConstructor.newInstance();

            Object obj = detectAllMethod.invoke(threadPolicyBuilderObject);

            obj = penaltyMethod.invoke(obj);
            Object threadPolicyObject = buildMethod.invoke(obj);
            setThreadPolicyMethod.invoke(strictModeClass, threadPolicyObject);

        } catch (Exception ex) {
            Log.w("Thread Policy", ex);
        }

        String result = "Succeed";
        HttpClient client = new DefaultHttpClient();
        HttpClientParams.setRedirecting(client.getParams(), true);
        HttpGet request = new HttpGet(url);
        try{
            HttpResponse response = client.execute(request);
            InputStream in = response.getEntity().getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(in));
            StringBuilder str = new StringBuilder();
            String line = null;
            int lineCount = 0;
            while((line = reader.readLine()) != null){
                if(lineCount>0) str.append("\n");
                str.append(line);
            }
            in.close();
            result = str.toString();
            Log.w("get Request", "asli : "+result);
            if(result.indexOf("<") > 0){
                result = result.substring(0, result.indexOf("<"));
            }
            Log.w("get Request", "proses : "+Html.fromHtml(result));
            return Html.fromHtml(result).toString();           
        }catch(Exception ex){
            ex.printStackTrace();
            result = "Error";
        }
        return result;
    }
}


Class PHP
<?php

$username = $_GET['username'];


include "connect_db.php";

$query = "delete from tbuser where username ='".$username."'";
$result = mysql_query($query, $link) or die('Error query:  '.$query);

echo $result;

?>

Tidak ada komentar:

Posting Komentar