How to use PUT Method Request parameters in Android Volley

In this android programming source code example, we are going to use PUT Method request parameters in Android Volley.

You can copy and adopt this source code example to your android project without reinventing the wheel.

Below is a step by step source code to use PUT Method request parameters in Android Volley.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="15dp">

    <android.support.design.widget.TextInputLayout
        android:id="@+id/titleinput"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <EditText
            android:id="@+id/title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/title"
            android:inputType="textNoSuggestions"
            android:maxLines="1"
            android:singleLine="true" />
    </android.support.design.widget.TextInputLayout>

    <android.support.design.widget.TextInputLayout
        android:id="@+id/bodyinput"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/titleinput">
        <EditText
            android:id="@+id/body"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/body"
            android:inputType="textNoSuggestions"
            android:maxLines="1"
            android:singleLine="true" />
    </android.support.design.widget.TextInputLayout>

    <android.support.design.widget.TextInputLayout
        android:id="@+id/useridinput"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/bodyinput">
        <EditText
            android:id="@+id/userid"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/userid"
            android:inputType="number"
            android:maxLines="1"
            android:singleLine="true" />
    </android.support.design.widget.TextInputLayout>

    <android.support.design.widget.TextInputLayout
        android:id="@+id/postidinput"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/useridinput">
        <EditText
            android:id="@+id/postid"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/postid"
            android:inputType="number"
            android:maxLines="1"
            android:singleLine="true" />
    </android.support.design.widget.TextInputLayout>

    <Button
        android:id="@+id/update"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorAccent"
        android:layout_below="@id/postidinput"
        android:textColor="#ffffff"
        android:text="update"/>



</RelativeLayout>

MainActivity.java

import android.app.ProgressDialog;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import java.util.HashMap;
import java.util.Map;


public class VolleyActivity8 extends AppCompatActivity {
    private EditText title;
    private EditText body;
    private EditText userid;
    private EditText postid;
    private Button update;
    private ProgressDialog progressDialog;
    private String baseUrl;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_volley7);
        title = (EditText) findViewById(R.id.title);
        body = (EditText) findViewById(R.id.body);
        userid = (EditText) findViewById(R.id.userid);
        postid = (EditText) findViewById(R.id.postid);
        update = (Button) findViewById(R.id.update);
        baseUrl = "http://jsonplaceholder.typicode.com/posts/";

        update.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
                } catch (Exception e) {
                    // TODO: handle exception
                }
                submitData();
            }
        });
    }

    private void submitData(){
        final String enteredtitle = title.getText().toString();
        final String enteredbody = body.getText().toString();
        final String entereduserid = userid.getText().toString();
        final String enteredpostid = postid.getText().toString();

        progressDialog = new ProgressDialog(VolleyActivity8.this);
        progressDialog.setMessage(getString(R.string.loading));
        progressDialog.setCancelable(false);
        progressDialog.show();
        //Defining api service
        RequestQueue requestQueue = Volley.newRequestQueue(VolleyActivity8.this);
        StringRequest stringRequest = new StringRequest(Request.Method.PUT, baseUrl+enteredpostid, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                progressDialog.dismiss();
                Toast.makeText(getApplicationContext(), response, Toast.LENGTH_LONG).show();
            }
        }, new Response.ErrorListener(){
            @Override
            public void onErrorResponse(VolleyError volleyerror){
                progressDialog.dismiss();
                Toast.makeText(getApplicationContext(), volleyerror.getMessage(), Toast.LENGTH_LONG).show();
            }
        }){
            @Override
            protected Map<String, String> getParams(){
                Map<String, String> params = new HashMap<String, String>();
                params.put("title", enteredtitle);
                params.put("body", enteredbody);
                params.put("userId", entereduserid);
                return params;
            }
        };
        requestQueue.add(stringRequest);
    }
}

build.gradle

implementation 'com.android.volley:volley:1.1.1'

If you have any question or suggestions kindly use the comment box or you can contact us directly through our contact page below.

 

Add a Comment