Can anyone point me to a good implementation of a way to send GET and POST Requests. They are alot of ways to do these, and i am looking for the best implementation. Secondly is there a generic way to send both these methods rather then using two different ways. After all the GET method merely has the params in the Query Strings, whereas the POST method uses the headers for the Params.
Thanks.
Create RequestListner intreface
Get Unique Number
You can use the
HttpURLConnection
class (in java.net) to send a POST or GET HTTP request. It is the same as any other application that might want to send an HTTP request. The code to send an Http Request would look like this:A GET request will look a little bit different, but much of the code is the same. You don't have to worry about doing output with streams or specifying the content-length or content-type:
As you said: the GET-Parameters are in the URL - So you can use a loadUrl() on your Webview to send them.
The developer training docs have a good example on GET requests. You're responsible for adding the query parameters to the URL.
Post is similar, but as you said, quite different. The HttpConnectionURLConnection class can do both, and it's easy to just set the post body with an output stream.
I prefer using dedicated class to do GET/POST and any HTTP connections or requests. Moreover I use
HttpClient
to execute these GET/POST methods.Below is sample from my project. I needed thread-safe execution so there is
ThreadSafeClientConnManager
.There is an example of using GET (fetchData) and POST (sendOrder)
As you can see
execute
is general method for executingHttpUriRequest
- it can be POST or GET.EDIT
The
execute
method is public because sometimes I use custom (or dynamic) GET/POST requests.If you have
URL
object you can pass toexecute
method: