Search

Wednesday, 7 December 2016

JSON Delete Data

Delete Data From MySQL DataBase

XML FILE

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="66dp"
  android:hint="Enter name "
        android:ems="10" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editText1"
        android:layout_alignRight="@+id/editText1"
        android:layout_below="@+id/editText1"
        android:layout_marginTop="46dp"
        android:text="Delete" />

</RelativeLayout>

JAVA FILE

package com.example.delete;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {

private EditText edname;
private Button delete;
InputStream IS;String line,result;
int code;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edname=(EditText)findViewById(R.id.editText1);
delete=(Button)findViewById(R.id.button1);
delete.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(edname.getText().toString().equals("")){
Toast.makeText(getApplicationContext(), "All Field Compulsory", 50).show();
}else{
new DeleteData().execute(edname.getText().toString());
}
}
});
}
class DeleteData extends AsyncTask<String, String, String>{

@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub

          ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
               nameValuePairs.add(new BasicNameValuePair("name", params[0]));
               try {
                   HttpClient httpclient = new DefaultHttpClient();
                  HttpPost httppost = new //HttpPost("http://10.0.2.2/MCA/delete.php");
              HttpPost httppost = new HttpPost("http://mca.freeoda.com/MCA/delete.php");
                   httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                   HttpResponse response = httpclient.execute(httppost);
                   HttpEntity entity = response.getEntity();
                   IS = entity.getContent();
                   Log.e("pass 1", "connection success ");
               } catch (Exception e) {
                   Log.e("Fail 1", "Error Delete"+e.toString());
                   Toast.makeText(getApplicationContext(), "Invalid IP Address",
                           Toast.LENGTH_LONG).show();
               }
               try {
                   BufferedReader reader = new BufferedReader
                           (new InputStreamReader(IS, "iso-8859-1"), 8);
                   StringBuilder sb = new StringBuilder();
                   while ((line = reader.readLine()) != null) {
                       sb.append(line);
                   }
                   IS.close();
                   result = sb.toString();


                   Log.e("pass 2", "connection success ");

               } catch (Exception e) {
                   Log.e("Fail 2","Error at Delete"+e.toString());
               }
               try {
                   JSONObject j_data=new JSONObject(result);
                   code=j_data.getInt("code");
                   if(code==1){
                       Log.e("Pass 3", "Done");
                       runOnUiThread(new Runnable() {
                           @Override
                           public void run() {
                               Toast.makeText(getApplicationContext(), "Record Deleted", Toast.LENGTH_LONG).show();
                           }
                       });
                   }else{
                       runOnUiThread(new Runnable() {
                           @Override
                           public void run() {
                               Toast.makeText(getApplicationContext(), "Record not Deleted", Toast.LENGTH_LONG).show();
                           }
                       });


                   }

                   Log.e("pass 3", "pass 3");
               } catch (Exception we) {
                   Log.e("Fail 3", "Error At Delete"+we.toString());
               }
return null;
}

} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}


}


No comments:

Post a Comment