简体   繁体   中英

How to send Json POST request using Volley in android in the Given Tutorial?

Hi I am a beginner in Android , I am learning to make Api Calls. I got a tutorial of Volley which uses GET to Receive response. Now I want to Send Post request Using Volley. I don't know how to do that, what would be the code for POST in the given Tutorial. Please guide me to send Post Request.The link to the tutorial that I am learning is http://www.truiton.com/2015/02/android-volley-example/

you have to used below code to send request

// Instantiate the RequestQueue.
RequestQueue queue = Volley.newRequestQueue(this);
String url ="http://www.google.com";

// Request a string response from the provided URL.
StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
            new Response.Listener<String>() {
    @Override
    public void onResponse(String response) {
        // Display the first 500 characters of the response string.
        mTextView.setText("Response is: "+ response.substring(0,500));
    }
}, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
        mTextView.setText("That didn't work!");
    }
});
// Add the request to the RequestQueue.
queue.add(stringRequest);
    try {
        /** json object parameter**/
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("hello", "hello");
        Log.e("jsonObject params", jsonObject.toString() + "");
        /**URL */
        String url ="http://google.com"

        progress.setVisibility(View.VISIBLE);
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, jsonObject, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                progress.setVisibility(View.GONE);
                Log.e(TAG, "Response " + jsonObject.toString());
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                progress.setVisibility(View.GONE);
                Log.e(TAG, volleyError);
                Util.showToast(activity, "Please try again");
            }
        });
        requestQueue.add(jsonObjectRequest);

    } catch (JSONException e) {
        progress.setVisibility(View.GONE);
        Log.e(TAG, e);
    }
    catch (Exception e) {
            progress.setVisibility(View.GONE);
            Log.e(TAG, e);
        }
    }
}

RequestQueue queue = Volley.newRequestQueue(this);
queue.add(jsonObjectRequest);

You can follow this : http://www.androidhive.info/2014/05/android-working-with-volley-library-1/

StringRequest request = new StringRequest(Method.POST,
        "post url",

        new ResponseListener() {

            @Override
            public void onResponse(String response) {
                Log.d("response", response);

                } catch (Exception e) {
                    e.printStackTrace();
                }

            }

        }, new ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                     Log.e("response","error");
            }

        }) {

   // post params
    @Override
    protected Map<String, String> getParams() throws AuthFailureError {
        Map<String, String> params = new HashMap<String, String>();
        params.put("param", "param");
        return params;
    }

};
//2.Use your custom volley manager  send request or like this

Volley.newRequestQueue(mCtx).add(request);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM