Android

Android - A Quick and Dirty Http Client

By Niall Kader1 min read

This article is from the original REM DEV blog (2008–2010). Some of the technologies it covers (Flex, Flash, AIR, JavaFX 1.x) have since been retired.

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;
    }
}