HTTP Connection Using Asynctask

We can make HTTP connection by many ways but Asynctask (a helper class which allows us to execute lengthy operations on a separate thread) is the best way for connection because by this method all lengthy operation done in background and our application doesn’t crash specially when when our internet connection is slow.

Source code :

Layout Designing: create a xml file named main.xml and add one Button and TextView.

main.xml

<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:orientation=”vertical” >

<Button
android:id=”@+id/readWebpage”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:onClick=”onClick”
android:text=”Load Webpage” />

<TextView
android:id=”@+id/TextView01″
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:text=”Placeholder” />
</LinearLayout>

mainifest.xml

Give Internet Permission to your application. Permission->Add->Uses-Permission-> Select android.permission.INTERNET.

<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
package=”com.anish.asynctask”
android:versionCode=”1″
android:versionName=”1.0″ >
<uses-sdk android:minSdkVersion=”8″ android:targetSdkVersion=”17″ />
<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.anish.asynctask.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>

MainActivity.java

package com.anish.asynctask;

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

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

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends Activity {
private TextView textView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textView = (TextView) findViewById(R.id.TextView01);
}
//AsyncTask function to use HTTP Connection
private class DownloadPage extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String… urls) {
String response = “”;
InputStream isr= null;
for (String url : urls) {
try {
DefaultHttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
HttpResponse execute = client.execute(httpGet);
isr = execute.getEntity().getContent();
BufferedReader buffer = new BufferedReader(new InputStreamReader(isr));
String s = “”;
while ((s = buffer.readLine()) != null) {
response += s;
}

} catch (Exception e) {
e.printStackTrace();
}
}
return response;
}

@Override
protected void onPostExecute(String result) {
textView.setText(result);
}
}
public void onClick(View view) {
DownloadPage task = new DownloadPage();
task.execute(new String[] { “https://workinvisible.wordpress.com&#8221; });
}
}

If You Have Any Problem or Suggestion For Me Then Drop Me a Mail. I Will Wait For Your Anticipated Feedback and Suggestion.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s