Non-Blocking Web-Request

import java.io.BufferedInputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import org.apache.http.util.ByteArrayBuffer;

public class Iconic extends Activity {
    private String html = "";
    private Handler mHandler;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    	mHandler = new Handler();
    	checkUpdate.start();
    }

    private Thread checkUpdate = new Thread() {
        public void run() {
            try {
                URL updateURL = new URL("http://iconic.4feets.com/update");
                URLConnection conn = updateURL.openConnection();
                InputStream is = conn.getInputStream();
                BufferedInputStream bis = new BufferedInputStream(is);
                ByteArrayBuffer baf = new ByteArrayBuffer(50);

                int current = 0;
                while((current = bis.read()) != -1){
                    baf.append((byte)current);
                }

                /* Convert the Bytes read to a String. */
                html = new String(baf.toByteArray());
                mHandler.post(showUpdate);
            } catch (Exception e) {
            }
        }
    };

    private Runnable showUpdate = new Runnable(){
       	public void run(){
    	    Toast.makeText(Iconic.this, "HTML Code: " + html, Toast.LENGTH_SHORT).show();
        }
    };
}

13 Comments

#
sissi - April 12, 2009 at 3:44 p.m.

This is really useful... Thanks a lot for sharing!

#
Me - May 2, 2009 at 1:32 a.m.

Nice. I just bookmarked this site. Not sure why it's so hard to find logical code like this on the web but alas, it is. Thanks for posting!

#
Derek - May 5, 2009 at 5:42 p.m.

Adding the import lines would be helpful. I get an unknown socket error when I import java.net. I can't find URL and URLConnection in android.net.

#
chris - May 5, 2009 at 6:29 p.m.

Re: Derek. I've added the import lines now. Hope that helps!

#
Derek - May 5, 2009 at 8:37 p.m.

I didn't realize I'd have to set the Allow Internet permission to the Manifest for the emulator.

[brackets removed]
uses-permission android:name="android.permission.INTERNET"

#
sheik - May 13, 2009 at 9:02 a.m.

hi...i just gone through the code...it is very helpful..can u pls able to add progress dialog or waiting dialog which i am looking through net ... thanks

#
billy - May 26, 2009 at 7:09 a.m.

hi sheik - you might want to try android.os.AsyncTask and use the method onProgressUpdate().

#
Matt - June 21, 2009 at 12:57 p.m.

I agree with billy, you should use AsyncTask that's already in the api ... Google have already written this code for you... you just need to know its there and use it.

#
Aaron - July 21, 2009 at 7:30 p.m.

don't forget to add the following to the manifest xml:

<activity android:name=".Iconic">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>

#
Atif - November 16, 2009 at 6:10 a.m.

Hi Murphy,

would you please compare Handlers with AsyncTask. And which one is better. Thanks

#
Skechers Shape Ups - January 28, 2010 at 12:01 a.m.

I also agree with billy, you should use AsyncTask that's already in the api

#
James - February 19, 2010 at 4:03 p.m.

Adding the import lines proved very helpful indeed.

#
Namyemali - February 23, 2010 at 3:37 a.m.

http://markonzo.edu http://www.netknowledgenow.com/members/effexor-side-effects.aspx http://blog.tellurideskiresort.com/members/celexa-side-effects.aspx http://profiles.friendster.com/neurontin#moreabout http://blog.tellurideskiresort.com/members/buy-clomid.aspx topping choral

Add a Comment