Non-Blocking Web-Request
来源:互联网 发布:域名哪家好 编辑:程序博客网 时间:2024/05/21 06:48
This code fetches content from the web without blocking the UI (runs in the background in a Thread). Once finished, it posts a Handler that is picked up by the UI as soon as possible.
- 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();
- }
- };
- }
原文:http://www.androidsnippets.org/snippets/1/
- Non-Blocking Web-Request
- Non-blocking Web Service Client
- Non blocking IO web server
- Blocking vs. Non-Blocking Sockets
- non-blocking and blocking mode
- Blocking vs. non-blocking sockets
- Callback,Non-Blocking,Blocking函数
- P2P non-blocking
- Tornado Non-blocking Process
- 关于non-blocking
- Non-Blocking Channel Operations
- asynchronous vs non-blocking
- PNP: non-blocking IO
- Synchronous/Asynchronous/Blocking/Non-blocking I/O
- 2.5. Non-blocking logging daemon
- Non-blocking BSD socket connections
- Java NIO: Non-blocking Server
- Java NIO: Non-blocking Server
- PM 应该做什么 - 思考ing
- DSP程序优化方法(3)
- DSP程序优化方法(4)
- 新研究方向
- Web实习生两月记
- Non-Blocking Web-Request
- 创世纪
- Autostart an application at bootup
- How to Make an Activity Fullscreen
- 详解:数据库名、实例名、ORACLE_SID、数据库域名(一)
- 详解:数据库名、实例名、ORACLE_SID、数据库域名(二)
- Ubuntu 8.10无法保存网络设置问题
- ORACLE 数据库名、实例名、ORACLE_SID的区别
- Memory Size Class for viewing available storage