ANDROID HttpURLConnection,HttpClient和最簡單的handler機制
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) */@Overridepublic 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;}}};@Overrideprotected 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) */@Overridepublic void onClick(View v) {sendRequestWithHttpURLConnection();Toast.makeText(MainActivity.this, onclick!1, Toast.LENGTH_SHORT).show();}/** * 開啟線程來發起網路請求 */private void sendRequestWithHttpURLConnection() {new Thread(new Runnable() {@Overridepublic 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 的作用相當於一個StringStringBuilder 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();}@Overridepublic 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;}@Overridepublic 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);}}