Hanlder HttpURL...

来源:互联网 发布:灵异事件知乎 编辑:程序博客网 时间:2024/05/03 09:06

A Handler allows you to send and process Message and Runnable objects associated with a thread'sMessageQueue. Each Handler instance is associated with a single thread and that thread's message queue. When you create a new Handler, it is bound to the thread / message queue of the thread that is creating it -- from that point on, it will deliver messages and runnables to that message queue and execute them as they come out of the message queue.

There are two main uses for a Handler: (1) to schedule messages and runnables to be executed as some point in the future; and (2) to enqueue an action to be performed on a different thread than your own.

Scheduling messages is accomplished with the post(Runnable),postAtTime(Runnable, long),postDelayed(Runnable, long),sendEmptyMessage(int),sendMessage(Message),sendMessageAtTime(Message, long), andsendMessageDelayed(Message, long) methods. Thepost versions allow you to enqueue Runnable objects to be called by the message queue when they are received; thesendMessage versions allow you to enqueue a Message object containing a bundle of data that will be processed by the Handler'shandleMessage(Message) method (requiring that you implement a subclass of Handler).

When posting or sending to a Handler, you can either allow the item to be processed as soon as the message queue is ready to do so, or specify a delay before it gets processed or absolute time for it to be processed. The latter two allow you to implement timeouts, ticks, and other timing-based behavior.

When a process is created for your application, its main thread is dedicated to running a message queue that takes care of managing the top-level application objects (activities, broadcast receivers, etc) and any windows they create. You can create your own threads, and communicate back with the main application thread through a Handler. This is done by calling the samepost or sendMessage methods as before, but from your new thread. The given Runnable or Message will then be scheduled in the Handler's message queue and processed when appropriate.

类概述

一个处理程序员允许您发送和过程Message和Runnable对象关联到一个线程的MessageQueue.每个处理程序实例相关联的单个线程和线程的消息队列,当你创建一个新的处理程序。它绑定到线程/线程的消息队列创建它---才能够那时起,它将提供消息和runnables消息队列和执行他们的消息队列。

处理程序有两个主要用途(1)调度信息和执行runnable在将来的某个时间点,和(2)排队一个动作比你自己的一个不同的线程上执行。

完成的调度信息post(Runnable),postAtTime(Runnable,long),postDelayed(Runnable,long),sendEmptyMessage(int),sendMessage(Message),sendMessageAtTime(Message,long),sendMessageDelayed(Message,long)方法。发布版本允许您将Runnable对象被称为由消息队列时收到;sendMessage版本允许您将一个Message对象包含一个包的数据将由处理程序的处理handleMessage(Message)方法(处理程序要求您实现一个子类)。

发布或发送到处理程序时,您可以允许项目紧靠处理消息队列准备这样做,或指定一个延迟之前加工处理或绝对时间,后两个允许您实现超时,和其他timing-base行为。为应用程序创建进程时,其主要致力于运行一个线程消息,负责管理项级应用程序对象(活动、广播接收器等)和他们创建的任何windows。您可以创建自己的线程,并且与主应用程序线程通信通过一个处理程序。这是通过调用相同的帖子或sendMessage方法和之前一样,但从你的新线程,给定的Runnable或消息被安排在处理程序的消息队列,并在适当的时候处理

Looper
Class Overview

--------------------------------------------------------------------------------

Class used to run a message loop for a thread. Threads by default do not have a message loop associated with them; to create one, call prepare() in the thread that is to run the loop, and then loop() to have it process messages until the loop is stopped.

Most interaction with a message loop is through the Handler class.

This is a typical example of the implementation of a Looper thread, using the separation of prepare() and loop() to create an initial Handler to communicate with the Looper.

  class LooperThread extends Thread {
      public Handler mHandler;
 
      public void run() {
          Looper.prepare();
 
          mHandler = new Handler() {
              public void handleMessage(Message msg) {
                  // process incoming messages here
              }
          };
 
          Looper.loop();
      }
  }

类用于运行一个消息循环的线程,默认情况下没有一个消息循环的线程与他们联系在一起,创建一个prepare()在运行的线程循环,然后loop()它处理消息,直到停止循环。

大多数与一个消息循环是通过交互Handle类。

这是一个典型的列子的实现底线分线,使用的分离prepare()和loop()创建一个初始化处理程序与它进行通信

MessageQueue
Class Overview

--------------------------------------------------------------------------------

Low-level class holding the list of messages to be dispatched by a Looper. Messages are not added directly to a MessageQueue, but rather through MessageQueue.IdleHandler objects associated with the Looper.

You can retrieve the MessageQueue for the current thread with Looper.myQueue().

低级类控股派出的消息列表Looper。消息不实直接添加MessageQueue,而是通过MessageQueue。IdleHandler与之相关的对象。您可以检索的MessageQueue当前线程Looper.myQueue()。

HttpURLConnection
Class Overview

--------------------------------------------------------------------------------

An URLConnection for HTTP (RFC 2616) used to send and receive data over the web. Data may be of any type and length. This class may be used to send and receive streaming data whose length is not known in advance.

于HTTP个URLConnection(RFC 2616)用于在网络上发送和接收数据。数据可以是任何类型和长度。这个类可以被用来发送和接收数据流的长度是事先不知道的

Uses of this class follow a pattern:

Obtain a new HttpURLConnection by calling URL.openConnection() and casting the result to HttpURLConnection.
Prepare the request. The primary property of a request is its URI. Request headers may also include metadata such as credentials, preferred content types, and session cookies.

获得新的httpurlconnection by呼唤url.openconnection()and the result to httpurlconnection铸造。

Prepare the request. The primary property of a request is its URI. Request headers may also include metadata such as credentials, preferred content types, and session cookies.

prepare the request。the primary property of a request is its URI。请求headers may also include元数据such as代表资格审查优先高兴的类型,会话Cookie


Optionally upload a request body. Instances must be configured with setDoOutput(true) if they include a request body. Transmit data by writing to the stream returned by getOutputStream().

可选的上传请求正文。实例必须配置setdooutput(真的)如果他们包括请求正文。数据写入返回getoutputstream()
Read the response. Response headers typically include metadata such as the response body's content type and length, modified dates and session cookies. The response body may be read from the stream returned by getInputStream(). If the response has no body, that method returns an empty stream.
Disconnect. Once the response body has been read, the HttpURLConnection should be closed by calling disconnect(). Disconnecting releases the resources held by a connection so they may be closed or reused.

读取响应。响应头通常包括元数据如响应正文的内容类型和长度,修改日期和会话cookie。响应体可以从流读取返回的getinputstream()。如果响应没有身体,这个方法将返回一个空流。

断开。一旦身体反应已读,HttpURLConnection应该通过调用disconnect()关闭。断开连接释放所持有的所以他们可以被关闭或重复使用资源

 

For example, to retrieve the webpage at http://www.android.com/:

   URL url = new URL("http://www.android.com/");
   HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
   try {
     InputStream in = new BufferedInputStream(urlConnection.getInputStream());
     readStream(in);
    finally {
     urlConnection.disconnect();
   }
 }
Secure Communication with HTTPS
Calling openConnection() on a URL with the "https" scheme will return an HttpsURLConnection, which allows for overriding the default HostnameVerifier and SSLSocketFactory. An application-supplied SSLSocketFactory created from an SSLContext can provide a custom X509TrustManager for verifying certificate chains and a custom X509KeyManager for supplying client certificates. See HttpsURLConnection for more details.
Response Handling
HttpURLConnection will follow up to five HTTP redirects. It will follow redirects from one origin server to another. This implementation doesn't follow redirects from HTTPS to HTTP or vice versa.

httpurlconnection will follow up to five HTTP重定向。它将跟随重定向到另一个从一个源服务器。implementation跟随重定向from to HTTPHTTPS亦然。


 

 

0 0
原创粉丝点击