Sii Ann Die Sii Ann Die - 4 months ago 20
Android Question

how to make code update data using volley?

can you help me to make a simple code for update data to server using volley...
please include the php file and java code.. this is my layout... and i have the database that was upload in 000webhost..

<?xml version="1.0" encoding="utf-8"?><LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:layout_marginTop="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp"
android:text="Pengaturan"
android:id="@+id/textView8"
android:layout_gravity="center_horizontal"
android:textStyle="bold"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="23sp"
android:layout_marginTop="15dp"
android:layout_marginLeft="15dp"
android:text="Profil"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="15dp"
android:src="@drawable/line"/>

<RelativeLayout
android:layout_width="345dp"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:layout_marginLeft="30dp"
android:layout_marginTop="10dp"
android:text="Nama :"
android:id="@+id/textView9" />
<TextView
android:text="E-Mail :"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:layout_marginLeft="30dp"
android:layout_marginTop="50dp"
android:id="@+id/textView10" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:layout_marginLeft="30dp"
android:layout_marginTop="90dp"
android:text="No Telpon :"
android:id="@+id/textView11" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:layout_marginLeft="30dp"
android:layout_marginTop="130dp"
android:text="Alamat :"
android:id="@+id/textView12" />

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/etnama"
android:layout_alignBottom="@+id/textView9"
android:layout_alignParentEnd="true" />

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/etemail"
android:layout_alignBottom="@+id/textView10"
android:layout_below="@+id/textView9"
android:layout_alignParentEnd="true" />

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/etnotelpon"
android:layout_alignBottom="@+id/textView11"
android:layout_below="@+id/etemail"
android:layout_alignParentEnd="true" />

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/etalamat"
android:layout_below="@+id/etnotelpon"
android:layout_alignParentEnd="true"
android:layout_alignBottom="@+id/textView12"/>

<Button
android:id="@+id/btEditProfil"
android:text="Edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_gravity="right"
android:layout_below="@+id/etalamat"/>

</RelativeLayout>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="23sp"
android:layout_marginTop="25dp"
android:layout_marginLeft="15dp"
android:text="Akun"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="15dp"
android:src="@drawable/line"/>
<RelativeLayout
android:layout_width="345dp"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/tvUsername"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:layout_marginLeft="30dp"
android:layout_marginTop="10dp"
android:text="Username :"/>
<TextView
android:id="@+id/tvPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:layout_marginLeft="30dp"
android:layout_marginTop="50dp"
android:text="Password :"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/etusername"
android:layout_alignBottom="@+id/tvUsername"
android:layout_alignParentEnd="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/etpassword"
android:layout_alignBottom="@+id/tvPassword"
android:layout_alignParentEnd="true" />
<Button
android:id="@+id/btEditAkun"
android:text="Edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_gravity="right"
android:layout_below="@+id/etpassword"/>
</RelativeLayout>
</LinearLayout>


if i click edit button the button save and cancel will appear.. please help me to make the code

Answer

This is sample php code for inserting values into database..

<?php
    $host='127.0.0.1';
    $uname='root';
    $pwd='password';
    $db="android";

    $con = mysql_connect($host,$uname,$pwd) or die("connection failed");
    mysql_select_db($db,$con) or die("db selection failed");

    $wmail=$_REQUEST['email'];
    $password=$_REQUEST['password'];

    $flag['code']=0;

    if($r=mysql_query("insert into sample values('$email','$password') ",$con))
    {
        $flag['code']=1;
        echo"hi";
    }

    print(json_encode($flag));
    mysql_close($con);
?>

and in Android

private void makeJsonObjReq() {

        Map<String, String> postParam= new HashMap<String, String>();
        postParam.put("email", "xyz@gmail.com");
        postParam.put("password", "somepasswordhere");

    JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.POST,
        You_Url, new JsonObject(postParam),
        new Response.Listener<JSONObject>() {

            @Override
            public void onResponse(JSONObject response) {
                Log.d(TAG, response.toString());


            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                VolleyLog.d(TAG, "Error: " + error.getMessage());

            }
        }) {

    /**
     * Passing some request headers
     * */
    @Override
    public Map<String, String> getHeaders() throws AuthFailureError {
        HashMap<String, String> headers = new HashMap<String, String>();
        headers.put("Content-Type", "application/json; charset=utf-8");
        return headers;
    }
};

// Adding request to request queue
AppController.getInstance().addToRequestQueue(jsonObjReq,tag_json_obj);

// Cancelling request
// ApplicationController.getInstance().getRequestQueue().cancelAll(tag_json_obj);       
}

And also this is just a sample code For you to learn.. I haven't tested the PHP code..Make proper changes to and also add the required params

Hope this helps

EDIT : Also please refer this

Comments