Android網路編程使用HttpClient訪問web網站
HttpClientDemo.java介面就是兩個按鈕和一個文字框
/* * 用HttpClientlai 來訪問提交請求,接收響應 * A,發送GET請求 * 1,建立HttpClient對象;HttpClient httpclient=new DefaultHttpClient(); * 2,發送GET請求,建立HttpGet對象:HttpGet httpget=new HttpGet("http://www.baidu.com"); * 3,用HttpClient對象實行HttpGet對象會得到伺服器響應對象HttpResponse的對象,響應就封裝在HttpResponse中: * HttpResponse httpresponse=httpclient.execute(httpget); * 4,從httpresponse響應中獲得Http執行個體HttpEntity entity=httpresponse.getEntity(); * */public class HttpClientDemo extends Activity { TextView response; //聲明HttpClient對象 HttpClient httpclient; Handler handler=new Handler(){ public void handleMessage(Message msg){ if(msg.what==0x123){// 使用response顯示伺服器的響應 response.append(msg.obj.toString()+"\n"); } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_http_client); //1,建立DefaultHttpClient對象,介面回調HttpClient是個介面 httpclient=new DefaultHttpClient(); response=(TextView) findViewById(R.id.response); } /* * 向服務發送GET請求流程 * * */ public void accessSecret(View v){ response.setText(""); //點擊按鈕,開啟線程,線上程中發送Get請求 new Thread(){ public void run(){ //2,建立一個HttpGet對象 HttpGet httpget=new HttpGet("http://localhost:8080/foo/secret.jsp");//jsp部署在To嗎cat伺服器上 try { //3,用HttpClient對象實行HttpGet對象會得到伺服器響應對象HttpResponse的對象,響應就封裝在HttpResponse中 HttpResponse httpresponse=httpclient.execute(httpget); //4,從httpresponse響應中獲得Http執行個體 HttpEntity entity=httpresponse.getEntity(); if(entity!=null){ //5,entity執行個體中獲得內容,建立輸入資料流,讀取伺服器內容 BufferedReader br=new BufferedReader(new InputStreamReader(entity.getContent())); String line=null; while((line=br.readLine())!=null){//迴圈從輸入資料流中讀取內容 Message msg=new Message(); msg.what=0x123; msg.obj=line; handler.sendMessage(msg);//發給UI線程更新UI組件 } } } catch (ClientProtocolException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} } }.start(); } /* * 發送Post請求流程 * * * */ public void showLogin(View v){ final View loginDialog=getLayoutInflater().inflate(R.layout.login, null); new AlertDialog.Builder(HttpClientDemo.this) .setTitle("登入系統") .setView(loginDialog) .setPositiveButton("確定", new DialogInterface.OnClickListener(){@Overridepublic void onClick(DialogInterface dialog, int which) {// 擷取對話方塊的使用者名稱和密碼final String name=((EditText)loginDialog.findViewById(R.id.name)).getText().toString();final String pass=((EditText)loginDialog.findViewById(R.id.pass)).getText().toString();//點擊確定,開啟線程,線上程中發送Post請求new Thread(){public void run(){try {//2,建立HttpPost對象HttpPost httppost=new HttpPost("http://localhost:8080/foo/login.jsp");//jsp部署在To嗎cat伺服器上//3,對傳遞的參數進行封裝,NameValuePair是簡單名稱值對節點類型List params=new ArrayList();params.add(new BasicNameValuePair("name",name));//添加參數params.add(new BasicNameValuePair("pass",pass));//3,設定編碼httppost.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));//4,HttpClient對象執行HttpPost請求,獲得相應HttpResponse httpresponse=httpclient.execute(httppost);//5,如果狀態代碼是200就表示伺服器成功相應if(httpresponse.getStatusLine().getStatusCode()==200){//200:響應成功,301/302:重新導向,404:not found未找到資源 ,501伺服器遇到錯誤,使其無法對請求提供服務String msg = EntityUtils.toString(httpresponse.getEntity());Looper.prepare();//提示登入成功Toast.makeText(HttpClientDemo.this, msg, Toast.LENGTH_LONG).show();Looper.loop();}} catch (ParseException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}.start();} }).setNegativeButton("取消", null).show(); }