Download Code

In this tutorial you will learn how to use HTTP with different interfaces like httpClient,httpGet,httpResponse etc.

Let me give some basic information about every class.

HTTP

HTTP(Hyper Text Tranfer Protocol) is a set of rules for tranfering files on WWW.More detail.

HTTPClient

Interface for an HTTP client. HTTPclients encapsulate a variety of objects required to execute HTTP requests while handling cookies, authentication, connection management, and other feature.

URI

Represents a Uniform Resource Identifier (URI) reference.

HTTPGet

The GET method means retrieve whatever information is identified by the Request-URI.

HTTPResponse

HTTPResponse used to get response from any source.

Buffered Reader

Reads text from a character-input stream, buffering characters so as to provide for the efficient reading of characters, arrays, and lines.

Input Stream Reader

An InputStreamReader is a bridge from byte streams to character streams: It reads bytes and decodes them into characters.

String Buffer

A thread-safe, mutable sequence of characters. A String buffer is like a String, but can be modified.

Let’s start create new android application project.Go to activity_main.xml and add a TextView.

http,android http,httpclient,httpclient library,httppost, httpget,stringbuffer,string buffer in android, how to use httpget in android, how to use httpclient in android, inputstreamreader,inputstreamreader in android, httpclient android,buffered reader,buffered reader in android, httpresponse,httpresponse in android,how to use httpresponse in android

Now go to MainActivity.java class.Do the following steps.

  • Refer textview id.
  • Create asyncTask with background and PostExecute method.
  • Within asynctask create httpclient.
  • Make get request with httpget and httpresponse.
  • Receive response as an input stream.
  • Convert input stream into string
  • Set string to textview in onPostExecute() method.

MainActivity.java

package developer.httpcodec;


import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.support.v7.app.ActionBarActivity;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;


public class MainActivity extends Activity {

	TextView txtDisplay;    // textview variable to display data
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		txtDisplay = (TextView) findViewById(R.id.display);    //refering id of textview
		new HTTPComponents().execute("http://www.androidcodec.com");    // executing asynctask with url
	}

	public class HTTPComponents extends AsyncTask<String, Void, String>{

		@Override
		protected String doInBackground(String... params) {
			// TODO Auto-generated method stub
			InputStream inputStream = null;     
			String result = "";
			try {

				//httpclient variable to set default http client
				HttpClient httpclient = new DefaultHttpClient();

				// httpget request to the url 
				HttpResponse httpResponse = httpclient.execute(new HttpGet(params[0]));

				// receive response as inputStream as an entity
				inputStream = httpResponse.getEntity().getContent();

				// convert inputstream to string
				if(inputStream != null){
					BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(inputStream));
					String line = "";
					while((line = bufferedReader.readLine()) != null)
						result += line;

					inputStream.close();
				}
				else
					result = "Unable to Load data!";

			} catch (Exception e) {
				Log.d("InputStream", e.getLocalizedMessage());
			}

			return result;
		}
		@Override
		protected void onPostExecute(String result) {
			// TODO Auto-generated method stub
			
			txtDisplay.setText(result);
		}
	}
}

Next go to Android Manifest.xml file.Add two permissions.

  • INTERNET
  • ACCESS NETWORK STATE

http,android http,httpclient,httpclient library,httppost, httpget,stringbuffer,string buffer in android, how to use httpget in android, how to use httpclient in android, inputstreamreader,inputstreamreader in android, httpclient android,buffered reader,buffered reader in android, httpresponse,httpresponse in android,how to use httpresponse in android, inputstream,inputstream in android,asyncTask in android,asyncTask,onBackground,onPostExecute,

Run your android application project.

http,android http,httpclient,httpclient library,httppost, httpget,stringbuffer,string buffer in android, how to use httpget in android, how to use httpclient in android, inputstreamreader,inputstreamreader in android, httpclient android,buffered reader,buffered reader in android, httpresponse,httpresponse in android,how to use httpresponse in android, inputstream,inputstream in android,asyncTask in android,asyncTask,onBackground,onPostExecute,

That’s it for this tutorial.

Thanks