标签:
handler机制在Android开发中主要用于主线程和子线程的沟通,子线程发送必要的信息给主线程,然后在主线程中更新ui;
package com.example.webview; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.support.v7.app.ActionBarActivity; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends ActionBarActivity implements OnClickListener{ public static final int SHOW_RESPONSE=0; private TextView reponseText; private Button sendButton; private Handler handler=new Handler(){ /* (non-Javadoc) * @see android.os.Handler#handleMessage(android.os.Message) */ @Override public void handleMessage(Message msg) { // 接收消息并且去更新UI线程上的控件内容 switch (msg.what) { case SHOW_RESPONSE: String response=(String) msg.obj; reponseText.setText(response); break; default: Toast.makeText(MainActivity.this, "show fail!!", Toast.LENGTH_SHORT).show(); break; } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); sendButton=(Button) findViewById(R.id.send_request_id); reponseText=(TextView) findViewById(R.id.text_id); sendButton.setOnClickListener(this); } /* (non-Javadoc) * @see android.view.View.OnClickListener#onClick(android.view.View) */ @Override public void onClick(View v) { sendRequestWithHttpURLConnection(); Toast.makeText(MainActivity.this, "onclick!1", Toast.LENGTH_SHORT).show(); } /** * 开启线程来发起网路请求 */ private void sendRequestWithHttpURLConnection() { new Thread(new Runnable() { @Override public void run() { HttpURLConnection connection=null; try { URL url=new URL("http://www.baidu.com"); connection=(HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); // 设置连接超时 connection.setConnectTimeout(8000); // 设置读取超时 connection.setReadTimeout(8000); // 获取输入流,进行读取 InputStream inputStream=connection.getInputStream(); BufferedReader reader=new BufferedReader(new InputStreamReader(inputStream)); // 下面这个StringBuilder 的作用相当于一个String StringBuilder response=new StringBuilder(); while (reader.readLine()!=null) { // 读一行就,把读到的添加到response里面 response.append(reader.readLine()); } // 调用dandler的obtainmessage方法,得到一个message对象 Message message=handler.obtainMessage(); /* 一个message对象,public static Message obtain(Handler h, int what, int arg1, int arg2, Object obj), h:处理消息的目标Handler对象; what:消息的编码; arg1:附加的整数数据; arg2:附加的整数数据; obj:附件的Object类型数据。 返回值:从全局的对象池中返回一个Message对象。*/ message.what=SHOW_RESPONSE; message.obj=response.toString(); // 这里是子线程,这个用handler发送信息给主线程 handler.sendMessage(message); } catch (Exception e) { // TODO: handle exception }finally{ connection.disconnect(); } } }).start(); Toast.makeText(MainActivity.this, "thread start", Toast.LENGTH_SHORT).show(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }
ANDROID HttpURLConnection,HttpClient和最简单的handler机制
标签:
原文地址:http://blog.csdn.net/ericzengyan/article/details/46455541