Introduction
This article explains HTTP in Android.
What is HTTP
HTTP stands for HyperText Transfer Protocol. It is used for communication between the client and server. HTTP works as a request/response protocol between a client and a server. In this protocol a client requests information from the server, then the server returns a response to the client. The response contains the information about the status and sometimes maybe the content.
HTTP provides two request methods, Get and Post.
Get: Requests data from the server.
Post: Submits data to the server.
Android has two HTTP clients, one is HttpUrlConnection and the other is HttpClient. A HTTP client is used when we want to send data to and receive data from the internet. The Get and Post methods are the basic methods to exchange information with the server. Suppose I want to send some text data to the server. So the first step is create a default HTTP client to send the data.
HttpClient httpClient=new DefaultHttpClient();
After creating the HttpCLient we will instantiate a class to handle the post request and an URL as a parameter.
HttpPost httpPost=new HttpPost(url);
url is the URL that you want to invoke to send data. Now make your list that contains your data that you want to send. Then set it inside our post request to be sent so that it can be sent.
httpPost.setEntity(new UrlEncodedFormEntity(your list object);
URL encoding is used to encode the parameter that you want to send. At last we execute our request through the instance of the HTTP client and receive the response.
HttpResponse httpResponse=httpClient.execute(url);
For reading the response we can obtain an inputstream and consume it reading data in this way.
InputStream inputStream=httpResponse.getEntity().getContent();
getEntity(): gets the message entity of this response.
getContent(): creates the inputStream of the entity.
What a Stream is
A Stream is just like a stream of water that consists of a sequence of data (bytes).
InputStream
An Input Steam in Java reads data from a source; it may be a file, array and so on. Input stream is a class in Java.
This is how to write to Get and Post data:
- HttpClient httpClient=new DefaultHttpClient();
- HttpPost httpPost=new HttpPost(url);
- httpPost.setEntity(new UrlEncodedFormEntity(your list object);
- HttpResponse httpResponse=httpClient.execute(url);
- InputStream inputStream=httpResponse.getEntity().getContent();
- BufferedReader reader=new BufferedReader(new InputStreamReader(inputStream));
-
- StringBuilder stringBuilder=new StringBuilder();
- String line=null;
- while(line=reader.readLine())
- {
- stringBuilder.append(line+"\n");
-
- }
- inputStream.close();
Step 1
Create an XML file with the following:
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent" >
-
- <TextView
- android:id="@+id/txtView"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentTop="true"
- android:layout_centerHorizontal="true"
- android:layout_marginTop="20dp"
- android:text="Enter Something Below:"
- android:textAppearance="?android:attr/textAppearanceLarge" />
-
- <EditText
- android:id="@+id/edtText1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_below="@+id/textView1"
- android:layout_centerHorizontal="true"
- android:layout_marginTop="30dp"
- android:ems="10"
- android:hint=""
- >
-
- <requestFocus />
- </EditText>
-
- <ProgressBar
- android:id="@+id/progressBar1"
- style="?android:attr/progressBarStyleLarge"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_below="@+id/editText1"
- android:layout_centerHorizontal="true"
- android:layout_marginTop="24dp" />
-
- <Button
- android:id="@+id/btn_send"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignLeft="@+id/textView1"
- android:layout_alignRight="@+id/editText1"
- android:layout_below="@+id/progressBar1"
- android:layout_marginTop="24dp"
- android:text="Submit" />
-
- </RelativeLayout>
Step 2
Create a Java class file with the following:
Step 3
Give internet permission to the Android Manifest.xml file as in the following:
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.example.locationapp"
- android:versionCode="1"
- android:versionName="1.0" >
-
- <uses-sdk
- android:minSdkVersion="8"
- android:targetSdkVersion="18" />
- <uses-permission android:name="android.permission.INTERNET" />
- <application
- android:allowBackup="true"
- android:icon="@drawable/ic_launcher"
- android:label="@string/app_name"
- android:theme="@style/AppTheme" >
- <activity
- android:name="com.example.locationapp.MainActivity"
- android:label="@string/app_name" >
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- </application>
- </manifest>
Step 4
Enter the string you want to send:
Step 5
Click on the button.