Its been such a long time ever since I have posted anything into the blog. Well not posting into it does not mean I have not learned anything new in the mean time. Well, I have learned so many different things in this month that I really have not been able to keep track of it. But, I will eventually post about all of them. Today, I am posting about the way we perform image uploading from android app to server.
I thought uploading image to server was really hard. I researched for whole day. Different blogs were giving different types of suggestions. I was literally going through paralysis analysis. Then,after a day I finally uploaded image to the server. So, I am feeling great and happy right now.
To perform uploading, you will have to first understand the following things:
HTTP is a protocol for exchanging messages between a client and a server. An HTTP client opens a connection and sends a request message to an HTTP server; the server then returns a response message, usually containing the resource that was requested. After delivering the response, the server closes the connection. Request has three parts : a method name – GET or POST method , resources and the version of HTTP being used. Response also has three parts: the HTTP version, a response status code that gives the result of the request, and status code.
GET method : Requests data from a specified resource.
Query strings (name/value pairs) is send in the URL of a GET request. It should only be used to retrieve data
POST method : Submits data to be processed to a specified resource
Query strings (name/value pairs) is sent in the HTTP message body of a POST request.
Requests made through these methods return an instance of HTTPResponse. This instance of HTTPResponse has methods for accessing the response headers ( getHeader()), response info (getStatusCode()), response data (getData(), getInputStream()) and any trailers that might have been sent.
Although the java.net packages provides basic functionality for accessing resources via HTTP, it doesn’t provide the full flexibility or functionality needed by many applications.
So we need following jar file which can be downloaded from here to get an efficient, up-to-date, and feature-rich package implementing the client side of the most recent HTTP standards and recommendations-
Hence, we have understood how Client-Server communication is handled. Now through the following code snippet, we can upload image to the server: