Android developmentAndroid tutorial

Android Volley Tutorial – Making HTTP Request GET, POST, PUT

Android Volley
java security cert certpathvalidatorexception

Hi everyone in this article I am sharing Android Volley Tutorial-Making HTTP Request GET, POST, PUT. In this android tutorial, we learn to share and explore get a request for REST API and getting a response from API data. For requesting a Rest API I am using the GET method, Post Method, and Put method.

In the Volley Rest API Requests POST data in Objects, with Aoth, and also send Data body Perm. in some, we use headers’ requests with Authentication. Android Volley Tutorial – Making HTTP Request GET, POST, PUT.

So Let’s Start on Volley Rest API Request Android example.

1. Android Volley Rest API with  GET Request.

// Get Request For JSONObject
public void EventDetails(){
    final ProgressDialog loading = new ProgressDialog(EventDetails_Screen.this);
    loading.setMessage("Please Wait...");
    loading.setCanceledOnTouchOutside(false);
    loading.show();

    JsonObjectRequest req = new JsonObjectRequest(Request.Method.GET, ConfiURL.BaseURL+"public/event/"+EventId, null, new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            Log.d("Response", response.toString());

            try {
                Log.d("JSON", String.valueOf(response));
                loading.dismiss();
                String Error = response.getString("httpStatus");
                if(Error.equals("OK")){
                    JSONObject Body = response.getJSONObject("body");
                    JSONObject asset = Body.getJSONObject("asset");
                    JSONObject variants =asset.getJSONObject("variants");
                    JSONArray speakers =Body.getJSONArray("speakers");
                    JSONArray tags =Body.getJSONArray("tags");
                    JSONArray eventSchedule =Body.getJSONArray("eventSchedule");

                    if (eventImage.isEmpty()) {
                        Toast.makeText(EventDetails_Screen.this, "no image for user", Toast.LENGTH_LONG).show();
                    } else {
                        Picasso.with(EventDetails_Screen.this)
                                .load(eventImage)
                                .placeholder(R.drawable.baseline_style_24)
                                .fit()
                                .into(Event_Image);
                    }
            
                }else {

                }
            } catch (JSONException e) {
                e.printStackTrace();
                loading.dismiss();

            }

        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            loading.dismiss();
            VolleyLog.d("Error", "Error: " + error.getMessage());
            Toast.makeText(EventDetails_Screen.this, "" + error.getMessage(), Toast.LENGTH_SHORT).show();

        }
    })
    {
        @Override
        public String getBodyContentType() {
            return "application/json; charset=utf-8";
        }

        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            HashMap<String, String> headers = new HashMap<String, String>();
            headers.put("Authorization", Token);
            return headers;
        }
    };
    RequestQueue queue = Volley.newRequestQueue(getApplicationContext());
    queue.add(req);
}

2. Android Volley Rest API Post Requests

 // Post Request For JSONObject
    public void postData() {
        final ProgressDialog loading = new ProgressDialog(Login_screen.this);
        loading.setMessage("Please Wait...");
        loading.setCanceledOnTouchOutside(false);
        loading.show();

        JSONObject object = new JSONObject();
        try {
            //input your API parameters
            object.put("usernameOrEmail",Email.getText());
            object.put("password",password.getText());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        // Enter the correct url for your api service site
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, ConfiURL.LOGIN_URL, object,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
//                        Toast.makeText(Login_screen.this,"String Response : "+ response.toString(),Toast.LENGTH_LONG).show();
                        try {
                            Log.d("JSON", String.valueOf(response));
                            loading.dismiss();
                            String Error = response.getString("httpStatus");
                            if (Error.equals("")||Error.equals(null)){

                            }else if(Error.equals("OK")){
                                JSONObject body = response.getJSONObject("body");
                                
                            }else {

                            }

                        } catch (JSONException e) {
                            e.printStackTrace();
                            loading.dismiss();
                        }
//                        resultTextView.setText("String Response : "+ response.toString());
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                loading.dismiss();
                VolleyLog.d("Error", "Error: " + error.getMessage());
                Toast.makeText(Login_screen.this, "" + error.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });
        RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
        requestQueue.add(jsonObjectRequest);
    }

 

 

Read More Tutorial