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
This is really useful... Thanks a lot for sharing!
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!
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.
Re: Derek. I've added the import lines now. Hope that helps!
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"
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
hi sheik - you might want to try android.os.AsyncTask and use the method onProgressUpdate().
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.
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>
Hi Murphy,
would you please compare Handlers with AsyncTask. And which one is better. Thanks
I also agree with billy, you should use AsyncTask that's already in the api
Adding the import lines proved very helpful indeed.
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