Android Networking - Using Apache HttpClient Library
|
|
Most network-connected Android apps use HTTP to send and receive data. Android contains the Apache's HttpClient Library to perform network operations. In this tutorial we will learn about how to use Apache HttpClient library to make simple HTTP connection in Android.
The followings are some of the useful packages of Apache HttpClient library:
- org.apache.http.HttpResponse
- org.apache.http.client.HttpClient
- org.apache.http.client.methods.HttpGet
- org.apache.http.impl.client.DefaultHttpClient
Below are the code snippets to make a simple HTTP connection using Apache HttpClient library.
1. First create HttpClient object.
HttpClient httpclient=new DefaultHttpClient();
2. To retrieve the information from the server use the HttpGet class constructor.
HttpGet request=new HttpGet(“http://www.androidaspect.com”);
3. Then pass the HttpGet object in method execute() of HttpClient class to retrieve HttpResponse object.
HttpResponse response = httpclient.execute(request);
4. Then read the response received.
BufferedReader rd = new BufferedReader
(new InputStreamReader(response.getEntity().getContent()));
String line = "";
while ((line = rd.readLine()) != null) {
Log.d(“output: ”,line);
}
(new InputStreamReader(response.getEntity().getContent()));
String line = "";
while ((line = rd.readLine()) != null) {
Log.d(“output: ”,line);
}
Note: To perform the network operations you must add the permission "android.permission.INTERNET" to "AndroidManifest.xml" file.
The following is the final code to make a simple HTTP connection using Apache HttpClient library.
Project Name : ApacheHttpClientExample
Package : com.sunil.apachehttpclient
main.xml
ApacheHttpClientExample.java
Output
That's it ! You can also download the source code of this tutorial.
Final Code
The following is the final code to make a simple HTTP connection using Apache HttpClient library.
Project Name : ApacheHttpClientExample
Package : com.sunil.apachehttpclient
Main Activity : ApacheHttpClientExample.java
Layout : main.xml
Build Target : Android 2.2
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sunil.apachehttpclient"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="5"
android:targetSdkVersion="8" />
<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.sunil.apachehttpclient.ApacheHttpClientExample"
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>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sunil.apachehttpclient"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="5"
android:targetSdkVersion="8" />
<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.sunil.apachehttpclient.ApacheHttpClientExample"
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>
main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="10dip"
android:layout_marginTop="10dip"
android:text="R E S U L T" />
<EditText
android:id="@+id/etResult"
android:layout_width="match_parent"
android:layout_height="fill_parent" />
</LinearLayout>
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="10dip"
android:layout_marginTop="10dip"
android:text="R E S U L T" />
<EditText
android:id="@+id/etResult"
android:layout_width="match_parent"
android:layout_height="fill_parent" />
</LinearLayout>
ApacheHttpClientExample.java
package com.sunil.apachehttpclient;
import java.io.BufferedReader;
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.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.EditText;
public class ApacheHttpClientExample extends Activity {
HttpClient httpclient;
HttpGet request;
HttpResponse response;
EditText result;
/* Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
result = (EditText) findViewById(R.id.etResult);
// Try to connect using Apache HttpClient Library
try{
httpclient=new DefaultHttpClient();
request=new HttpGet("http://www.androidaspect.com");
response = httpclient.execute(request);
}
catch (Exception e){
//Code to handle exception
}
// response code
try{
BufferedReader rd = new
BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String line = "";
while ((line = rd.readLine()) != null) {
result.append(line);
}
}
catch (Exception e){
//Code to handle exception
}
}
}
import java.io.BufferedReader;
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.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.EditText;
public class ApacheHttpClientExample extends Activity {
HttpClient httpclient;
HttpGet request;
HttpResponse response;
EditText result;
/* Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
result = (EditText) findViewById(R.id.etResult);
// Try to connect using Apache HttpClient Library
try{
httpclient=new DefaultHttpClient();
request=new HttpGet("http://www.androidaspect.com");
response = httpclient.execute(request);
}
catch (Exception e){
//Code to handle exception
}
// response code
try{
BufferedReader rd = new
BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String line = "";
while ((line = rd.readLine()) != null) {
result.append(line);
}
}
catch (Exception e){
//Code to handle exception
}
}
}
Output
That's it ! You can also download the source code of this tutorial.










Post a Comment