Android - A Quick and Dirty Http Client

Here's a simple app that you can use to pull data from your website.  I must admit, I was pretty excited to when I pulled data from my website into my Android emulator.

The only trick is that you have to adjust the AndroidManifest.xml file to allow the application to access the internet.  Here's the entry you need to add (put it right after the application element closing tag (</application>)...
  
   <uses-permission android:name="android.permission.INTERNET">
   </uses-permission>

Here's the code...

main.xml...

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="5px"
    android:text="@string/url_string"
    />
<EditText
android:id="@+id/url"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/default_url"
/>
<Button
android:id="@+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/request"
/>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content">
    <TextView
    android:id="@+id/content"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="5px"
    />
</ScrollView>
</LinearLayout>

NKHttpClient (the default Activity)...

package com.remwebdevelopment.nkHttp;

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

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;
/*
 NOTE: You have to give this app permission to access
 the internet!!! See the AndroidManifest.xml file
 */
public class NKHttpClient extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        final EditText txtUrl = (EditText)findViewById(R.id.url);
        final Button btnFetch = (Button)findViewById(R.id.button);
        final TextView txtResult = (TextView)findViewById(R.id.content);
   
        btnFetch.setOnClickListener(new Button.OnClickListener(){
            public void onClick(View v){
                getRequest(txtResult,txtUrl);
            }
        });
    }
   
    public void getRequest(TextView txtResult, EditText txtUrl){
        String url = txtUrl.getText().toString();
        HttpClient client = new DefaultHttpClient();
        HttpGet request = new HttpGet(url);
        try{
            HttpResponse response = client.execute(request);
            txtResult.setText(HttpHelper.request(response));
        }catch(Exception ex){
            txtResult.setText("Failed!");
        }
    }
}

HttpHelper (this class actually does the work)...

package com.remwebdevelopment.nkHttp;

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

import org.apache.http.HttpResponse;

public class HttpHelper {
   
    public static String request(HttpResponse response){
        String result = "";
        try{
            InputStream in = response.getEntity().getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(in));
            StringBuilder str = new StringBuilder();
            String line = null;
            while((line = reader.readLine()) != null){
                str.append(line + "\n");
            }
            in.close();
            result = str.toString();
        }catch(Exception ex){
            result = "Error";
        }
        return result;
    }
}
16 Comments - Average Rating:4.06

Comments:
Thumbs up man...
Rating: 5
Date Posted: October 21st, 2011


the example has a few flaws, while teaching programming please refrain from using shortcut methods
Rating: 2
Date Posted: September 19th, 2011


Very bad practice to issue IO operation on the Gui thread...
Rating: 3
Date Posted: August 6th, 2011


Thanks alot~
Rating: 5
Date Posted: July 29th, 2011


You are great for start in the Android authentication. Please do more good articles.
Rating: 5
Date Posted: July 15th, 2011


Hi and thank you for your tutorial. I find it very interesting but i want to apply this on my app. It is a quiz app, the questions are on a server and i want to put them in a ListView. Do you know how i can do this? I appreciate a lot your help.Thank
Rating: 4
Date Posted: May 31st, 2011


perfect, just what's needed.
Rating: 5
Date Posted: May 24th, 2011


please tell what to enter as url
Rating: 4
Date Posted: April 28th, 2011



RECENT ARTICLES