Search

Wednesday, 7 December 2016

JSON Update data

Update 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" >

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_centerVertical="true"
        android:text="Mobile"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView2"
        android:layout_marginTop="20dp"
        android:text="Address"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <EditText
        android:id="@+id/editText3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/textView2"
        android:layout_alignLeft="@+id/editText2"
        android:ems="10"
        android:inputType="phone" />

    <EditText
        android:id="@+id/editText4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/textView3"
        android:layout_alignLeft="@+id/editText3"
        android:ems="10"
        android:inputType="textPostalAddress" />

    <EditText
        android:id="@+id/editText2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/editText3"
        android:layout_alignLeft="@+id/editText1"
        android:layout_marginBottom="18dp"
        android:ems="10"
        android:inputType="textEmailAddress" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/editText2"
        android:layout_alignLeft="@+id/textView3"
        android:text="Email"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/editText1"
        android:layout_alignLeft="@+id/textView1"
        android:text="Name"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/textView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/editText2"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="29dp"
        android:text="data change based on name"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView3"
        android:layout_marginTop="49dp"
        android:layout_toRightOf="@+id/textView1"
        android:text="Update" />


</RelativeLayout>



JAVA FILE

package mysql.update;

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.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {

private Button search1;
private Button update;
EditText edname;
EditText edemail;
EditText edmobile;
EditText edaddr;
EditText searchname;
InputStream IS=null;
String name,result=null;
String line;
JSONArray jsonarray;
int code=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
search1=(Button)findViewById(R.id.button1);
update=(Button)findViewById(R.id.button2);
edname=(EditText)findViewById(R.id.editText1);
edemail=(EditText)findViewById(R.id.editText3);
edmobile=(EditText)findViewById(R.id.editText4);
edaddr=(EditText)findViewById(R.id.editText5);


update.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if((edname.equals("")) || (edmobile.equals(""))|| (edaddr.equals(""))||(edemail.equals("") ))
{
Toast.makeText(getApplicationContext(), "All Fields are compulsory", 50).show();
}else{

AlertDialog.Builder ab=new AlertDialog.Builder(MainActivity.this);
ab.setMessage("Do You Want to Update");
ab.setPositiveButton("Yes", new OnClickListener() {

@Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
new UpdateData().execute(edname.toString(),edemail.toString(),edmobile.toString(),edaddr.toString());
}
       
});
ab.setNegativeButton("No",new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(),"you select no", 50).show();
}
});
AlertDialog a1=ab.create();
a1.show();
}
}
});
}

private class UpdateData extends AsyncTask<String, String, String>{

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

ArrayList<NameValuePair> names=new ArrayList<NameValuePair>();
       
       names.add(new BasicNameValuePair("name",arg[0].toString()));
       names.add(new BasicNameValuePair("email",arg[1].toString()));
       names.add(new BasicNameValuePair("mobile",arg[2].toString()));
       names.add(new BasicNameValuePair("addr", arg[3].toString()));
     
      try {
        HttpClient httpclient = new DefaultHttpClient();
           HttpPost httppost = new HttpPost("http://10.0.2.2/MCA/updatedata.php");
          //  HttpPost httppost = new HttpPost("http://mca.freeoda.com/ANDROID/selectL.php");
           httppost.setEntity(new UrlEncodedFormEntity(names));
           HttpResponse response = httpclient.execute(httppost);
           HttpEntity entity = response.getEntity();
           IS = entity.getContent();
           Log.e("pass 1", "connection success "+IS.available());
       } catch (Exception e) {
           Log.e("Fail 1", 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 + "\n");
               Log.e("pass",""+line);
           }
           IS.close();
           result = sb.toString();


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

       } catch (Exception e) {
           Log.e("Fail 2", e.toString());
       }
      
       try {
       
        JSONObject j_data=new JSONObject(result);
        code=j_data.getInt("code");
        String sr;
        if(code==1){
        sr="Updated...";
        }else{
               sr="Not Updated...";
        }
           
        Log.e("pass 3",""+sr);
        } catch (Exception we) {
           Log.e("Fail 3", 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