0

I'm confused about post and get request in android volley.

Can you explain me their differences?

And can I use post method with no Param to get a JSON from URL?

Barry Michael Doyle
  • 9,333
  • 30
  • 83
  • 143
S.Hossein Emadi
  • 79
  • 2
  • 10
  • Welcome to StackOverflow. Please be so kind and try to find some information yourself before asking a question here. E.g. use the first answer for [this Google result](https://www.google.de/#q=what+is+the+difference+between+post+and+get+request%3F&*). Beside that please ask only one question per post. – gus27 Feb 26 '17 at 15:58
  • 1
    Possible duplicate of [When should I use GET or POST method? What's the difference between them?](http://stackoverflow.com/questions/504947/when-should-i-use-get-or-post-method-whats-the-difference-between-them) – gus27 Feb 26 '17 at 16:02

2 Answers2

0

Their difference is in functions defined in server.

In simple words, With a GET method, you are sending your data via the URL. While, with A POST method, data is embedded in the form object and sent directly from your browser to the server. ... We usually use GET to identify and dynamically render pages and POST to send form data but it's not always the case.

and answer of your second question is yes you can but that's not a good idea get would be better for that. here is a example of how you can send requests using Volley Library

StringRequest request = new StringRequest(Request.Method.POST, "www.example.com", new Response.Listener<String>() {
    @Override
    public void onResponse(String response) {

    }
}, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {

    }
}) {
    @Override
    protected Map<String, String> getParams() {
        Map<String, String> parameters = new HashMap<>();
        return parameters;
    }

    @Override
    public String getBodyContentType() {
        return "application/x-www-form-urlencoded; charset=UTF-8";
    }
};
AppController.getInstance().addToRequestQueue(request, tag);
Amir_P
  • 8,322
  • 5
  • 43
  • 92
  • thanks for your answer amir. but in this scenario we send request with our android app,not with browser.is our request for example user password display in any place and unsafe with get method? – S.Hossein Emadi Feb 26 '17 at 14:08
  • basically they're same in the field of security but for login, register and operations like these it's better to do with `POST` method @S.HosseinEmadi – Amir_P Feb 26 '17 at 14:19
0

Are you working on server or server is handled by someone else? In general, person who works on server decides the method.

Like if you work on JAVA server, then say an API end point is https://sample.api.someurl.com/userInfo/

TO maintain some consistency server programmer may use GET method to get userInfo and he may use POST method to update user info and he may use DELETE method to delete the existing user info.

In this example, your API end point remains same but the request method decides how that end point will behave.

In other example, to save time, a server developer may redirect all the requests to one method and handle it there, so no matter you call GET, POST or DELETE API will return same response.

So yes, Its not Android or UI developer who decides the Method alone, Major role of deciding which method to use is decided by server programmer.

P.S. If you are working on server too, then good practice is to use GET to get the info, POST method to update or add the info and DELETE to remove the info.

Mohammed Atif
  • 4,383
  • 7
  • 28
  • 57